473,406 Members | 2,847 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,406 software developers and data experts.

Action depending on drop down

64
Hi

I have a drop down thus

Expand|Select|Wrap|Line Numbers
  1. <form name = frmUtils method = POSTaction = "">
  2. <select name = Do>
  3. <option>Add</option>
  4. <option>Delete</option>
  5. <option>Update</option>
  6. </select>
  7. </form>
I want the form action to change based on the selection ie

if selected Index is Add
action = add.php


if selected Index is Delete
action = delete.php


if selected Updateis Add
action = update.php

Somebody help
Jul 23 '07 #1
17 4094
nathj
938 Expert 512MB
Hi

I have a drop down thus


<form name = frmUtils method = POSTaction = "">
<select name = Do>
<option>Add</option>
<option>Delete</option>
<option>Update</option>
</select>
</form>

I want the form action to change based on the selection ie

if selected Index is Add
action = add.php


if selected Index is Delete
action = delete.php


if selected Updateis Add
action = update.php

Somebody help
Thats sounds tricky. Could you use some javascript on the dropdown onchange event? This could use document.getElementById to write the innerHtml of the form (give the form an id that is the same as the name) and add the appropritae action.

That would be the lines of my first attempt. Have a go and let me know how you get on. I'll be happy to help.

Cheers
nathj
Jul 23 '07 #2
kovik
1,044 Expert 1GB
You should approach the situation differently.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(!empty($_POST['do']))
  3. {
  4.     switch($_POST['do'])
  5.     {
  6.         case 'add':
  7.         case 'update':
  8.         case 'delete':
  9.             header('Location: http://www.mydomain.tld/' . $_POST['do'] . '.php');
  10.     }
  11. }
  12. ?>
  13. <form method ="post" action = "">
  14. <select name ="do">
  15.     <option value="add">Add</option>
  16.     <option value="delete">Delete</option>
  17.     <option value="update">Update</option>
  18. </select>
  19. </form>
Jul 23 '07 #3
nathj
938 Expert 512MB
You should approach the situation differently.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(!empty($_POST['do']))
  3. {
  4.     switch($_POST['do'])
  5.     {
  6.         case 'add':
  7.         case 'update':
  8.         case 'delete':
  9.             header('Location: http://www.mydomain.tld/' . $_POST['do'] . '.php');
  10.     }
  11. }
  12. ?>
  13. <form method ="post" action = "">
  14. <select name ="do">
  15.     <option value="add">Add</option>
  16.     <option value="delete">Delete</option>
  17.     <option value="update">Update</option>
  18. </select>
  19. </form>
I see where you are coming from. Use the same action but check what to do on the receiving page. A much slicker solution.

This isn't even my issue and I found that helpful. Thanks for the tip. I don't know what I was thinking.

I would change the !empty() to isset() in this example though, I find it to be a more comprehensive check, and being an optimist I prefer to check that something is done rather than not done.

All in all a much better, less compicated solution than my original thought. I think I may have got a bit stuck on using a technology just because I can - a rookie mistake.

Cheers
nathj
Jul 24 '07 #4
gnawz
64
these are my files

reports.php
Expand|Select|Wrap|Line Numbers
  1. $Report = $_GET['report'];
  2.  
  3. switch ($Report) 
  4. {
  5.  
  6.     case 'add' :
  7.  
  8.     addfunction();
  9.  
  10.     break;
  11.  
  12.     case 'delete' :
  13.  
  14.     deletefunction();
  15.  
  16.     break;
  17.  
  18.     case 'update' :
  19.  
  20.     updatefunction();
  21.  
  22.     break;
  23. }
  24.  
the other file with my select...

checkrpt.php
Expand|Select|Wrap|Line Numbers
  1. <form name="frmUtils" method="post" action="">
  2.  
  3.   <select name="Do"  id ="Do"onChange="SwapReport();">
  4.         <option value="0">Select view</option>
  5.         <option value="1">Add</option>
  6.         <option value="2">Update</option>
  7.     <option value="2">Delete</option>
  8.       </select>
  9.     </form>
  10.  
  11.  
  12. <script type="text/javascript">
  13.  
  14. function SwapReport()
  15. {
  16.     if (document.frmUtils.Do.selectedIndex == 1)
  17.     {
  18.         document.frmUtils.action = "reports.php?report=add";
  19.     }
  20.     else if (document.frmUtils.Do.selectedIndex == 2)
  21.     {
  22.         document.frmUtils.action = "reports.php?report=update";
  23.     }
  24.     else (document.frmUtils.Do.selectedIndex == 3)
  25.     {
  26.         document.frmUtils.action = "reports.php?report=delete";
  27.     }
  28.  
  29. }
  30. </script>
Help from here. It is not working yet.
Jul 24 '07 #5
code green
1,726 Expert 1GB
I think this is a syntax error. Should be else if.
Expand|Select|Wrap|Line Numbers
  1. else (document.frmUtils.Do.selectedIndex == 3)
Unless you want
Expand|Select|Wrap|Line Numbers
  1. document.frmUtils.action = "reports.php?report=delete";
as a default then remove the condition after else
Expand|Select|Wrap|Line Numbers
  1. else {
  2. document.frmUtils.action = "reports.php?report=delete";
You originally asked to make the form action conditional so why not
<script type="text/javascript">
[HTML]function SwapReport()
{
if (document.frmUtils.Do.selectedIndex == 1)
{
document.frmUtils.action = "add.php";
}
else if (document.frmUtils.Do.selectedIndex == 2)
{
document.frmUtils.action = "update.php";
}
else if(document.frmUtils.Do.selectedIndex == 3)
{
document.frmUtils.action = "delete.php";
}
}
</script>[/HTML]
Jul 24 '07 #6
gnawz
64
Still not working...
I appreciate all the help people
Jul 24 '07 #7
nathj
938 Expert 512MB
Still not working...
I appreciate all the help people
In what way? What exactly is not working?

Do you get any error messages at all?

Cheers
nathj
Jul 25 '07 #8
gnawz
64
It just won't do anything at all.
Jul 25 '07 #9
nathj
938 Expert 512MB
It just won't do anything at all.
Hi gnawz,

Sorry to go on like this but have you turned error reporting on?

This will really help to track down what may be going on.

When you say it won't do anything what do you mean:
1) Does the drop down appear on the page?
2) If so is there data in the drop down?
3) Can you select the data in the drop down?
4) What happens when you tab out of the drop down (or move focus in some other way)? Do you get any error messages?
5) Have you checked the generated source code view using something like FireFox with the web developer add-on, if so what doe sit say for the code in each case?

Perhaps answers to these will shed a bit more light on it, otherwise it's a complete black out from this side.

Cheers
nathj
Jul 25 '07 #10
gnawz
64
The code does not swap any thing n I do not get any errore.

Yes I can select but when I do,nothing happens.

No change in the page and nothing moves.

I'm stuck
Jul 25 '07 #11
kovik
1,044 Expert 1GB
I would change the !empty() to isset() in this example though, I find it to be a more comprehensive check, and being an optimist I prefer to check that something is done rather than not done.
empty() is actually a more comprehensive check, as it does what isset() does as well as check that the data has a value. And, in this case, we have no use for an empty value, therefore empty() is ideal.
Jul 26 '07 #12
kovik
1,044 Expert 1GB
The code does not swap any thing n I do not get any errore.

Yes I can select but when I do,nothing happens.

No change in the page and nothing moves.

I'm stuck
Why is no one asking the obvious question? Where do you define addfunction(), updatefunction(), and deletefunction(), and what is it that they actually do? If nothing is being displayed, it's because of them.
Jul 26 '07 #13
nathj
938 Expert 512MB
empty() is actually a more comprehensive check, as it does what isset() does as well as check that the data has a value. And, in this case, we have no use for an empty value, therefore empty() is ideal.
volectricity,

Thanks for the clarity there. i had misunderstood some documentation I read, obviously reading in a rush as I learn all this stuff.

Cheers
nathj
Jul 26 '07 #14
gnawz
64
My functions are defined in the same page they are called.
Jul 26 '07 #15
kovik
1,044 Expert 1GB
My functions are defined in the same page they are called.
This issue isn't whether or not they are defined, it is what they are doing and what they are meant to output to the screen. If you are getting no output, likely the functions are failing.

Have you tried debugging by putting echo statements in place of the functions to ensure that the switch statement is reaching the correct branches?
Jul 26 '07 #16
Looking back at the code you posted above,

You are POSTing from the form, and then testing the $_GET variable.

You must either have method=POST and use $_POST or amethod=GET and use the $_GET variable.

Regards

Ian
Jul 27 '07 #17
kovik
1,044 Expert 1GB
You are POSTing from the form, and then testing the $_GET variable.
His JavaScript alters the action attribute of the form to include a query string, so that's not the issue.
Jul 27 '07 #18

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

Similar topics

4
by: riteshjain82 | last post by:
Hi, I am maintaining a site which is written in ASP. Now i have to create some new pages. In one page we have a table with many rows. Now, I want to enable or disable(showing and hiding also) 2...
7
by: RobKinney1 | last post by:
The subject line sounds a little funny, let me quickly explain: I have created a custom control using ComboBox. But inside my class, I need to know when the user does NOT click my control. Of...
4
by: Boozel | last post by:
i need to create a drop down menu that contains different values depending on another drop down menu. eg. Countries and regions. so if U.S.A is selected in the first menu it'll show the all the...
6
by: suresh Chowdary | last post by:
Hi All, depending upon text box entry i want to show drop down box with respective values. means if user enters 'ls' in the text box i want to show the values like 'she','he' in dropdown box it...
1
by: ummaria | last post by:
Hi, How can I make a drop down list show data from the database depending on another drop down list that has to be selected before i can select the next one? please help me on this..........
6
by: Arthur | last post by:
Hello. How might it be possible to change where a form action is directed based on a selected option. For example I have this: <FORM METHOD = "post" ACTION = ""> And a drop down such as
2
by: aartu | last post by:
i have two dropdown list box..if i select one value from the drop down list box the corresponding value matching to the first value shud b got in the second drop down list box
1
kaleeswaran
by: kaleeswaran | last post by:
HI! i am working in banking domain.. now i struggled with that displaying more number of rows in data..i have a screen like passing type like if the manager wants to passing the loan sanction...
1
by: boss1 | last post by:
hello everyone, I am having a problem with populating second a drop down list depending on selected value of first drop down list. I have been searching the net for the last couple of days i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.