Geoff
Geoff

Reputation: 9

How to use multiple buttons in a single form with PHP?

I'm trying to make a form that allows me to NAVIGATE/INSERT/MODIFY/DELETE entries in a database.

To apply the corresponding queries (insert, modify and delete) I would like to use simple HTML buttons at the bottom of the form.

My question is would it be possible to have each button calling a different query? Any suggestion on how to do this in a simple way as I'm a beginner.

Upvotes: 1

Views: 1285

Answers (2)

SubjectCurio
SubjectCurio

Reputation: 4872

You could do it like this:

PHP

$do_action = null;
if (isset($_POST['insert'])) {
    // Do Insert
    $do_action = 'Insert';
} elseif (isset($_POST['modify'])) {
    // Do Modify
    $do_action = 'Modify';
} elseif (isset($_POST['delete'])) {
    // Do Delete
    $do_action = 'Delete';
} elseif (isset($_POST['navigate'])) {
    // Do Navigate
    $do_action = 'Navigation';
}

if ($do_action != null) {
    echo 'The last action performed was : <strong>' . $do_action . '</strong>';
    // After performing the action, you should redirect the user, so they cannot Refresh the page/press F5 to re-submit the form by mistake
}

HTML

<form method="POST" action="">
    <input type="submit" name="insert" value="Insert">
    <input type="submit" name="modify" value="Modify">
    <input type="submit" name="delete" value="Delete">
    <input type="submit" name="navigate" value="Navigate">
</form>

As I've commented, after you've performed your query, make sure to redirect your user somewhere sensible so they cannot accidentally Refresh the page and submit the same query again.

Upvotes: 2

sameer kumar
sameer kumar

Reputation: 149

Try this

    <?php
if(isset($_POST['submit1']))
{
//INSERT QUERY 
}
if(isset($_POST['submit2']))
{
//DELETE QUERY 
}
if(isset($_POST['submit3']))
{
//UPDATE QUERY 
}
?>

Upvotes: 0

Related Questions