Is It possible to have two submit buttons in one f

2019-09-06 09:48发布

问题:

Wordpress (Contact form 7)

I have one submit button know, that has a redirect to a page: on_sent_ok: "location = 'http://xxxxxxxxxxx.com/xxxx/?page_id=1';"

Know I wanna know if its possible to have one more submit button in the same form redirecting to another page?

Or Is It possible to do it without using on_sent_ok:?

回答1:

Yes, it is possible, you have to name your button and add a value to it. That will be sent with the form. On the server you can check what button was pressed by checking which button name (and value) is present in the request parameters.

For example:

HTML on the client:

<form action="auth.php" method="POST" enctype="application/x-www-form-urlencoded; charset=utf-8">
    <label for="email">Email address</label>
    <input type="text" name="email" />
    <br />
    <label for="password">Password - at least 5 characters</label>
    <input type="password" name="password">
    <br />
    <button name="subject" type="submit" value="register">Sign up</button>
    <button name="subject" type="submit" value="login">Sign in</button>
</form>

PHP on the server

if ($_POST['subject'] == 'register') {
    //register user
    header('location: registered.php');
}
else if ($_POST['subject'] == 'login') {
    //login user
    header('location: profile.php');
}

note:
If you want to send the same form using completely different actions, then you have to use javascript onsubmit or a non-submit button with onclick to change the form action, or send the data with AJAX.