Click here to Skip to main content
15,441,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a form, which shows user information and at the same time allows user to update the information, if they wish. It works perfectly with all other input fields, but not with select options. When I click on update, I get a message that the this field is empty, unless I chose "Male" or "Female" from the dropdown.
<?php echo $sex; ?>
between the option tags is displayed correctly. Any ideas on how I can get it to work? The code is below. Thanks a million in advance!

  <select class="input-settings settings" value="<?php echo $sex; ?>" name="sex">
 <option value="<?php echo $sex; ?>" selected disabled><?php echo $sex; ?></option>
 <option value="Male">Male</option>
 <option value="Female">Female</option>

What I have tried:

I have searched the internet, tried to move and remove
<?php echo $sex; ?>
from option tag, nothing seems to help.
Updated 14-Jul-21 21:49pm
Richard MacCutchan 14-Jul-21 13:48pm    
You have not declared a variable named $sex.
InnaTS 14-Jul-21 18:01pm     CRLF
I have declared it in another file (script below), that I referenced to on the top of the script in question using require_once. As mentioned above, there's no problem with other input fields (it's a larger form) and variables in them, only the select option. Besides, I can see the correct value in between the first option tags in the script posted above. But when I try to save it into my database, it appears to be empty. Is there another way to declare it? $sql = "SELECT * FROM users WHERE id = :id"; if($stmt = $pdo->prepare($sql)){ $stmt->bindParam(":id", $id, PDO::PARAM_INT); $param_id = $id; if($stmt->execute()){ $result = $stmt->fetch(PDO::FETCH_ASSOC); $fname = $result['fname']; $lname = $result['lname']; $email = $result['email']; $country = $result['country']; $bdate = $result['bdate']; $sex = $result['sex']; } }
Richard Deeming 15-Jul-21 3:47am    
name="sex" is what controls how it's posted back to the server. The id is purely client-side. :)
Richard MacCutchan 15-Jul-21 4:05am     CRLF
Hence the prefix to my message. I actually looked this up on both W3Schools and MDN but did not find it clearly spelled out; or I just misunderstood what was written. [edit] I was looking at the option tag not the select. [/edit]
Richard Deeming 15-Jul-21 4:09am     CRLF
It could probably be clearer, but the MDN documentation does mention it in the first paragraph: <select>: The HTML Select element - HTML: HyperText Markup Language | MDN[^] "It is given an id attribute to enable it to be associated with a <label> for accessibility purposes, as well as a name attribute to represent the name of the associated data point submitted to the server." <option> tags generally don't have IDs either, unless you need to target a specific one from Javascript. :)

1 solution

<option value="<?php echo $sex; ?>" selected disabled>
Disabled items are usually not sent back to the server when the form is submitted. Remove the disabled attribute from this option.
Share this answer
InnaTS 18-Jul-21 4:36am     CRLF
It worked, thank you very much! I just wanted to have two options to choose from. I've added an if statement instead, that determines the other option. Thanks again guys! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900