Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to load a dropdown with php on my html form 1

Status
Not open for further replies.

malibu65k

Programmer
Sep 27, 2004
131
US
I have this code I'm using to load a dropdown on my html form. What am I doing wrong? Should I be putting code in my HTML file for the dropdown box. I did that but this looks like it creates it but I'm not sure what I'm missing. I did a search and found several examples and none of them work.


In my PHP file...

$result = mysql_query("SELECT Makeid, Makename FROM tblMake");

echo '<select name="Make">';

while($row = mysql_fetch_assoc($result))
{
echo '<option value="'.$row['Makeid'].'">'.$row
['Makename'].'</option>';
}

echo '</select>';

Thanks in advance for any assistance.
 
Can you elaborate on what happens?

That looks o.k, how are you calling the PHP file into your html form?

Is your html form in an html file? Or a PHP file?

Are you sure your query is returning the expected results?

Tells ore about how this is set up so we may help you out.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
My HTML file (Form Test.html)...

Code:
<html>
<head>
<link rel="stylesheet"
type="text/css" href="VMMT204Styles.css" />
</head>

<body>

<form action="[URL unfurl="true"]http://localhost/PHPFiles/insert.php"[/URL] method="post">

<h2>New Vehicle Type</h2>
<h4>Enter vehicle type: <input type="text" name="TypeName">
<input type= "submit" name = "Type_submit" value = "Save">
</h4>

<h2>New Shop</h2>
<h4>Enter shop name: <input type="text" name="ShopName"><br/>
Enter shop number: <input type="text" name="ShopNumber">
<input type= "submit" name = "Shop_submit" value = "Save">
</h4>

<h2>New Make</h2>
<h4>Enter vehicle make: <input type="text" name="MakeName">
<input type= "submit" name = "Make_submit" value = "Save">
</h4>

<h2>New Model</h2>
<h4>Select the make:<Select name="Make">
</select>
</H4>

<H4>Enter Vehicle Model: <input type="text" name="ModelName">
<input type= "submit" name = "Make_submit" value = "Save">
</h4>
<BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/>

</form>
</body>
</p>
</html>

My PHP file (insert.php)...

Code:
<?php
mysql_connect("localhost","root","") or die('Could not connect to localhost');

mysql_select_db("VMMT204mc") or die('Could not select to Database');


If(array_key_exists('Type_submit', $_POST))
	{
		$query="SELECT MAX(TypeID) FROM tbltype";
		$result = mysql_query($query);
			while ($row=mysql_fetch_assoc($result))

				{ 
				$TypeID = $row['MAX(TypeID)'] +1;
				}

		mysql_query("INSERT INTO tbltype(TypeID, TypeName) VALUES($TypeID,'$_POST[TypeName]')");
		echo "1 record added";

	}
If(array_key_exists('Shop_submit', $_POST))
	{
		$query="SELECT MAX(ShopID) FROM tblShop";
		$result = mysql_query($query);
			while ($row=mysql_fetch_assoc($result))

				{ 
				$ShopID = $row['MAX(ShopID)'] +1;
				}

		mysql_query("INSERT INTO tblShop(ShopId, ShopName, ShopNo) VALUES($ShopID,'$_POST[ShopName]','$_POST[ShopNumber]')");
		echo "1 record added";

	}
If(array_key_exists('Make_submit', $_POST))
	{
		$query="SELECT MAX(MakeID) FROM tblMake";
		$result = mysql_query($query);
			while ($row=mysql_fetch_assoc($result))

				{ 
				$MakeID = $row['MAX(MakeID)'] +1;
				}

		mysql_query("INSERT INTO tblMake(MakeId, MakeName) VALUES($MakeID,'$_POST[MakeName]')");
		echo "1 record added";

	}
If(array_key_exists('Model_submit', $_POST))
	{
		$query="SELECT MAX(ModelID) FROM tblModel";
		$result = mysql_query($query);
			while ($row=mysql_fetch_assoc($result))

				{ 
				$ModelID = $row['MAX(ModelID)'] +1;
				}

		mysql_query("INSERT INTO tblModel(ModelId,MakeId, ModelName) VALUES($ModelID,'$_POST					[MakeId]','$_POST[ModelName]')");
		echo "1 record added";

	}
