Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/297660-php-dropdown-display-and-edition/
Share on other sites

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 by Ch0cu3r
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.