By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,639 Members | 2,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,639 IT Pros & Developers. It's quick & easy.

Saving selection in drop-down lists in PHP after page reload?

P: n/a
Hi all,

I can't find an answer to my quesiton anywhere.

Given a drop-down list with USA states and other text fields I pass
this information for further processing via PHP. If any of the required

fields is empty, the PHP script would return an error and reload the
form page asking to fill out those fields. All entered information is
saved... except for the drop-down list selection. I tried to write a
Javascript function which is echoed by PHP, but interpolation of PHP
variables in the Javascript section gives a syntax error message in
Javascript.

The PHP script returns, e.g., an "AL" string for Alabama. Of course, I
don't want to write PHP code for each option to make it selected.

Would appreciate any reply to my question.

Thanks!

Dshan

Jun 19 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Message-ID: <11**********************@u72g2000cwu.googlegroups .com> from
ds***@web.de contained the following:
The PHP script returns, e.g., an "AL" string for Alabama. Of course, I
don't want to write PHP code for each option to make it selected.

The way I do it requires some setup, but you only do it once,
First you create an array of states and codes

e.g.

$states= array("AL"=>"Alabama","TX"=>"Texas","VA", "Virginia",...);

then use PHP to create your select box

Untested.

<select name='state'>
<?php
foreach($states as $key=>$value){
if(isset($_POST[$key]) &&$_POST[$key]==$value){
$selected="selected";
}
else{
$selected="";
}
echo"<option value='$key' $selected >$value</option>\n";

}
?>
</select>
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jun 19 '06 #2

P: n/a
Thank you, Geoff,

You did it! I made some changes to your code, it works fine!

Geoff Berrow wrote:
Message-ID: <11**********************@u72g2000cwu.googlegroups .com>


Jun 20 '06 #3

P: n/a
Message-ID: <11**********************@i40g2000cwc.googlegroups .com> from
dshan contained the following:
Thank you, Geoff,

You did it! I made some changes to your code, it works fine!


Did I make a mistake?
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jun 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.