fractal5 Posted November 22, 2011 Share Posted November 22, 2011 Is there a way where I can have one form submit to multiple phps depending on which button is clicked? For example if I have: <FORM action="http://localhost/smatrix/phptest/phpcode1.php" name=optionsform METHOD="POST"> <input type="checkbox" name="criteria[]" value="box1"> <input type="checkbox" name="criteria[]" value="box2"> <INPUT TYPE=SUBMIT VALUE="php1" > <INPUT TYPE=SUBMIT VALUE="php2" > </form> Now the other php would be "http://localhost/smatrix/phptest/phpcode2.php" and that is accessed if button "php2" is clicked. Is there any way to do this without using 2 seperate forms? If statements? Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/ Share on other sites More sharing options...
Fadion Posted November 22, 2011 Share Posted November 22, 2011 The form can have only one action attribute and as such, only one PHP script to send the form data. It's not a limitation, but something logical, because such things are not HTML's responsibility. What you can do is use the same processing script (phpcode1.php), but in there add some logic to make different actions depending on which button was pressed. Create a form like the one below first: <form action="http://localhost/smatrix/phptest/phpcode1.php" name="optionsform" method="post"> <input type="checkbox" name="criteria[]" value="box1"> <input type="checkbox" name="criteria[]" value="box2"> <button type="submit" name="button1">First Button</button> <button type="submit" name="button2">Second Button</button> </form> The <button> isn't included in POST if it wasn't clicked, which makes it interesting for your needs. You can see what button was pressed just by checking if it exists in the $_POST superglobal. So, in phpcode1.php you could have: <?php if (isset($_POST['button1'])) { //logic when button 1 is pressed } else { //logic when button 2 is pressed } ?> Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290236 Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 You could use a little javascript. <html> <head> <script> function goSomewhere(dir) { document.getElementById('form').action = dir; document.getElementById('form').submit(); } </script> </head> <body> <form name="test" method="POST" id="form" action="default.php"> <input type="submit" name="default" value="default.php"> <input type="button" name="second" value="Blah.php" onClick="goSomewhere('Blah.php');"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290237 Share on other sites More sharing options...
Fadion Posted November 22, 2011 Share Posted November 22, 2011 Using Javascript without a fallback solution isn't quite a good idea. Problem is there's no fallback solution... Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290241 Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 I used to agree with that, but I think in this day and age, we can expect users to have javascript. We can make this work even in IE6. Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290244 Share on other sites More sharing options...
Fadion Posted November 22, 2011 Share Posted November 22, 2011 I agree with that too, but with progressive enhancement in mind. Sure, Javascript is a standart nowadays, but you can't rely just on it for form submission. Especially if there are other, imho better ways. Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290247 Share on other sites More sharing options...
seany123 Posted November 22, 2011 Share Posted November 22, 2011 GuiltyGears solution is the best to use, its easy and stays using HTML/php which is a bonus. Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290257 Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 The problem with the button solution is that the user can push enter to submit the form. Then neither would be registered. The reality is, if you wan't to be paranoid about making it right, you should make two separate form pages. Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290265 Share on other sites More sharing options...
fractal5 Posted November 22, 2011 Author Share Posted November 22, 2011 Thanks a ton GuiltyGear. I love that idea of putting them in one form and using the php to see which button was pressed. Also makes the code simpler on several other fronts. As for the teynon's Javascript solution, I don't exactly understand what it is doing. However I was using javascript in the beginning and then chose to move things over to php on suggestions that it can be more secure that way. Well this is kind of just the first draft for the webpage so I'm going to overlook the possibility of the user pressing Enter. Although that did not cross my mind and is something I will consider in the future. By two seperate pages, do you mean different phps? Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290267 Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 Two separate forms means FormA.html posts to FormA.php and FormB.html posts to FormB.php Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290269 Share on other sites More sharing options...
fractal5 Posted November 22, 2011 Author Share Posted November 22, 2011 I need to have all of it on one page though, makes it more user-friendly. I'm gonna try GG's suggestion and see if I can get it done. Thanks for your help though. Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290273 Share on other sites More sharing options...
Fadion Posted November 22, 2011 Share Posted November 22, 2011 The problem with the button solution is that the user can push enter to submit the form. Then neither would be registered. The reality is, if you wan't to be paranoid about making it right, you should make two separate form pages. Actually the first button will be added to POST data when the form is submitted, either by pressing the button or just submitting the form via keyboard (pressing enter). I wouldn't have posted that solution if it would have such a big usability issue. The way the browser puts data to POST, by adding the first button and not the second is strange and I'm unable to explain technically, but it gets the job done Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290294 Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 The problem with the button solution is that the user can push enter to submit the form. Then neither would be registered. The reality is, if you wan't to be paranoid about making it right, you should make two separate form pages. Actually the first button will be added to POST data when the form is submitted, either by pressing the button or just submitting the form via keyboard (pressing enter). I wouldn't have posted that solution if it would have such a big usability issue. The way the browser puts data to POST, by adding the first button and not the second is strange and I'm unable to explain technically, but it gets the job done Not in IE. http://tomsfreelance.com/posttest.php Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290298 Share on other sites More sharing options...
Fadion Posted November 22, 2011 Share Posted November 22, 2011 Not in IE. http://tomsfreelance.com/posttest.php I thought IE6 was dead?! In IE7+ it works fine. I would rather not support IE6 than provide a Javascript hack. Form submission would be the smallest problem anyway. PS: You made me run Parallels (virtual machine) just to test that thing. I like debate, but this one is completely pointless. It's a standart solution vs a Javascript solution. I've nothing against JS, because I do use it on a daily basis, but it's just wrong making a standart form submission be relied only on it. Anyway, anyone has his/her own way of thinking. Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290301 Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 The place I work does work for some big companies who still use IE6. I don't like it, but it's not my choice. Besides that, my test was run on IE 9. Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290303 Share on other sites More sharing options...
Fadion Posted November 22, 2011 Share Posted November 22, 2011 The place I work does work for some big companies who still use IE6. I don't like it, but it's not my choice. Besides that, my test was run on IE 9. The ever repeating Internet Explorer 6 debate. Sure, they use IE6 so monolithic, proprietary, expensive and not-updated-since-the-90s web applications can work. I've been there and I know that, but those types of visitors aren't the ones the web expects today. When someone uses a browser that's a decade old (which is technically equal to a millenium), it's certain they don't care much about the web. Should we? From W3Counter statistics, IE6 has a global share of only 2.28%. If I remember right, China is the world leader in IE6 usage and if that's true, it lowers the percentage locally to Europe, USA and other countries. Even if that wasn't true, 2.28% is very low for us to care. As for the tests, I run them successfully in IE7 and IE8. Don't have IE9 right now to test, but I doubt the browser behavior has changed... Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290310 Share on other sites More sharing options...
fractal5 Posted November 22, 2011 Author Share Posted November 22, 2011 Don't mean to interrupt the IE6 convo but I have a new question. Similar enough so I didn't wanna make a new thread for it. I've been able to successfully submit the values to php and have it parse them depending on the button pressed. Now I'm stuck with quite the opposite. I have 2 forms and I need values from both to go to the same php. The first form is already doing it, but I also need to collect the values from the second one and send it to the php being used by the first. Basically both forms contain checkboxes and I need the php to know which checkboxes have been selected from each form. The reason I cannot put the checkboxes in the same form is because I need 1 set of boxes to be on the sidebar, and the second to be on the main frame. Naming the forms the same doesn't work. I even wrote a javascript function which will check which boxes are ticked in the second form and create an identity string accordingly and pass it to the first form, but I can't seem to make this work. I don't think I can pass a javascript string to the body of an html? Any workaround to this? So that I can get values from 2 different forms and submit it to the same php? Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290315 Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 You're going to have to move the form block a few levels out in the hierarchy so that it encloses all of it. You can't blend two forms together. In otherwords, do it like this: <form> <div class="leftBar"> <input stuff> </div> <div class="contentBox"> <input more stuff> </div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290414 Share on other sites More sharing options...
fractal5 Posted November 22, 2011 Author Share Posted November 22, 2011 Can I say I love you? Haha thanks that worked perfectly. I feel dumb for not having thought of that, seems so simple. Quote Link to comment https://forums.phpfreaks.com/topic/251582-one-form-to-multiple-php/#findComment-1290417 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.