tags:

views:

76

answers:

4

Hey I am very new to Web Programming. I have been learning PHP from the past few days and I am stuck at one thing.

I have a form tag in my code which has two submit buttons to manipulate on the data. Since I can have only one action definition on my form tag, it can lead me to one page only. (Not very sure)

Now depending on the button clicked on the form, I want to load a different page. One way is to check the button clicked in an if-else construct and then use echo '...' in the branches and show as if it is a different page. But it doesn't seem right for some reason. Can some one give me a better solution? Thanks.

+2  A: 

One way is to use Javascript to switch the form's action depending on which control has been clicked. The following example uses the jQuery library:

<form id="theForm" action="foo.php">
...
    <input id="first" type="submit"/>
    <input id="second" type="submit"/>
</form>​

$(document).ready(function() {
    $("#theForm input").click(function(e) {
        e.preventDefault();
        if(e.target.id == 'first') {
            $("#theForm").attr("action", "somePage.php");
        } else {
            $("#theForm").attr("action", "anotherPage.php");
        }
        alert($("#theForm").attr("action"));
        $("#theForm").submit(); 
    });
​});​

Demo here: http://jsfiddle.net/CMEqC/2/

karim79
This will completely break if JavaScript is not available (and there are enough people who have tripped over one to many JS bombs and installed NoScript out there that you shouldn't assume it will be). Doing it with server side branching is the right approach.
David Dorward
@David - This is true, I should have stated that in the answer. My solution is only good if his application *requires* that the user has Javascript.
karim79
This is great! I will be using JS at some point! Thanks! But Right now I have just made two forms for the time being so that I get two actions. Thanks!
Gooner
+1  A: 

But it doesn't seem right for some reason.

That's wrong assumption.
Any other solution would be much worst.

Checking on the server side is the only reliable solution.
However echo in branches isn't necessary. There are a lot other ways.
To use include statement is most obvious one.

Col. Shrapnel
When we speak of the MVC architecture, I fell this isn't right and besides it's a lame way of doing things.
Gooner
@Gooner LOL that's most funny comment I've ever seen. You do not understand what you talking about :)
Col. Shrapnel
Thanks for letting me know :)
Gooner
A: 

to add another solution based on @karim79's, since it's tagged with PHP:

<form id="theForm" action="foo.php">
...
    <input id="first" name="button" value="first" type="submit"/>
    <input id="second" name="button" value="second" type="submit"/>
</form>​

in your foo.php, do something like this:

<?php
$submit = isset($_GET['button']) ? trim($_GET['button']) : '';

if($submit == 'first')
{
   header('Location: somePage.php');
}
else if($submit == 'second')
{
   header('Location: anotherPage.php');
}
?>

Summary: to be able to read on your button (2 submit buttons), you need to add name on each one. To make it simple, just use the same name on both. Then, add different value. Next, you need to know what button is being clicked by checking what value is sent on that particular button.

silent
That's going to discard all the form data when it redirects, but it otherwise identical to the approach the question describes.
David Dorward
A: 

just as a reference... int HTML5 buttons can redefine the form's action,method,type etc. http://w3schools.com/html5/tag_button.asp for me, that's a good way to control a form :)

pleasedontbelong
This answer might be useful in 2030 when browsers which haven't implemented that feature probably won't be common enough to worry about.
David Dorward
i know..that's why i said "as a reference"... i think it's a good feature though
pleasedontbelong