Drop down not finding the right selection

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
utahfriend
Forum Commoner
Posts: 34
Joined: Thu Nov 10, 2005 12:25 pm
Location: Bountiful, Utah

Drop down not finding the right selection

Post 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>";  ?>
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post 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!
utahfriend
Forum Commoner
Posts: 34
Joined: Thu Nov 10, 2005 12:25 pm
Location: Bountiful, Utah

Post by utahfriend »

I tried both ways and it still does not work
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

where does $co_name_font come from?
utahfriend
Forum Commoner
Posts: 34
Joined: Thu Nov 10, 2005 12:25 pm
Location: Bountiful, Utah

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
utahfriend
Forum Commoner
Posts: 34
Joined: Thu Nov 10, 2005 12:25 pm
Location: Bountiful, Utah

Post 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());
highonsnow
Forum Newbie
Posts: 5
Joined: Sat Feb 04, 2006 1:59 pm

Post 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 ;)
Post Reply