Hi everyone i cant seem to get my drop down boxes to retain the data the user selected is this code right?

<select name="bap1" id="bap1" onchange='this.form.submit()' value="" >
      <option>Select One</option>
      <option value='Energy Efficient Cooking' <?php if($_POST['bap1'] == 'Energy Efficient Cooking') echo "selected"?>>Energy Efficient Cooking</option>

Tahnks in advance anyone!!

Recommended Answers

All 3 Replies

Member Avatar for diafol

XHTML's a bit off:

<select name="bap1" id="bap1" onchange="this.form.submit()" value="" >
      <option>Select One</option>
      <option value="Energy Efficient Cooking" <?php if($_POST['bap1'] == 'Energy Efficient Cooking') echo "selected = \"selected\"";?>>Energy Efficient Cooking</option>

Should work - remember always double quote (") in XHTML - not interchangeable with (') as in JS/PHP. Also all attributes must have a value, therefore selected = "selected" and checked = "checked" should be used.
The attribute stuff is not important if you just want to get something working, but it may fail XHTML validation.

In addition, I would strongly advise that you 'clean' your $_POST variables. Otherwise, this should work... unless I missed something.

does a javascript submit refresh the page?

I ask, I dont use javascript much
'selected' is written by the server
if a javascript submit does not refresh the page selected will not be written,
I think..?

thanks as always!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.