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

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

Would appreciate any reply to my question.

Thanks!

Dshan

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


P: n/a
ds***@web.de wrote:
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
Would appreciate any reply to my question.

Thanks!

Dshan


Jun 20 '06 #2

P: n/a
Erwin Moller
<si******************************************@spam yourself.com> wrote in
news:44***********************@news.xs4all.nl:
ds***@web.de wrote:
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.)


Rather than use JS, I would do this with php alone. Ust use php to write
the options for the select control. As php is looping through all the
states, a php "if" statement can determine if this state has been
selected, and include the "selected" for this option.
Jun 20 '06 #3

P: n/a

Erwin Moller wrote:
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

Jun 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.