Setting value of SELECT

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
steven.cottom
Forum Newbie
Posts: 10
Joined: Mon Apr 05, 2010 2:14 pm

Setting value of SELECT

Post 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>';
	
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Setting value of SELECT

Post 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'.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Setting value of SELECT

Post 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>';
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Setting value of SELECT

Post 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?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Setting value of SELECT

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply