kamal_g Posted August 6, 2015 Share Posted August 6, 2015 -2 down vote favorite I have a form with many fields and a drop down list for the title. In the drop down list as shown below, I can select the title and update the mysql table properly, when the record is selected for the edition, the title is displayed appropriately too. However if the title is not modified the update sends null to the title value. How can I ensure the title is not updated if not modified? The title part is attached here. can someone send me a sample code? <form id="form1" name="form1" method="post" action="prcuded.php"> <div> <div ><label>Title<span style="color:red;"> * </span></label><span class=""> <select id="custitle" name="custitle" value=<?php echo $rE['cust_title'];?>" style="height:30px;width:188px"> <?php echo "<option value=''>$title</option>"; ?> <option value=""> </option> <option value="MS.">MS.</option> <option value="Mrs.">Mrs</option> <option value="Mr.">Mr</option> <option value="Miss.">Miss</option> <option value="Master.">Master</option> </select><label>Title</label></span> </form> Quote Link to comment https://forums.phpfreaks.com/topic/297660-php-dropdown-display-and-edition/ Share on other sites More sharing options...
Ch0cu3r Posted August 6, 2015 Share Posted August 6, 2015 (edited) I assume you are using this echo "<option value=''>$title</option>"; to have the users title selected by default. Although you may think this works what is infact happening is because you have used an empty value='' attribute in that option tag it will be overriding the title value to null when the form is submitted. Thus why the title is being changed to null in the database This is not the correct way of setting a default value for a drop down. What you should do apply the selected attribute to the option tag where the title matches the value in the database. The following is is how I would of generated the title options (I assume $rE['cust_title'] contains the users title returned by your query). <select id="custitle" name="custitle" style="height:30px;width:188px"> <?php // define each title in an array $titles = array('', 'MS', 'Mrs', 'Mr', 'Miss', 'Master'); // loop over the array of titles foreach($titles as $title) { // if the current title matches the title in the database then apply the selected attribute to the option // this will have that option selected by default when the form loads $selected = (isset($rE['cust_title']) && $rE['cust_title'] == $title) ? ' selected="selected"' : ''; // generate the html option tag dynamically echo '<option'.$selected.'>'.$title.'</option>'; } ?> </select> Edited August 6, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/297660-php-dropdown-display-and-edition/#findComment-1518138 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.