Home > Back-end >  How do you check if a button has been pressed in html/php
How do you check if a button has been pressed in html/php

Time:09-26

I'm trying to build a blog page via HTML and PHP. I have a file called edit.php with two buttons on it, Update and Delete. I want to know how PHP can check to see WHICH button was pressed (Update or delete) so it knows which function to call to either update or delete the post?

i have my html as:

<input type="submit" name="command" value="Update" />
<input type="submit" name="command" value="Delete" onclick="return confirm('Are you sure you wish to delete this post?')" />

and in my php file that processes the posts Create, Delete or Update functions, i have tried using like:

if ($_POST && ($_POST['submit'] == "Delete"))
{
  delete();
}

but I keep getting errors back saying: Warning: Undefined array key "submit"

If anyone can give me some direction on how one PHP file can see a button value from another PHP file, I would appreciate that!

CodePudding user response:

Use isset to check if var exists and has any value other than null. You are checking by input type, instead, Check by input field name.

<input type="submit" name="update" value="Update" />
<input type="submit" name="delete" value="Delete" onclick="return confirm('Are you sure you wish to delete this post?')" />

<?php
    if (isset($_POST['update'])) {
        update();
    }
    elseif (isset($_POST['delete'])) {
        delete();
    }
?>
  • Related