473,395 Members | 1,622 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,395 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 7000
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.