HTML Form

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
rodan
Forum Newbie
Posts: 2
Joined: Thu Apr 01, 2010 9:34 am

HTML Form

Post 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
Last edited by Benjamin on Fri Apr 09, 2010 3:51 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: HTML Form

Post 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>
rodan
Forum Newbie
Posts: 2
Joined: Thu Apr 01, 2010 9:34 am

Re: HTML Form

Post 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
Last edited by rodan on Sun Apr 11, 2010 12:37 pm, edited 2 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: HTML Form

Post 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]
Post Reply