Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - Form to send information to different pages according to which button you push

So I have this form with a dropdown list and two buttons. I want to be able to select something from the dropdown list and send that information to two different pages depending on which button I press.

    <form action="(edit_user.php or create_user.php)" method="POST">
        <select id="dropdown" name="users" size="30">
            <option value="user1">Joe</option>
            <option value="user2">Jane</option>
            <option value="user3">Snuffy</option>
            <option value="user4">Mahabirsinghmatos</option>
        </select>
        <input type="submit" value="Create User" />
        <input type="submit" value="Edit User" />
    </form>

I want to send the form information to one of those two php files according to which button I pressed.

How would I go about doing that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Give the submit buttons names:

<input type="submit" name="task" value="Create User" />
<input type="submit" name="task" value="Edit User" />

Have the PHP script that handles your action determine which file to run it though.

e.g.

if (!isset($_POST['task'])) {
    # Some default action
} elseif ($_POST['task'] == "Edit User") {
    include('edit_user.php');
} elseif ($_POST['task'] == "Create User") {
    include('create_user.php');
} else {
    # Some error state
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...