0
votes

In my HTML I have a form where users can register or login.The problem is both Register and Login buttons are in the same form, and they are handled by two scripts: register.php and login.php.

In

<form action=" "> 

there could just be one script name. So how to make the form able to handle two submit buttons(two scripts)?

  <form action="register.php" method="post" class="login_form">     

    <div class="login_form_input_container">   
      <label for="email" class="login_form_label">Email</label>
      <input type="email" id="email" name="email" class="login_form_input">
    </div>

    <div class="login_form_input_container">  
      <label for="password" class="login_form_label">Password</label>
      <input type="password" id="password" name="password" class="login_form_input">
    </div>

    <a href="forgot_password/" class="forgot_password_link">Forgot password?</a>

    <input type="submit" id="login_submit" name="login_submit" value="Log In" class="login_form_submit">

    <input type="submit" id="register_submit" name="register_submit" value="Register" class="login_form_submit">

  </form>
2
if (isset($_POST['btn_name']))u_mulder
Why do you need one script to handle both?Blender
@Blender No he wants a form that can submit to varying scripts.TwilightSun
@TwilightSun: Then you use two forms.Blender
@Blender Are you reading his question? He doesn't want to make two forms.TwilightSun

2 Answers

2
votes

Create another script register_or_login.php, let that script handle the form submission and make it route to either the login or register backend script:

if (isset($_POST['login_submit'])) {
    require 'login.php';
} elseif (isset($_POST['register_submit'])) {
    require 'register.php';
}

A button is not much different from, say, a checkbox or textfield; if you give it a name it will be present in $_POST.

0
votes

you can just check with isset which one was sent/clicked like so

if(isset($_POST['login_submit'])){
    //do login
}elseif(isset($_POST['register_submit'])){
    //do register
}

I just thought of this and checked that if a user just hits enter without clicking a submit button it sends the first submit name/value as a default. So in your case login_submit will be sent if user just hits enter which is probably a good thing.