Jump to content

One form to multiple PHP ?


fractal5

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.