0
   <?php
 mysql_connect("localhost","root","");
 mysql_select_db("lastasignment"); 
if(isset($_GET['act'])){
 $nid=$_GET['i']; ?> 
<script type="text/javascript">
 if(confirm("you want to delete "+" <?php echo $nid ?>")){ 
<?php $i=$_GET['i']; 
mysql_query("delete from testings where id=$i");
 ?> ;
 }else {
alert("cancelled");
} 
</script>

The data in the table is deleted whether i click ok or cancel?

Cyril de Wit
  • 446
  • 4
  • 12

2 Answers2

0

You are trying to alert the user whether to continue or not. Here is that code

if (confirm('Are you sure you want to do this thing into the database?')) {
    // operation to execute
} else {
    // operation on false
}

call the function by onclick event in edit e.g(onclick="showUser(this.value)")

AJAX:

<script>
function showUser(str) {     
    if (confirm('Are you sure you want to do this thing into the database?')) {
        // operation to execute
        if (str == "") {
            document.getElementById("txtHint").innerHTML="";
            return;
        }

        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else { // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange = function() {
            if (this.readyState==4 && this.status==200) {
              document.getElementById("txtHint").innerHTML = this.responseText;
            }
        }
        xmlhttp.open("GET","linktophp.php?q="+str,true);
        xmlhttp.send();
    } else {
        alert("cancelled");
    }
}
</script>

PHP FILE:

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','****','abc123','my_db');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");
$sql="DELETE FROM user WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
?>
Cyril de Wit
  • 446
  • 4
  • 12
jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22
0

If you are using a form you can do something like that:

<form onsubmit="if (!confirm('Are you sure you want to delete this item?')) return false;">
    <input type="hidden" name="act" value="delete" />
    <button name="i" value="1">Delete</button>
</form>

If you are using an anchor tag, you can use onclick:

<a href="YOUR_URL" onclick="if (!confirm('Are you sure you want to delete this item?')) return false;">Click Me</a>

And BTW, you should use GET for fetching things only, you should not use it for insert/update/delete. Use POST instead. The reason? for example, you don't want the user to access that link from his browser history, then you'll have a dupliacte entery / delete again / update again.

HTMHell
  • 5,761
  • 5
  • 37
  • 79