Nikolay Belobradov
Nikolay Belobradov

Reputation: 11

PHP - if(isset($_POST['button1'')){... if(isset($_POST['button2'])).. not working?

I have this code:

   echo'<form method="POST" action="">';

    echo'Code:<input type="text" name="name">';
    echo'<input type="submit" name="save" value="Save">';

    echo'</form>';

if(isset($_POST['save'])){

    //something
    echo'<form method="POST" action="">';

    echo'Code:<input type="text" name="name">';
    echo'<input type="submit" name="delh" value="Delete">';

    echo'</form>';

    if(isset($_POST['delh'])){
        // Cant show this! :(
        echo "Deleted!";

    }
}

When i press "DELETE", the page reloads and the message "Deleted!" remains hidden.


This is a schedule. The idea is if someone presses the Save button but has already saved an hour it says "You have already saved an hour, do you want to cancel it?". When it clicks "Delete", the hour is deleted from the database.

In the case where the Save button is pressed, but the person has not saved an hour, the delete button is not displayed.

Upvotes: 0

Views: 975

Answers (2)

BritishWerewolf
BritishWerewolf

Reputation: 3968

An alternative method would be something like this.
This makes use of the fact that you can open and close PHP tags anywhere.

<?php if (isset($_POST["delh"])) { ?>
    <p>Deleted!</p>
<?php } ?>
<form method="POST" action="">
    Code:<input type="text" name="name">
    <?php if (isset($_POST["save"])) { ?>
        <input type="submit" name="delh" value="Delete" />
    <?php } elseif (isset($_POST["delh"])) { ?>
        <input type="submit" name="save" value="Save" />
    <?php } ?>
</form>

Upvotes: 0

David
David

Reputation: 218960

This line:

echo "Deleted!";

Can only be reached if both of these conditions are true:

if(isset($_POST['save'])){
    //...
    if(isset($_POST['delh'])){

But the form you're showing contains no element named save. The first condition is false, so the code inside that if block never runs. (It may have been true in a previous request, but not in the request you're making with this form.)

You may have meant to separate those conditions?:

if(isset($_POST['save'])){
    //...
}

if(isset($_POST['delh'])){
    //...
}

Upvotes: 1

Related Questions