2
votes

I create one simple list in PHP where user can add Name, age, emails, etc. I added a delete option too, but I want to add a confirmation message when the user clicks on delete option.

I tried searching Google but I found only jQuery and JavaScript solutions. Is there any way to do this with PHP only?

List.php

<?php

   include('config.php');

   $query1=mysql_query("select id, name, age from addd");

   echo "<table><tr><td>Name</td><td>Age</td><td></td><td></td>";

   while($query2=mysql_fetch_array($query1))
        {
           echo "<tr><td>".$query2['name']."</td>";
           echo "<td>".$query2['age']."</td>";
           echo "<td><a href='edit.php?id=".$query2['id']."'>Edit</a></td>";
           echo "<td><a href='delete.php?id=".$query2['id']."'>x</a></td><tr>";
        }

    </table>
?>

Delete.php

<?php

    include('config.php');
    if(isset($_GET['id']))
      {
        $id=$_GET['id'];
        $query1=mysql_query("delete from addd where id='$id'");
        if($query1)
          {
          header('location:list.php');
          }
      }
?>
7

7 Answers

11
votes

If you want to do this only in PHP, you will need to add "steps" in your script, like:

step1 (show form) -> step2 (ask validation) -> step3 (validate)

To do so, you can use sessions to keep form content, and GET parameter to track the step. Otherwise the simplest solution is to use javascript:

echo "<td><a onClick=\"javascript: return confirm('Please confirm deletion');\" href='delete.php?id=".$query2['id']."'>x</a></td><tr>"; //use double quotes for js inside php!
5
votes

This is u need

while($query2=mysql_fetch_array($query1))
{
     echo "<tr><td>".$query2['name']."</td>";
     echo "<td>".$query2['age']."</td>";
     echo "<td><a href='edit.php?id=".$query2['id']."'>Edit</a></td>";
     echo "<td><a onclick='javascript:confirmationDelete($(this));return false;' href='delete.php?id=".$query2['id']."'>x</a></td><tr>";
}

and create javascript function

function confirmationDelete(anchor)
{
   var conf = confirm('Are you sure want to delete this record?');
   if(conf)
      window.location=anchor.attr("href");
}

believe me it's work :)

1
votes

Here is a variation of above that gives you the Confirmation box and passes a variable from PHP to Javascript and back to PHP.
I used this to select a radio button to delete a file from a list of files.
See OnClick runs function with php $fileName past to Javascript, confirmation asked with file name, and if yes, transfers to href with variables for $_GET

PHP/HTML Code:

<?php
echo "
    <tr>
        <td>
            <input type='radio' name='input_sched_button'  
            onclick='confirmation(".'"'.$fileName.'"'.")' />
        </td>
        <td class='fixed-td-450'><a href='./$namehref'>$fileName</a></td>
        <td class='fixed-td-40'>$extn</td>
        <td class='col3'>$size</td>
        <td class='fixed-td-80'>$modtime</td>
    </tr>
";
?>

Javascript

<script>
    function confirmation(delName){
    var del=confirm("Are you sure you want to delete this record?\n"+delName);
    if (del==true){
        window.location.href="Some_Program.php?delete=y&file="+delName;
    }
    return del;
}
</script>
0
votes

Add an onClick event to trigger the dialog box and javascript:return confirm('are you sure you want to delete this?');

echo "<td><a href='delete.php?id=".$query2['id']."' onClick=\"javascript:return confirm('are you sure you want to delete this?');\">x</a></td><tr>";
0
votes
<script>
function deleletconfig(){

var del=confirm("Are you sure you want to delete this record?");
if (del==true){
   alert ("record deleted")
}
return del;
}
</script>

//add onclick event
onclick="return deleletconfig()"
0
votes

work for me but, change this:

onclick='javascript:confirmationDelete($(this));return false;'

with:

onclick='confirmationDelete(this);return false;'
0
votes

I don't know, if this makes any sence, but I have come up with a solution on my own. It does not work, but I would like to share the idea in here, as it basically is supposed to do the same thing. My solution was to just have php echo the javascript, and then having the code, that is supposed to be executed echoed in javascript, so it would be execuded as normal php on site. this is the site where I got the idea of running the php inside the JS

echo "
    <script>
    if (window.confirm('möchten Sie die Datei wirklich unwiderruflich löschen?')){
        window.alert('<?php
        unlink($allFiles);
        rmdir($fileDir));
    ?>');
    }
    else{
    window.alert('Vorgang abgebrochen');
    }
    </script>
    ";

How I mentioned, it does not work, so I solved it just by not having confirmation.

Would be curious why this solution does not work.