I'm trying to have a form submit to a url with the users selection input from a dropdown. When I submit the form it goes to the search.html page as in the code but is not picking up the prodMFG from the dropdown. Any ideas how to make it work...a javascript or is there something wrong in my code below? Thanks!

<?php 

$sql = "SELECT prodMFG FROM productDetail WHERE prodMFG != '' GROUP BY prodMFG";
$result = mysql_query($sql);
echo "<form action='{$GLOBALS['baseurl']}/search.html?=$prodMFG' method='POST'>
      <select name='category' value=''></option>";

while($nt=mysql_fetch_array($result)) 

{
echo "<option value=$nt[prodMFG]>$nt[prodMFG]</option>";
}

echo "</select>"; 
?>
Member Avatar for spthorn

Hmmm. That's some kinda odd code there. Let's do a quick rewrite:

$sql = "SELECT prodMFG FROM productDetail WHERE prodMFG != '' GROUP BY prodMFG"; // (1)
$result = mysql_query($sql);
echo "<form action='$GLOBALS[baseurl]/search.html' method='POST'>"; // (2)
echo "<select name='category'>"; // (3)
while($nt=mysql_fetch_array($result)) 
{
echo "<option value=$nt[prodMFG]>$nt[prodMFG]</option>";
}
echo "</select>";

(1) You can't use the GROUP BY unless there's an aggregation in the SELECT. Perhaps you mean ORDER BY?
(2) I removed the {} around the variable name, and removed the ?=$prodMFG. The latter is most important. Any input fields within the form are sent with the Action URL, so you don't need to do it manually.
(3) I removed the Value attribute and /Option tag from the opening Select tag.

This will at least set you going in the right direction.

Hi there, in order to set the action of the form in response to a user selecting something from the drop down list, you will need to use javascript, try this :

$sql = "SELECT prodMFG FROM productDetail WHERE prodMFG != '' GROUP BY prodMFG"; // (1)
$result = mysql_query($sql);
echo "<form action='' id='myForm'method='POST'>"; // (2)
echo "<select name='category' onchange='setFormAction(this)'>"; // (3)
while($nt=mysql_fetch_array($result)) 
{
echo "<option value=$nt[prodMFG]>$nt[prodMFG]</option>";
}
echo "</select>";

And now the javascript to manage the onchange event:

<script>
function setFormAction(object)
{
//Get the selected value
    var get_param = object.options[object.oprions.selectedIndex].value;
var form = document.getElementById('myForm');
//set the form's attribute
form.action = 'search.html?var='+get_param+'';
}
</script>

Hmmm. That's some kinda odd code there. Let's do a quick rewrite:

$sql = "SELECT prodMFG FROM productDetail WHERE prodMFG != '' GROUP BY prodMFG"; // (1)
$result = mysql_query($sql);
echo "<form action='$GLOBALS[baseurl]/search.html' method='POST'>"; // (2)
echo "<select name='category'>"; // (3)
while($nt=mysql_fetch_array($result)) 
{
echo "<option value=$nt[prodMFG]>$nt[prodMFG]</option>";
}
echo "</select>";

(1) You can't use the GROUP BY unless there's an aggregation in the SELECT. Perhaps you mean ORDER BY?
(2) I removed the {} around the variable name, and removed the ?=$prodMFG. The latter is most important. Any input fields within the form are sent with the Action URL, so you don't need to do it manually.
(3) I removed the Value attribute and /Option tag from the opening Select tag.

This will at least set you going in the right direction.

Hi SP.

I cleaned up the code as suggested.
RE: (1)
if I use ORDER BY alone it repeats the vendor in the list everytime a vendor has a product..just changed that to SELECT DISTINCT.
(2)I need the choice the user selects from the dropdown to get placed into the action URL such as search.html?sony.

That's what I get get going.

Thanks Menster!. That just made the page reload itself for some reason.

Hey SP! I used your code and it was putting select name "content" into the URL. I changed it to "q" & it works like a charm now! Thanks!!

Here's the final...

<?php 

$sql = "SELECT DISTINCT prodMFG FROM productDetail WHERE prodMFG != '' ORDER BY prodMFG"; 
$result = mysql_query($sql);
echo "<form action='$GLOBALS[baseurl]/search.html' method='POST'>"; 
echo "<select name='q'>"; 
while($nt=mysql_fetch_array($result)) 
{
echo "<option value=$nt[prodMFG]>$nt[prodMFG]</option>";
}
echo "</select>"; 

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.