PHP submit form action from dropdown selection

Thread Solved

Join Date: Jul 2009
Posts: 4
Reputation: kdhare is an unknown quantity at this point 
Solved Threads: 0
kdhare kdhare is offline Offline
Newbie Poster

PHP submit form action from dropdown selection

 
0
  #1
Jul 23rd, 2009
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!




  1. <?php
  2.  
  3. $sql = "SELECT prodMFG FROM productDetail WHERE prodMFG != '' GROUP BY prodMFG";
  4. $result = mysql_query($sql);
  5. echo "<form action='{$GLOBALS['baseurl']}/search.html?=$prodMFG' method='POST'>
  6. <select name='category' value=''></option>";
  7.  
  8. while($nt=mysql_fetch_array($result))
  9.  
  10. {
  11. echo "<option value=$nt[prodMFG]>$nt[prodMFG]</option>";
  12. }
  13.  
  14. echo "</select>";
  15. ?>
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 21
Reputation: spthorn is an unknown quantity at this point 
Solved Threads: 5
spthorn's Avatar
spthorn spthorn is offline Offline
Newbie Poster

Re: PHP submit form action from dropdown selection

 
0
  #2
Jul 24th, 2009
Hmmm. That's some kinda odd code there. Let's do a quick rewrite:
  1. $sql = "SELECT prodMFG FROM productDetail WHERE prodMFG != '' GROUP BY prodMFG"; // (1)
  2. $result = mysql_query($sql);
  3. echo "<form action='$GLOBALS[baseurl]/search.html' method='POST'>"; // (2)
  4. echo "<select name='category'>"; // (3)
  5. while($nt=mysql_fetch_array($result))
  6. {
  7. echo "<option value=$nt[prodMFG]>$nt[prodMFG]</option>";
  8. }
  9. 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.
All opinions 100% correct. Or your money back.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 172
Reputation: Menster is an unknown quantity at this point 
Solved Threads: 22
Menster's Avatar
Menster Menster is offline Offline
Junior Poster

Re: PHP submit form action from dropdown selection

 
0
  #3
Jul 24th, 2009
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 :
  1. $sql = "SELECT prodMFG FROM productDetail WHERE prodMFG != '' GROUP BY prodMFG"; // (1)
  2. $result = mysql_query($sql);
  3. echo "<form action='' id='myForm'method='POST'>"; // (2)
  4. echo "<select name='category' onchange='setFormAction(this)'>"; // (3)
  5. while($nt=mysql_fetch_array($result))
  6. {
  7. echo "<option value=$nt[prodMFG]>$nt[prodMFG]</option>";
  8. }
  9. echo "</select>";
And now the javascript to manage the onchange event:
  1. <script>
  2. function setFormAction(object)
  3. {
  4. //Get the selected value
  5. var get_param = object.options[object.oprions.selectedIndex].value;
  6. var form = document.getElementById('myForm');
  7. //set the form's attribute
  8. form.action = 'search.html?var='+get_param+'';
  9. }
  10. </script>
Last edited by Menster; Jul 24th, 2009 at 8:50 am.
$me = new Person();
if (isset($_COOKIE)){
$me->eat($_COOKIE);
} else { $me->starve(); }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: kdhare is an unknown quantity at this point 
Solved Threads: 0
kdhare kdhare is offline Offline
Newbie Poster

Re: PHP submit form action from dropdown selection

 
0
  #4
Jul 24th, 2009
Originally Posted by spthorn View Post
Hmmm. That's some kinda odd code there. Let's do a quick rewrite:
  1. $sql = "SELECT prodMFG FROM productDetail WHERE prodMFG != '' GROUP BY prodMFG"; // (1)
  2. $result = mysql_query($sql);
  3. echo "<form action='$GLOBALS[baseurl]/search.html' method='POST'>"; // (2)
  4. echo "<select name='category'>"; // (3)
  5. while($nt=mysql_fetch_array($result))
  6. {
  7. echo "<option value=$nt[prodMFG]>$nt[prodMFG]</option>";
  8. }
  9. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: kdhare is an unknown quantity at this point 
Solved Threads: 0
kdhare kdhare is offline Offline
Newbie Poster

Re: PHP submit form action from dropdown selection

 
0
  #5
Jul 24th, 2009
Thanks Menster!. That just made the page reload itself for some reason.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: kdhare is an unknown quantity at this point 
Solved Threads: 0
kdhare kdhare is offline Offline
Newbie Poster

Re: PHP submit form action from dropdown selection

 
0
  #6
Jul 24th, 2009
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...

  1. <?php
  2.  
  3. $sql = "SELECT DISTINCT prodMFG FROM productDetail WHERE prodMFG != '' ORDER BY prodMFG";
  4. $result = mysql_query($sql);
  5. echo "<form action='$GLOBALS[baseurl]/search.html' method='POST'>";
  6. echo "<select name='q'>";
  7. while($nt=mysql_fetch_array($result))
  8. {
  9. echo "<option value=$nt[prodMFG]>$nt[prodMFG]</option>";
  10. }
  11. echo "</select>";
  12.  
  13. ?>
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).
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC