0

I have a sign-up form in php, which sends data to another php form for insertion into mysql

If any of the fields are missing, an error mesage is sent back to sign-up form using session variable (value="<?php echo $_SESSION['error']; ?>") along with all the fields the user had already filled up ( so that they dont have to fill it up again ) using session variables as well..

for text boxes im using value="<?php echo $_SESSION['fname']; ?>" which works fine.. but this doesnt seem to work for drop down boxes or radio buttons..

any suggestions?

1
  • Hmmm.. trying to imagine what is source code, hmm... I failed. Please post it here, then. Commented Nov 5, 2011 at 8:15

2 Answers 2

2

Drop down boxes and radio buttons use a selected or click index. You need to check for each one if it was selected or clicked and give it the appropriate attribute.

 <?php if($_SESSION['dropBox1'] == "value") echo ' selected="selected"'; ?>
Sign up to request clarification or add additional context in comments.

Comments

1

First, you shouldn't submit the form to another page for validation. Do it in the current page. And drop down lists and radio buttons value attribute is the value that is tied to the selection. Not what is displayed or in any way tells it that it is the selected item. @James beat me for code on how to show which is selected.

<select>
  <option value="0"  <?php echo $_SESSION['dropBox1'] == "0" ? ' selected="selected"' : '' ?>>option1</option>
  <option value="1"  <?php echo $_SESSION['dropBox1'] == "1" ? ' selected="selected"' : '' ?>>option2</option>
  <option>etc..</option>
</select>

4 Comments

Shredder, the original form was in html.. which is the reason why i had it post to separate php for submission, i just changed the form to php to get the session functionality.. ( are there any benefits of having it post to itself?) thanks
@ABI Even if it is in html, you can use javascript to do your form validation before submission. This is convenient for the user so they don't have to wait for 1 (or 2 in the case of validating in another page) http requests cycles. It is instant, done on the client. If the data is good, then you can post it to your other page for mysql insert
using the ternary operator you don't havemto specify echo
@Marcx Yes, you need echo in there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.