| | |
PHP submit form action from dropdown selection
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 4
Reputation:
Solved Threads: 0
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 Syntax (Toggle Plain Text)
<?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>"; ?>
Last edited by peter_budo; Jul 24th, 2009 at 3:38 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Hmmm. That's some kinda odd code there. Let's do a quick rewrite:
(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.
PHP Syntax (Toggle Plain Text)
$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>";
(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.
All opinions 100% correct. Or your money back.
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 :
And now the javascript to manage the onchange event:
php Syntax (Toggle Plain Text)
$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>";
javascript Syntax (Toggle Plain Text)
<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>
Last edited by Menster; Jul 24th, 2009 at 8:50 am.
$me = new Person();
if (isset($_COOKIE)){
$me->eat($_COOKIE);
} else { $me->starve(); }
if (isset($_COOKIE)){
$me->eat($_COOKIE);
} else { $me->starve(); }
•
•
Join Date: Jul 2009
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
Hmmm. That's some kinda odd code there. Let's do a quick rewrite:
(1) You can't use the GROUP BY unless there's an aggregation in the SELECT. Perhaps you mean ORDER BY?PHP Syntax (Toggle Plain Text)
$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>";
(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.
•
•
Join Date: Jul 2009
Posts: 4
Reputation:
Solved Threads: 0
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...
Here's the final...
php Syntax (Toggle Plain Text)
<?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>"; ?>
Last edited by peter_budo; Jul 24th, 2009 at 3:39 pm. Reason: Keep It Organized - Do not flood the forum by posting the same question more than once (ie in multiple forums).
![]() |
Similar Threads
- Form without action "imitate" of other Form (which do have action) ??? (JavaScript / DHTML / AJAX)
- Alert box after success submit form validate (PHP)
- Rewriting .htaccess from a php file or form (PHP)
- Change Form action with Submit button? (PHP)
- PHP Dynamic Form HELP! (PHP)
- php results from form - stuck (PHP)
- php mail form - need to redirect to new page (PHP)
- dreamweaver, PHP and submit form query (PHP)
Other Threads in the PHP Forum
- Previous Thread: PHP Rapid Development
- Next Thread: Out Source PHP
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax alerts apache api array beginner binary broken cakephp checkbox class cms code convert cron curl database date directory display download dynamic echo email error file files folder form forms function functions google hack href htaccess html htmlspecialchars image include insert integration ip java javascript joomla limit link login loop mail menu methods mlm mod_rewrite multiple mysql network object oop overwrite parse paypal pdf php problem query radio random recursion redirect regex remote script search securephp server sessions sms soap source space sql structure syntax system table tutorial update upload url validation validator variable video web xml youtube





