表单需要在PHP中处理两个提交按钮
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>
在我的HTML中,我有一个用户可以注册或登录的表单。问题是注册和登录按钮都在 相同的表单,它们由两个脚本处理:register.php和login.php。 p>
在 p>
&lt; form action =“”&gt;
code> pre>
可能只有一个脚本名称。 那么如何使表单能够处理两个提交按钮(两个脚本)? p>
&lt; form action =“register.php”method =“post”class =“login_form “&GT;
&lt; div class =“login_form_input_container”&gt;
&lt; label for =“email”class =“login_form_label”&gt;电子邮件&lt; / label&gt;
&lt; input type =“email”id =“email”name =“email”class =“login_form_input”&gt; \ n&lt; / div&gt;
&lt; div class =“login_form_input_container”&gt;
&lt; label for =“password”class =“login_form_label”&gt;密码&lt; / label&gt;
&lt; input type =“password”id =“password”name =“password”class =“login_form_input”&gt; \ n&lt; / div&gt;
&lt; a href =“forgot_password /”class =“forgot_password_link”&gt;忘记密码?&lt; / a&gt;
&lt; input type =“submit”id =“login_submit “name =”login_submit“value =”登录“class =”login_form_submit“&gt;
&lt; input type =”submit“id =”register_submit“name =”register_submit“value =”Register“class =”login_form_submit “&gt;
&lt; / form&gt;
code> pre>
div>
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
.
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.