Reputation: 113
I have an HTML page that has a form in it. The form then posts to a PHP page where it sends out an email to the user. I have a 'State' dropdown box with values of 1-58(I need these for my database). Is there a way to get the name of the state instead of the number value when sending the email?
My HTML:
<Select name="State" id="State" style="width:40px;" >
<option value="17"></option>
<option value="18">Alabama</option>
<option value="19">Alaska</option>
<option value="20">American Samoa</option>
<option value="21">Arizona</option>
<option value="22">Arkansas</option>
<option value="15">California</option>
<option value="6">Canada</option>
<option value="11">Colorado</option>
<option value="23">Connecticut</option>
<option value="24">Delaware</option>
<option value="25">District Of Columbia</option>
<option value="26">Federated States Of Micronesia</option>
<option value="7">Florida</option>
<option value="27">Georgia</option>
<option value="28">Guam</option>
<option value="29">Hawaii</option>
<option value="30">Idaho</option>
<option value="8">Illinois</option>
<option value="14">Indiana</option>
<option value="31">Iowa</option>
<option value="32">Kansas</option>
<option value="33">Kentucky</option>
<option value="34">Louisiana</option>
<option value="35">Maine</option>
<option value="36">Marshall Islands</option>
<option value="37">Maryland</option>
<option value="38">Massachusetts</option>
<option value="1">Michigan</option>
<option value="5">Minnesota</option>
<option value="39">Mississippi</option>
<option value="40">Missouri</option>
<option value="41">Montana</option>
<option value="42">Nebraska</option>
<option value="43">Nevada</option>
<option value="44">New Hampshire</option>
<option value="12">New Jersey</option>
<option value="45">New Mexico</option>
<option value="46">New York</option>
<option value="13">North Carolina</option>
<option value="47">North Dakota</option>
<option value="4">Ohio</option>
<option value="48">Oklahoma</option>
<option value="49">Oregon</option>
<option value="10">Pennsylvania</option>
<option value="50">Rhode Island</option>
<option value="51">South Carolina</option>
<option value="52">South Dakota</option>
<option value="53">Tennessee</option>
<option value="2">Texas</option>
<option value="54">Utah</option>
<option value="55">Vermont</option>
<option value="3">Virginia</option>
<option value="56">Washington</option>
<option value="57">West Virginia</option>
<option value="9">Wisconsin</option>
<option value="58">Wyoming</option>
</Select>
Current PHP $message
line:
"\nState: " . stripslashes($_POST["State"]) .
The emails I'm receiving are saying "State: 1"
instead of "State: Michigan".
Thanks.
Upvotes: 1
Views: 2340
Reputation: 22947
Have an array control all the values. Use this same array to show the drop down option, then use the same array to reference the value.
<?php
//Place state array at the top of the file
$states = array(
18 => "Alabama",
19 => "Alaska",
...
);
//Generate select box
if(is_array($states)) {
echo "<select name='state' id='state' style='width:40px'>";
foreach($states as $id => $name) {
echo "<option value='$id'>$name</option>";
}
echo "</select>";
}
//Form POSTed
if(is_array($_POST)) {
//Send email message
$email = "State: ".$states[$_POST['state']];
//Do email sending or message displaying
//Submit to datebase
$query = "INSERT INTO table ('state') VALUES ('".mysqli_escape_string($_POST['state'])."')";
//Do insert into mysql
}
?>
Upvotes: 1
Reputation: 12508
The value
attribute is what is posted to the PHP side. In your case, each state is assigned a numeric value and that is posted to the server. Simply change your drop down so your text and values are the same like so:
<option value="Michigan">Michigan</option>
<option value="New York">New York</option>
...
When posted, the name will be posted rather than the number. Your could:
$_POST['state']
would now return Michigan
or New York
, etc.
The only other way to accomplish this is to create an array in PHP where the index is the numerical value of your option and the array value is the state name (i.e. Michigan, New York, etc.). Your front end code would not change. You would then receive the numerical value and have to map it against this array.
Hope this helps.
Upvotes: 0
Reputation: 7092
it will work as you want it if you delete the values. With no value attribute the real value will be used (whatever is between the option tags)
eg
Replace
<option value="8">Illinois</option>
with this
<option>Illinois</option>
Upvotes: 1