Jump to content

Retaining Values In Drop Down Menu


AJayUK

Recommended Posts

Hi guys, I had a problem before which is similar to the problem im about to describe, however Pikachu helped me solve the last problem so a big thanks to him! However this problem is slightly different and im struggling to find a solution. I will first describe the problem I had which pikachu helped me solve as it would be easier for me to describe this past problem.

 

I have an update form, so a user can edit existing records which are pulled from a database. When the user changes the values in a text field then submits the form, the values in the text fields updates fine. However if they change the values in the text box and then tries to submit the form but there was an error, the values in the text fields go back to the values from the database and the user has to change the text fields again.

 

Well this problem was solved with the help of pikachu and the this is what I did...

 

<?php if(isset($_POST['title'])){echo htmlspecialchars($_POST['title']);} else if (isset($title)) {echo htmlspecialchars($title);}?>

 

Again, the above works fine however the problem I have now is the same problem as described above however it relates to drop down menus and not text fields. I can't seem to figure out how to retain the values in a drop down menu the same way I have for text fields. Below is the code that im using that I thought would work however it isnt working, the values of the drop down menu (if there was an error) are going back to the values from the database. Heres the code...

 

<option value="Psychopathic" <?php if ((isset($_POST['category']))&&($_POST['category'] == 'Psychopathic')) { echo ' selected=selected'; }  else if ((isset($category))&&($category == 'Psychopathic')) { echo ' selected=selected'; } ?>>Psychopathic</option>

 

Does anybody know what i am doing wrong and how i can fix this problem? I understand what i need to do but i cant seem to get the code to do what i want to do and thats to retain values of a drop down menu when the page is returned with an error.

 

Any help would be much appreciated.

 

AJay

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.