3

I have been searching this stuff but could not find anywhere. when i search about button name and value, everywhere its mentioned about input type=button

But i am looking for button type=submit instead of input type=button i want to allow users to remove their comments. So, the delete button is just showing like a link

The Html is

<button class="likeslink" type="submit" name="delcom" value="<?php echo $comid ?>"><small>Delete</small></button>

I am not getting how can i check if this button is clicked or not not using php as i tried

<?php
if(isset('delcom'))
{
$sql = "DELETE FROM comments WHERE id = :id";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":id", $comid);
$stmt->execute();
}
?>

But its not working.

Meanwhile, i want a popup alert message that confirms Delete Query includings Delete or Cancel buttons on it

I don't want that simple javascript alert on the top of the page. I want some popup window in the middle of the screen with my own Color scheme

Please help

Atif Sheikh
  • 65
  • 2
  • 9

2 Answers2

3

Make sure to get it from the POST variable to check. This is probably what you're actually intending on doing.

<?php
    if(isset($_POST['delcom']))
    {
        $sql = "DELETE FROM comments WHERE id = :id";
        $stmt = $DB->prepare($sql);
        $stmt->bindValue(":id", $comid);
        $stmt->execute();
    }
?>
jkeys
  • 431
  • 3
  • 10
1

Basically, just create a simple form to go with your submit button, then use $_GET[] or $_POST[] to get the value:

<form method="POST" action="path/to/php">
  <input type="hidden" name="dodelete" value="1" />
  <button type="submit">Delete</button>
</form>

And the PHP:

if (isset($_POST['dodelete'])) {
  // do something
}

You could also just use a normal link:

<a href="path/to/php?dodelete=1">Delete</a>

and then just replace the $_POST with a $_GET in the PHP. You could also use a <button> instead of an anchor, but you'd have to add an onClick event to it.

Also, just to let you know, the following sets are the same:

  • <input type="button" value="ABC" /> and <button tyoe="button">ABC</button>
  • <input type="submit" value="ABC" /> and <button type="submit">ABC</button>
  • <input type="reset" value="ABC" /> and <button type="reset">ABC</button>
samanime
  • 25,408
  • 15
  • 90
  • 139
  • So, i must use form with post method to get the value of that button on the same page. Is not there any way to get the value of delete button without using form? – Atif Sheikh Jun 20 '17 at 19:11
  • You need to use a form. The form is what gives you data. The button is only a way to tell the form to send it's data. – samanime Jun 20 '17 at 19:15
  • You are right, i was trying that button to send its value without using form and it was not working. well, Do you have the ans of my 2nd question about popup alert to reconfirm if user wants to delete comment or not. – Atif Sheikh Jun 21 '17 at 10:13