Page 1 of 1

HTML Form

Posted: Fri Apr 09, 2010 3:49 pm
by rodan
I have an html form for contact data displays a drop down for state. The rows for the states are loaded in another table (state). When I use the following code to insert a new row, it works just fine.

My problem is when I use this to "update" on another window, the state is updated with the first row of data from the state table. How do I get the form variable to display the value that's currently on the contact row?

CODE--

Code: Select all

<td><b>State:</b></td>
<td><select name="state_abbr" >
	<?php
		$statelink = mysql_connect('localhost', 'id', 'pwd');

		//the database that will be used
		mysql_select_db('db') or die('An error as occurred.');
		$statequery = 'SELECT abbr,name FROM state ORDER BY abbr';
		$stateresult = mysql_query($statequery, $statelink);

		while($staterow = mysql_fetch_array($stateresult, MYSQL_ASSOC))
		{
			$statelevel = $staterow['abbr'];
			$statename = $staterow['name'];
			print("<option value=$statelevel>$statelevel - $statename</option>");
		}
	?>
	</select>
</td>
Thanks

Re: HTML Form

Posted: Fri Apr 09, 2010 4:56 pm
by minorDemocritus
Check out viewtopic.php?f=1&t=115134, Eiolon asked a very similar question.

Also, make sure you quote your HTML tag attributes... Something like this would work.

Code: Select all

<td><b>State:</b></td>
<td><select name="state_abbr" >
	<?php
		$statelink = mysql_connect('localhost', 'id', 'pwd');

		//the database that will be used
		mysql_select_db('db') or die('An error as occurred.');
		$statequery = 'SELECT abbr,name FROM state ORDER BY abbr';
		$stateresult = mysql_query($statequery, $statelink);

                // you need to define this
                $whichStateToSelect = 'IL';
                $selectedString = '';
		while($staterow = mysql_fetch_array($stateresult, MYSQL_ASSOC))
		{
			$statelevel = $staterow['abbr'];
			$statename = $staterow['name'];
                        if ($statelevel == $whichStateToSelect) {
                                $selectedString = "selected='selected'"
                        }
			print("<option value='$statelevel' $selectedString>$statelevel - $statename</option>");
		}
	?>
	</select>
</td>

Re: HTML Form

Posted: Sun Apr 11, 2010 9:23 am
by rodan
Thanks minorDemocitrus you put me on the right road. I really appreciate your help and guidance.

The code that works for my update form is as follows:

Code: Select all

<td><b>State:</b></td>
<td><select name="state_abbr">
	<?php
		$statelink = mysql_connect('localhost', 'id', 'pwd');

		//the database that will be used
		mysql_select_db('db') or die('An error as occurred.');
		$statequery = 'SELECT abbr,name FROM state ORDER BY abbr';
		$stateresult = mysql_query($statequery, $statelink);

                while($staterow = mysql_fetch_array($stateresult, MYSQL_ASSOC))
                {
                        $statelevel = $staterow['abbr'];
                        $statename = $staterow['name'];
                       // $state_abbr is the current state value for the row being displayed/updated.
                        if($statelevel == $state_abbr)
                        {
                         print("<option value=$statelevel SELECTED>$statelevel - $statename</option>");
                         }
                         else 
                         {
                         print ("<option value=$statelevel>$statelevel - $statename</option>");

                         }
                }
        ?>

	</select>
</td>
On to the next challenge...

rodan

Re: HTML Form

Posted: Sun Apr 11, 2010 10:47 am
by Benjamin
Please use the appropriate

Code: Select all

 [ /syntax] tags when posting code blocks in the forums. Your code will be syntax highlighted making it much easier for everyone to read. You will most likely receive more answers as well.

If you are new to the forums, please be sure to read:

[list=1][*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]