Page 1 of 1

Setting value of SELECT

Posted: Thu Apr 29, 2010 4:03 am
by steven.cottom
I am trying to create a form where the user can update a full row from a MySql db.

I want text areas and select lists to show the correct value from the database.

I am trying to do this by using value="'.$row['*****'].'"

This works on the input field but not on the select field, when the page loads it shows the top entry in the list. Can anyone explain what i am doing wrong?

Thanks

Code: Select all

<?php

mysql_connect ("localhost", "MyUserbame", "MyPassword") or die (mysql_error());
mysql_select_db ("enquiries") or die (mysql_error());

$enterid = $_POST['enterid']; 

$sql = mysql_query("select * FROM enquiry WHERE id LIKE '$enterid'");

echo "You are about to update record number '<strong>$enterid</strong>'<br />";
echo "<br />";
print ('<table style="border-width: thin; border-style: solid; border-color: grey; cellpadding=1; border-width: 2px;">');
print '<tr bgcolor="grey">';
print '<td width="3%"><t><strong>ID</strong></t></td>';
print '<td width="9%"><t><strong>ENTRY DATE</strong></t></td>';
print '<td width="4%"><t><strong>TITLE</strong></t></td>';
print '<td width="9%"><t><strong>FIRST NAME</strong></t></td>';
print '<td width="8%"><t><strong>SURNAME</strong></t></td>';
print '<td width="8%"><t><strong>DOB</strong></t></td>';

print '</tr>';


while ($row = mysql_fetch_array( $sql ))
{
	print '<tr bgcolor="lightgrey">';
	print '<td width="3%"><t>'.$row['id'].'</t></td>';
	print '<td width="9%"><t>'.$row['date'].'</t></td>';
	print '<td width="4%"><t><select name="title" id="title" tabindex="5" value="'.$row['title'].'">
		        <option value="Mr">Mr</option>
                <option value="Mrs">Mrs</option>
                <option value="Miss">Miss</option>
                <option value="Ms">Ms</option>
                </select></t></td>';
	print '<td width="9%"><t><input name="firstname" type="text" value="'.$row['firstname'].'" /></t></td>';
	print '<td width="8%"><t>'.$row['surname'].'</t></td>';
	print '<td width="8%"><t>'.$row['dob'].'</t></td>';
	

Re: Setting value of SELECT

Posted: Thu Apr 29, 2010 7:12 am
by social_experiment
Are you refering to this select list :

Code: Select all

<?php print '<td width="4%"><t><select name="title" id="title" tabindex="5" value="'.$row['title'].'">
                        <option value="Mr">Mr</option>
                <option value="Mrs">Mrs</option>
                <option value="Miss">Miss</option>
                <option value="Ms">Ms</option>
                </select></t></td>'; ?>
You have to change your code to the following :

Code: Select all

<?php  print '<td width="4%"><t><select name="title[]" id="title" tabindex="5" value="'.$row['title'].'">
                        <option value="Mr">Mr</option>
                <option value="Mrs">Mrs</option>
                <option value="Miss">Miss</option>
                <option value="Ms">Ms</option>
                </select></t></td>'; ?>
Now you have an array called '$_POST['title']. In the code where you update the query you check if any values
have been placed into the array :

Code: Select all

<?php
if(is_array($_POST['title'])) {
 foreach ($_POST['title'] as $value) 
} ?>
The value of '$_POST['title'] will be in the variable '$value'. When you then update your query will have '$value' as the value of 'title'.

Re: Setting value of SELECT

Posted: Thu Apr 29, 2010 10:36 am
by AbraCadaver
They don't need an array. Why would someone select Mr and Mrs? Also, a select doesn't have a value, the options do. If you need to show the current value then you do it with the selected property of the option tag, something like this:

Code: Select all

$titles = array('Mr','Mrs','Miss','Ms');

print '<td width="4%"><select name="title" id="title" tabindex="5">';

foreach ($titles as $title) {
	$selected = ($title == $row['title']) ? ' selected ' : ' ';
	print '<option' . $selected . '>' . $title . '</option>';
}
print '</select></td>';

Re: Setting value of SELECT

Posted: Thu Apr 29, 2010 12:50 pm
by social_experiment
AbraCadaver wrote: They don't need an array.
Just a question regarding the code you wrote, could that also be used where a visitor had to select an option on a form? I normally set the 'name' as an array( <select name="selection[]"> ). It works but i understand what you mean about the multiple selections. So my select menu would look like this :

Code: Select all

<?php echo '<select name="selection">';
 echo "<option value="X">X</option>";
 echo "<option value="Y">Y</option>"; 
 echo '</select>'; ?>
And when i retrieve the answer i would just get it inside '$_POST['selection']', correct?

Re: Setting value of SELECT

Posted: Thu Apr 29, 2010 1:53 pm
by AbraCadaver
Yes. You only need an array if you use <select multiple ...> in which case you must use an array or you will only get the last selection.