Jump to content

How to handle different form submit actions on the same page?


greenace92

Recommended Posts

I had this before but unfortunately I lost a lot of code. After hunting through my various online accounts and looking at prior posts, I was not able to find the one that I needed.

 

So let's say there are two forms, a login and a register, the two submit button values are:

<form name="form1" action="login">
    <input type="submit" name="submit1" value="login">
</form>

<form name="form2" action="register">
    <input type="submit" name="submit2" value="register">
</form>

I have some idea of what it's supposed to be, it's an action catch or something inside the POST on the same page... something like:

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){
    If(POST['action'] == "login") {
        // read login input values, perform login
    }
    else if(POST['action'] == "register") {
        // read register input values, perform registration
    }
}

?>

I may vaguely recall the use of && and/or || not sure.

 

I'd appreciate any help.

Link to comment
Share on other sites

The action form attribute specifies the URL of the target script. When you use two different actions, that means your forms will be sending their data to two entirely different scripts, not a single script.

 

If you want a single script, you need to put that into the attribute. Then you can check for the value of the submit button:

if (isset($_POST['login']))
{
    ...
}
elseif (isset($_POST['register']))
{
    ...
}

That's not exactly a good solution, though, because now the business logic depends on the label of your submit buttons.

 

Alternatively, use a hidden field which you may actually call action. Then your code above works as intended.

Edited by Jacques1
Link to comment
Share on other sites

Thanks Jacques1

 

I could have simply redirected to a different folder eg. /login vs /register... but with jQuery I hoped to just keep the user on the same page, by making fields appear/disappear.

 

I don't understand what you mean by the "...now the business logic depends on the label of your submit buttons." Why is this not a good solution?

 

Maybe it really doesn't matter(noticeable) if I redirect to new page that looks exactly like the last one with an extra field for email and a different submit button value.

 

Ohhh man, having an obsession moment... just the other day I learned/found out about CSS sprites to save bandwith.

 

I'm working on media queries right now and see that I could just use one style sheet.

 

Similarly with this login/register, the register form only has one additional email field.

 

So I figure why have four things when I can have two... but at the same time the additional headache...

 

For instance there is a Terms of Service button at the bottom of the form, and I've created a "last state" javascript variable that keeps track of the last window that was open, so that when you click on the ToS button, then click on the ToS button again, you return back to what form you were last looking at. I'm having problems with this too. My last state variable is getting stuck in certain combinations of clicks.

So to just use two separate directories for login and register would clear all of that headache, but the extra code, but then it's like how often do people register/login as opposed to just using the site... I don't know...

Edited by greenace92
Link to comment
Share on other sites

Don't try to solve ten problems at once, and don't get lost in micro-optimizations.

 

Registering and logging in are two entirely different actions, even if the GUI may look similar at the moment (this can change when you start collecting more data during the registration). So it makes sense to have two separate scripts instead of one complex script with a lot of if-then-else logic.

 

Shared functionalities can be put into functions, shared GUI elements can be managed with a template engine. However, you can worry about that later. Start with the basic structure and then improve it step by step.

 

 

 

I don't understand what you mean by the "...now the business logic depends on the label of your submit buttons." Why is this not a good solution?

 

Because a button label is a graphical feature which should have no effect at all on the underlying functionality. It would be insane if the entire application breaks just because you've changed the label from, say, “register” to “Sign up now!”.

 

If you insist on sending the two forms to the same script, either use a hidden action field or replace the submit element with a button element (which allows you to separate the label from the value).

Edited by Jacques1
Link to comment
Share on other sites

Because a button label is a graphical feature which should have no effect at all on the underlying functionality. It would be insane if the entire application breaks just because you've changed the label from, say, “register” to “Sign up now!”.

 

I see what you mean, that makes sense.

 

Thanks for your help.

Link to comment
Share on other sites

Just my $.02 here. As a general practice I name all of my submit buttons as "name='btn'". All my scripts then do this:

 

if (!isset($_POST['btn'])
{
handle the first time into your script here, send your output html and exit.
}
$btn = $_POST['btn'];
if ($btn == 'Return')
{
handle my return (to previous caller) button
header()
exit()
}
if ($btn == 'Do This')
{
}
if ($btn == 'Do That')
{
}
// handle unexpected input.
echo "Unknown button submitted $btn";
exit();

 

Obviously, each button has a different label on it and that is how my script knows what to do.

Edited by ginerjm
Link to comment
Share on other sites

As a general practice I name all of my submit buttons as "name='btn'".

 

As Jaques1 has said, depending on the name of a button to be submitted is a bad idea. It wont always be and it will completely fail under certain circumstances. Always use

 

 if ($_SERVER['REQUEST_METHOD'] == 'POST')

 

along with the hidden input if needed. 

Edited by benanamen
Link to comment
Share on other sites

Actually I don't know how my logic can fail since I ALWAYS name the buttons the same and then use the same logic to handle them, as I showed. And as for the check for request_method - if my search for a $_POST element fails then my code fails, as it should if it doesn't find a defined button.

Link to comment
Share on other sites

For starters try running your code in IE8 and submit using the enter button. It will completely fail.

 

Another example, If I was to submit your form using cURL, I have no idea that it depends on me also sending a button name to work so it will fail. The field names would be obvious.

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.