If(array_key_exists('Noticetype_submit', $_POST))
	{
		$query="SELECT MAX(NoticetypeID) FROM tblNoticetype";
		$result = mysql_query($query);
			while ($row=mysql_fetch_assoc($result))

				{ 
				$NoticetypeID = $row['MAX(NoticetypeID)']+1;
				}

		mysql_query("INSERT INTO tblNoticetype(NoticetypeId,NoticetypeName) VALUES($NoticetypeID,'$_POST					[NoticetypeName]')");
		echo "1 record added";

	}
If(array_key_exists('Base_submit', $_POST))
	{
		$query="SELECT MAX(BaseID) FROM tblBase";
		$result = mysql_query($query);
			while ($row=mysql_fetch_assoc($result))

				{ 
				$BaseID = $row['MAX(BaseID)'] +1;
				}

		mysql_query("INSERT INTO tblBase(BaseId,BaseName,BaseAbbr) VALUES($BaseID,'$_POST					[BaseName]','$_POST[BaseAbbr]')");
		echo "1 record added";

	}
If(array_key_exists('Rank_submit', $_POST))
	{
		$query="SELECT MAX(RankID) FROM tblRank";
		$result = mysql_query($query);
			while ($row=mysql_fetch_assoc($result))

				{ 
				$RankID = $row['MAX(RankID)'] +1;
				}

		mysql_query("INSERT INTO tblRank(RankId,RankName,RankAbbr) VALUES($RankID,'$_POST					[RankName]','$_POST[RankAbbr]')");
		echo "1 record added";

	}

$result = mysql_query("SELECT Makeid, Makename FROM tblMake");

 
echo '<select name="Make">';
 
while($row = mysql_fetch_assoc($result)) 

  {  

   echo '<option value="'.$row['Makeid'].'">'.$row['Makename'].'</option>';

  }
  
echo '</select>';

?>
 
So you submit the form, lots of queries get done and then you echo out a dropdown.

From your description, I'm going to guess that your dropdonw output at the end is supposed to be showing up in your html form here:

Code:
<h4>Select the make:<Select name="Make">
</select>
</H4>

Lets see if I can explain this: There is no link between your dropdown PHP code, and your actual form. there is no way for it to end up where you want it simply because you think it should.

If you want PHP to create HTML for a form, you need to have the PHP either in the same page or included into it some how.

Sticking PHP code in a file that is called after the form is submitted will do nothing to the form itself. Things don;t happen magically they have a sequence and reason.

IF you want your dropdown to be created for the form, then you need to change your form's file to PHP and stick the dropdown code in the form file:

Code:
<html>
<head>
<link rel="stylesheet"
type="text/css" href="VMMT204Styles.css" />
</head>

<body>

<form action="[URL unfurl="true"]http://localhost/PHPFiles/insert.php"[/URL] method="post">

<h2>New Vehicle Type</h2>
<h4>Enter vehicle type: <input type="text" name="TypeName">
<input type= "submit" name = "Type_submit" value = "Save">
</h4>

<h2>New Shop</h2>
<h4>Enter shop name: <input type="text" name="ShopName"><br/>
Enter shop number: <input type="text" name="ShopNumber">
<input type= "submit" name = "Shop_submit" value = "Save">
</h4>

<h2>New Make</h2>
<h4>Enter vehicle make: <input type="text" name="MakeName">
<input type= "submit" name = "Make_submit" value = "Save">
</h4>

<h2>New Model</h2>
<h4>
[red]<?PHP[/red]
[green]
mysql_connect("localhost","root","") or die('Could not connect to localhost');

mysql_select_db("VMMT204mc") or die('Could not select to Database');

$result = mysql_query("SELECT Makeid, Makename FROM tblMake");

 
echo '<select name="Make">';
 
while($row = mysql_fetch_assoc($result)) 

  {  

   echo '<option value="'.$row['Makeid'].'">'.$row['Makename'].'</option>';

  }
  
echo '</select>';


[/green]
[red]?>[/red]
</h4>

<H4>Enter Vehicle Model: <input type="text" name="ModelName">
<input type= "submit" name = "Make_submit" value = "Save">
</h4>
<BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/>

</form>
</body>
</p>
</html>

Make your form file a PHP file, so that the PHP code can be run and create the dropdown.









----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Ok, I get it. Works!

That means I can place all my textboxes and buttons using "echo" in the PHP file and code to insert just like the dropdown example above?

 
Yes, you can.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Phil thank you, you helped me understand from submission allot better also, as I sorta had the same issue..

MA WarGod

I believe if someone can think it, it can be programmed
 
Glad I could help.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top