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?

1
  • did it worked ? Commented Dec 9, 2017 at 15:06

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

i am using an achor tag in php and passing the 'act' and 'i' in it.
Then it's pretty much the same, 1 sec I'll update my answer.
echo "<a href='?act=delet & na=$n & i=$i'>"."delete".'</a>
echo "<a href='?act=delet & na=$n & i=$i' onclick=\"if (!confirm('Are you sure you want to delete this item?')) return false;\">delete</a>";
it didn't work dear.
|
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);
?>

23 Comments

Soory, i pasted the other code, i am new here so i didnt know how to ask paste the required code.
it okay i understood by reading description & heading . i really got confused "what is with the PHP code" ,though
i want to delete a data from the database using php and javascript confirm. if the user clicks ok the data should be deleted and if he clicks cancel the data should not be deleted. But the error is the whether clicks or cancel the data is deleted.
place your php call in if condition. post your corect code
edit the question cant view clearly
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.