473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

PHP dynamic dropdown-change form action based on user selection

Hello,

I have a dropdown which populates from a database.
When user selects option 1 from the dropdown, he should be directed to sample.php, if he selects option 2, then to sampleb.php and so on.

The code is as follows:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="JavaScript">
  4.  
  5. function go(formname,url){
  6.  
  7.       document.getElementById(myteam).action = url;
  8.  
  9.       document.getElementById(myteam).submit();
  10.  
  11. }
  12.  
  13. </script>
  14. </head>
  15. <title>Homepage</title>
  16. <?php
  17. //dbconnect is a file which has the connection paramaters
  18. //to the server and the database.
  19. require ("dbconnect.php");
  20.  
  21.  
  22. $sqlquery="SELECT teamqueuename FROM teamqueue;";
  23. $results= mssql_query($sqlquery);
  24. ?>
  25. <body>
  26. <table> 
  27. <tr><td><center>Welcome to the homepage!</center><td></tr>
  28. <tr><td><br></td></tr>
  29. <tr><td>Please select a team queue</td></tr>
  30. <form name="myteam" method="get" target="_blank">
  31.  
  32. <tr><td><select name="myteam1" onChange="call()"> 
  33.  
  34. <?php
  35. while ($row=mssql_fetch_array($results)){
  36.  
  37.         $teamqueuename=$row["teamqueuename"];
  38.  
  39. echo "<option>$teamqueuename</option>";
  40.  
  41.  
  42.  
  43. }
  44. mssql_close($sqlconnect);
  45. ?>
  46. </select></td></tr>
  47. <tr><td><input type="submit" value="submit"></td></tr>
  48.  
  49. </form>
  50. <?php
  51. function call(){
  52. $myteam=$_get['myteam'];
  53. if ($myteam=="Build Team")
  54. {
  55.     go(myteam, "http://sample.php");
  56. }    
  57. if ($myteam=="PPG Team")
  58. {    
  59.     go(myteam, "http://sampleb.php");
  60.  
  61. }
  62. }
  63.     ?>    
  64. </table>
  65. </body>
  66. </html>
[Please use CODE tags when posting source code. Thanks! --pbmods]

Thank you..
Jun 29 '07 #1
3 6991
kovik
1,044 Expert 1GB
Firstly, you can't call a JavaScript function using PHP. Secondly, you don't really need JavaScript for this at all.

Post to the same page and, before anything is displayed, check the posted variable. If it meets the condition for the first page, use header() redirection to send them to the first page, or the second, or third, etc...
Jun 29 '07 #2
Thanks for your reply.
I tried the header redirect..but it doesnt work.
Where am i going wrong??

Code:
<?php

$myteam1=$_post['myteam1'];
if ($myteam1=="Build Team")
{
header("location: newdata.html");
}
if ($myteam1=="PPG Team")

{
header("location: test/newdata.php");
}
?>
<html>
<head>
</head>
<title>Homepage</title>

<?php
//dbconnect is a file which has the connection paramaters
//to the server and the database.

require ("dbconnect.php");

$sqlquery="SELECT teamqueuename FROM teamqueue;";

$results= mssql_query($sqlquery);
$myteam1=$_get['myteam1'];
?>
<body>
<table>
<tr><td><center>Welcome to the homepage!</center><td></tr>
<tr><td><br></td></tr>
<tr><td>Please select a team queue</td></tr>
<form name= "myteam" method="post" target="_blank" action="test1.php">
<tr><td><select name="myteam1">
<?php
while ($row=mssql_fetch_array($results)){
$teamqueuename=$row["teamqueuename"];
echo "<option>$teamqueuename</option>";

}
mssql_close($sqlconnect);
?>
</selec></td></tr>

<tr><td><input type="submit" value="submit"></td></tr>

</form>

</table>
</body>
</html>
Jun 29 '07 #3
kovik
1,044 Expert 1GB
A lot of things. :-p

Firstly, never assume that a variable has been posted. Secondly, the header() function takes absolute paths. I believe it is either a browser issue or a server issue, but not everything recognizes relative paths correctly.

But I can't really help you until you tell me what error messages you are getting.
Jun 30 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: adam | last post by:
Hi - im looking to have two drop down boxs one with makes of car and the second drop down box to automatically populate with all the model of that particular make of car. there are about 50 makes...
6
by: Kingdom | last post by:
I'm using this script to dynamicaly populate 2 dropdowns and dispaly the results. Choose a component type from the first drop down, lets say 'car' and the second box will list all the car...
8
by: Drew | last post by:
I am trying to build a small app that shows a Course Title from the database, then displays a dropdown full of categories for the user to choose one... I thought a loop would be the best way to...
4
by: Niks | last post by:
Hi, I have to create a DataGrid which has 5 columns (only for display not for Edit). Problem is with one column, which displays the database value if present else it has to display DropDownlist in...
0
by: Just Me | last post by:
My Dynamic dropdown menus are appearing above the text below. How can I prevent this, I have tried setting the zorder high for them, but it makes not difference. Cheers
1
by: Steve Richter | last post by:
I am using <div align=centerto center a horizontal menu on the page. Problem is the center align of the div is rippling down to the menu items and causing the text of the dynamic popup sub menu...
0
by: Javilen | last post by:
Hello, Maybe one of you can offer a possible solution to the problem I have below. The request to me is to build a dynamically populating dropdown list from a text box.(something similar to...
2
by: chowdary | last post by:
hi, Iam new to javascript. I'm wondering how to go about creating two dropdown boxes such that when you select a value from the first one, the second is populated with values that depend on the...
3
by: d1156676 | last post by:
Hi I have a Dynamic dropdown reading in data from a MySql database, I need to refresh the dropdown when the database is updated without refreshing the whole page. I have had a go at trying to...
1
by: sydd | last post by:
Hi, I was wondering if it's possible to create a dynamic dropdown menu from this code. if($rs->getNumRows() > 0){ $intProjectCount = 1; $htmlOut ="<table width='100%' border='0'...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.