Saving selection in drop-down lists via JavaScript after page reload?

D

dshan

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. How can I
save user selection (Alabama) using JavaScript in this case?

Would appreciate any reply to my question.

Thanks!

Dshan
 
E

Erwin Moller

Hi all,
Hi,


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.

In that case you made a syntax error.
Just look at the source from your browser and see what PHP wrote and why it
is bad syntax.
Remember that PHP has NOTHING to do with Javascript, except for the fact it
has produced it.
Your JS should be syntactically allright, then the browser will execute it.
Simple as that.
The PHP script returns, e.g., an "AL" string for Alabama. How can I
save user selection (Alabama) using JavaScript in this case?

that can be done in many ways.
You can print the SELECTED from PHP in case you found the state.

First look at the source, and fix the syntax error (Use firefox if you need
debugging.)

Regards,
Erwin Moller
 
D

dshan

Erwin said:
In that case you made a syntax error.
Just look at the source from your browser and see what PHP wrote and why it
is bad syntax.
Remember that PHP has NOTHING to do with Javascript, except for the fact it
has produced it.
Your JS should be syntactically allright, then the browser will execute it.
Simple as that.
that can be done in many ways.
You can print the SELECTED from PHP in case you found the state.

First look at the source, and fix the syntax error (Use firefox if you need
debugging.)

Regards,
Erwin Moller


Thank you Erwin and Jim,

In a test file (without PHP) JS worked fine, I don't know exactly where
the problem was.

What I did, I fixed the problem by creating a PHP array of states,
e.g., "TX" => "Texas", and made a loop like this:

echo "<select name=state class=dropdown>";
foreach($states as $key => $value){
if($key == $rt['state']){
$selected="selected";
}
else{
$selected="";
}
echo"<option value=$key $selected>$value</option>";
echo "</select>"

Thank you again for your replies!

Regards,

Dshan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top