Page 1 of 1

Drop down not finding the right selection

Posted: Fri Feb 03, 2006 8:00 pm
by utahfriend
I created a drop down menu for a program. However, when I select an item in the drop down, even though it it looks like it is changes, the variable returns the first item in the list of choices, no matter what I select. Can anyone see what I am doing wrong?

Code: Select all

<?php

function font($font_selected) { 
	$text = "<option value='Select'>Select</option>"; 
	$sql = "SELECT id, name FROM web_data where type='font' order by name"; 
	$result = mysql_query($sql); 
	while($row = mysql_fetch_array($result)) { 
		$text .= "<option value='$row[name]'"; 
		if ($row["name"] == $font_selected) { 
			$text .= " Selected"; 
		} 
		$text .= "> $row[name]</option>\n"; 
	} 
	$text .= "</select>"; 
	return $text; 
} 

?>

<div align="center">
  <center>
  <table border="1" cellpadding="5" cellspacing="0" "border-collapse: collapse" bordercolor="#C0C0C0" width="700" id="AutoNumber1" style="border-collapse: collapse">
    <tr> 
      <td width="132"><p align="right"><font size="2" face="Verdana">Font:</font></td>
          <?PHP $font_selected = $co_name_font; echo "<td width=\"30\" height=\"19\" colspan=\"1\"><select size=\"1\" name=\"header_font\" style=\"float: left\">
      	  ".font($font_selected)."</select></td>";  ?>

Posted: Fri Feb 03, 2006 8:24 pm
by yum-jelly
{enclose} your array variables or dot them and turn error reporting on so you see what your doing wrong!

Like this...

Code: Select all

function font ( $font_selected )
{
	$text = "<option value='Select'>Select</option>";
 
	$sql = "SELECT id, name FROM web_data WHERE type = 'font' order by name";

	$result = mysql_query ( $sql );

	while ( $row = mysql_fetch_assoc ( $result ) )
	{
		$text .= "<option value='" . $row['name'] . "'";

		if ( $row['name'] == $font_selected )
		{
			$text .= " selected";
		}

		$text .= "> " . $row['name'] . "</option>\n";
	}

	$text .= "</select>";

	return ( $text );  
}


or this....

Code: Select all

function font ( $font_selected )
{
	$text = "<option value='Select'>Select</option>";
 
	$sql = "SELECT id, name FROM web_data WHERE type = 'font' order by name";

	$result = mysql_query ( $sql );

	while ( $row = mysql_fetch_assoc ( $result ) )
	{
		$text .= "<option value='{$row['name']}'";

		if ( $row['name'] == $font_selected )
		{
			$text .= " selected";
		}

		$text .= "> {$row['name']}</option>\n";
	}

	$text .= "</select>";

	return ( $text );  
}
yj!

Posted: Fri Feb 03, 2006 8:42 pm
by utahfriend
I tried both ways and it still does not work

Posted: Fri Feb 03, 2006 10:27 pm
by feyd
where does $co_name_font come from?

Posted: Fri Feb 03, 2006 11:11 pm
by utahfriend
It is pulled from Mysql. If it is the first time the account is added it is blank. It will contain the variable when it is chosen. I have tried to manually enter a font name in Mysql and it shows the correct choice when you first open the window, but when you click save, it reverts to the first choice in the drop down

Posted: Fri Feb 03, 2006 11:16 pm
by feyd
then that is where you should concentrate your debugging. Are you sure the field name (which is different) is being mapped to this variable correctly? Is it inserted correctly?

Posted: Fri Feb 03, 2006 11:30 pm
by utahfriend
This is the code in the next file that pulls the variable from the $_POST and then displays it and then updates the mysql.

It isn't pulling the right info from the $_POST because the echo shows the first link in the drop down.

Code: Select all

$thisco_name_font = $_POST['header_font'];
echo "Company Name Font: $thisco_name_font<br>";

$edit_master = "update webs set co_name_font='$thisco_name_font', co_name_size='$thisco_name_size',co_name_color='$co_name_color',
		co_name_align='$co_name_align',co_name='$co_name',heading_text_font='$heading_text_font',
		heading_text_size='$heading_text_size',heading_text_color='$heading_text_color',
		heading_text_align='$heading_text_align',heading_text='$heading_text',
		link_1='$link_1',link_1_ref='index.html',link_2='$link2',
		link_2_ref='$link2_ref',link_3='$link3',link_3_ref='$link_3_ref',
		link_4='$link4',link_4_ref='$link4_ref',
		link_5='$link5',link_5_ref='$link5_ref',
		link_font='$link_font',link_font_color='$link_font_color',link_size='$link_size',
		link_active='$link_active',link_style='$link_style',link_visited ='$link_visited' 
		
		WHERE `user_id` = '$user_id' and page_name='index'";	

mysql_query($edit_master) or die(mysql_error());

Posted: Sat Feb 04, 2006 2:24 pm
by highonsnow
Hi Ronald,

Should these be exact, as in if I do:

Code: Select all

if ($row['name'] == $font_selected)
{
     $text .= " selected";
}
should I expect:

Code: Select all

if ("SanSerif" == "San Serif")
{
     $text .= " selected";
}
you should debug it by echoing out the values like so:

Code: Select all

echo "$"."row['name'] = " $row['name'] . " -> " . $font_selected . "<br>";
Sure, it'll break your presentation a little bit, but it will give you an idea of exactly what you're comparing against, and you'll see what needs changing without tearing your hair out too much.

Try it out and let us know what you get in return ;)