0

I making a form that will take contact info and send it to an email. It works expect the cities which I have more than 2000 options. The cities drop down sends the value, which is just a number. I want the form to send the city, which is HTML element content not the value number in the attribute. I do not want to replace the 2000 different values with the cities. I know a loop would the way to go but I don't know how to create it.

HTML -

<option value="2" >Los Angeles - CA</option>

PHP -

$city = $_REQUEST['city'] ;

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
       mail( "$webmaster_email", "Submission", "$city,"From:$email" );
header( "Location: $thankyou_page" );
4
  • Why a loop? Use an array. Commented Jul 3, 2013 at 19:39
  • how and where are the 2000 cities options stored? Commented Jul 3, 2013 at 19:39
  • I have a gigantic list of 2000 options Commented Jul 3, 2013 at 19:43
  • Are you willing to add Javascript to your page? Commented Jul 3, 2013 at 19:50

3 Answers 3

1

I believe you want to send the text and not the value from the option. Right?

I suggest you to create this element on your form: <input type='hidden' id='city_name' name='city_name' /> to hold the select value with JavaScript when user select his desired option. Then, create and event on the select like this:

<select onchange="document.getElementById('city_name').value=this.options[this.selectedIndex].text;">

So you'll have the $_POST['city_name'] available on your PHP code.

Sign up to request clarification or add additional context in comments.

4 Comments

Correct I want to capture the plain HTML text within the element. Then I want to send it to the email not a number because when the office gets the number they won't know what to do with it.
So that code will do it for you. I didn't tested it, but it should work. By the way, syntax highlight's showing that you have and error on your code in here "$city, fix that to this "$city", or you'll have a syntax error.
forgive my newest but is this all the Js I need in the head?<script type="text/javascript"> onchange="document.getElementById('city_name').value=this.options[this.selectedIndex].text;" </script>
Its not that way. Look my edit. The event goes on the select tag like this: <select onchange="document.getElementById('city_name').value=this.options[this.selectedIndex].text;">
1

Use an array! Example:

<?php
    $cities = array(
        '0' => 'New York - NY',
        '1' => '...'
        '2' => '...'
    );
    if (isset($cities[$_REQUEST['city']])) {
        $city = $cities[$_REQUEST['city']];
    } else {
        $city = $_REQUEST['city'];
    }

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        mail( "$webmaster_email", "Submission", "$city,"From:$email" );
        header( "Location: $thankyou_page" );
    }
?>

Comments

0

Presumably you produced that <option> list with something on the server, e.g. an array:

$locations = array(
   ...
   2 => 'Los Angeles - CA'
   ...
);

then you'd simply have

$place = $locations[$_REQUEST['city']];

to get the text for the option.

Alternatively, you embed the full city name in the option:

<option value="Los Angeles - CA">Lost Angeles - CA</option>

3 Comments

The question says he doesn't want to rewrite all the options in the second way.
@user2101421 where is this list? In your JS or HTML or on a database?
@barmar: doesn't matter. either there's a list of the options available to the code somewhere, or the html list has to be rewritten. The form will submit 2 as written in the OP's code, and there is NO way to translate that 2 into Los Angeles without some kind of lookup/translation system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.