I am displaying a JavaScript confirmation dialog that asks if the user is sure they wish to delete the record. However, even when the user clicks "yes" the query is still executed and the record is still deleted what can be the problem here is the code:
<script>
function deleletconfig() {
var del=confirm("Are you sure you want to delete this record?");
if (del==true){
alert ("record deleted")
} else {
alert("Record Not Deleted")
}
}
</script>
So even if I click cancel the query/record gets deleted. How can I stop this from happening what am I doing wrong? Still a newbie at JS!:(
You must use the return value of the confirm dialog:
echo"<form method = \"post\" action =\"change.php?change=$productid\">";
echo// form fields here!...
echo"<input type=\"submit\" name = \"delete\" value=\"Delete\" onclick=\"return deleletconfig()\" />";
if (isset($_POST['delete'])){ //delete clicked
//get variables here
//run query delete record from xyz where id=$id
}
<script>
function deleletconfig(){
var del=confirm("Are you sure you want to delete this record?");
if (del==true){
alert ("record deleted")
}else{
alert("Record Not Deleted")
}
return del;
}
</script>
See the changes in "onclick" and "deleletconfig".
Create a function and call it:
<script type="text/javascript">
function confirmation() {
return confirm('Are you sure you want to do this?');
}
</script>
Delete
Just copy and paste
it gives an alert box to confirm whatever you want your link to do.
Alert returns true or false based on selected answer, and then will either do what you've set your link to do or will not do what your link is supposed to do.
<td>
<a class="delete_button" href="#">Delete</a>
</td>
<script type="text/javascript">
$('.delete_button').click(function(e){
var result = confirm("Are you sure you want to delete this user?");
if(!result) {
e.preventDefault();
}
});
</script>
Instead of
if (del==true){
do
if(del) {
add return = true for deletion, return = false for no deletion.
Related
I'm making a forum and I want the user panel to change whenever a user is logged in. So by default it says "Sign in or register." But when a user is logged in it says their name and an option to log out.
The idea is that when they click sign out jQuery will ask for a confirm and if its true they will be redirected to ?logout=true where php handles the rest.
The problem is that jQuery just won't work on the element echoed by php.
Here is the PHP:
<div id="userbar">
<span id="userPanel">User Panel</span>
<?php
if (isset($_SESSION['signedIn'])) {
echo '
Sign Out
<span id="welcome">
Hello, <b><a href="#" class="sessName">' . $_SESSION["username"] . '
</b></a></span>
';
} else {
echo '
Sign In
Register
';
}
?>
</div>
And here is the jQuery:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
$(".logout").click(function(){
var exit = confirm("Are you sure you would like to sing out?");
if (exit == true) {
window.location("header.php?logout=true");
}
return false;
});
</script>
First, you need to update your jquery is too old.
this the final function i create, you can try it.
function alertLogOut(){
var exit = confirm("Are you sure you would like to sing out?");
if (exit == true) {
window.location.href="header.php?logout=true";
}
return false;
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
Sign Out
You may add your listener on the top of your html. Try to make it this way :
$(document).ready(function() {
$(".logout").click(function(){
var exit = confirm("Are you sure you would like to sing out?");
if (exit == true) {
window.location("header.php?logout=true");
}
return false;
});
});
This way, jQuery waits until your DOM is completely loaded.
As the element is not already created in the DOM, you need to use a delegated event for it to work.
See Understanding delegated events
So you would do like that, but you can attach to another container if you wish :
$(document).on('click', '.logout',
function() {
// Do your things
})
EDIT : I misleaded, everything is already in the DOM with PHP echo
page:content display.php
<td width="100"><a onclick="return confirmSubmit()" href="delete.php?id=<?php echo $row['id']; ?>&table=labour_details&return=content_display.php"><img src="../images/delete.png" border="0" alt="delete"></a></td>
page:delete.php
if(isset($_GET['id']) && isset($_GET['table']))
{
$id=$_GET['id'];
$tablename=$_GET['table'];
$return=$_GET['return'];
if($tablename=="labour_details")
{
$sql=mysql_query("SELECT * FROM `labour_details` WHERE `id`='$id'");
$row_1=mysql_fetch_array($sql);
$labour_name= $row_1['labour_name'];
if($labour_name!="")
{
$str=mysql_query("DELETE FROM `labour_details` WHERE `id`='$id'");
if($str)
{
header("location:$return?msg=Record Deleted Successfully!&id=$id");
}else{
echo "Problem in deleting :";
}
}
}
}
my problem is where to add alert so that before deleting record it show javascript massage if allow then it will delete record.
function confirmSubmit()
{
var agree=confirm("Are you sure you wish to Delete this Entry?");
if (agree)
return true ;
else
return false ;
}
This line will show confirm dialogbox before redirect.
<a onclick="return confirm('Are You sure?')">...</a>
First you need to create the confirmSubmit() function
<script>
function confirmSubmit()
{
if (confirm('Are you sure you want to delete this?'))
{
return true;
}
return false;
}
</script>
Then you attach that to your A tag, as you have done..
blah
You're probably looking for confirm.
Then, the body of the confirmSubmit function would look something like:
function confirmSubmit() {
return confirm ("Are you sure you want to do this?");
}
This works due to how event handlers work in javascript. Confirm returns true when the user clicks OK and false when they click Cancel.
By returning a true/false value from an event handler function you tell the window whether to keep processing that event. Returning false (which happens when the user clicks Cancel) tells the browser to stop processing the event and not follow the link.
<td width="100"><a onclick="confirmSubmit(id)"><img src="../images/delete.png" border="0" alt="delete"></a></td>
function confirmSubmit(delete_id)
{
var r=confirm("Are you sure you want to delete?");
if(r==true)
{
window.location.href = "delete.php?id="+delete_id"; ?>&table=labour_details&return=content_display.php"
}
}
<a onclick="return confirm('Are you sure?')" href="delete.php">delete</a>
I want to display a message using php only which says do want to delete this record ?
Then yes or no. Please help
echo "<tr bgcolor='#cccccc'><td>$hometeam</td> <td>Vs</td><td>$awayteam</td><td>$date</td><td>$venue</td><td><a href='fixtures.php?fixture_id=$fixture_id&edit=1' name='edit' >Edit</a></td><td><a href='fixtures.php?fixture_id=$fixture_id&delete=1' name='delete'>Delete</a></td></tr>";
In fixtures.php, you have to check the delete field of $_GET array and add a confirmation.
Something like that using only PHP :
if (! empty ($_GET['delete']) && ! empty ($_GET['fixture_id']) && empty ($_GET['confirm']))
{
$fixture_id = intval($_GET['fixture_id']);
echo "Are you sure you want to delete this record ?<br />";
echo "<a href='fixtures.php?fixture_id=$fixture_id&delete=1&confirm=1'>YES</a> - "
echo "<a href='fixtures.php?fixture_id=$fixture_id'>NO</a> - "
}
else if (! empty ($_GET['delete']) && ! empty ($_GET['fixture_id']) && ! empty ($_GET['confirm']))
{
mysqli_query ("DELETE FROM fixtures WHERE fixture_id=".intval($_GET['fixture_id']));
}
<script type="text/javascript">
function confirm_delete() {
return confirm("Are you sure you wish to delete that?");
}
</script>
Then in the link, add onclick='return confirm_delete();'
Edit: My mistake, didn't see the 'PHP only' bit...
Try it like this.
if(empty($_GET[verification])){?>
<center>Really wanna delete?<br> Yes No</center>
<?}else{
mysql_query("DELETE FROM XY WHERE id='$_GET[id]'");
jsAlert("has been deleted");
}
Something i use:
script>
jQuery(document).ready(function(){
jQuery(".deleteID").click(function () {
if (!confirm('Are you sure you want to delete this record?')) {
return false;
}
});
});
</script>
And in your HTML use the deleteID for ID.
i'm currentrly coding in php and when i click a button the message should me Would you like to delete this entry?
This is where I'm currently at
<button onClick="alert('Sure to delete entry?')">Remove Entry</button>
And sure it works. But when I press "x" to close the alert window it still deletes the entry.
How can you make a popup window that has the "Cancel" opition?
I would really appreciate the help! :)
if (confirm("Your question")) {
// do things if OK
}
Use confirm
Click here for examples
You can do this using the following JavaScript code:
if (confirm('Sure to delete entry?') == true) {
//write your code here to delete
}
The HTML
<a href="delete.php?delete=<?php echo $row['guestbook_id'];?>"
onclick="return areYouSure()">Remove Entry</a>
And the JS
function areYouSure()
{
var con = window.confirm("Are You Sure?");
if(con)
{
return true;
}
else
{
return false;
}
}
I am using php and javascript. I want to delete a record from database after a giving "Yes" and "No" confirmation to user. I am using confirmbox by google but don't know what should be my code structure inside that. I am new to programing. Can any one help me please
How can I add this php function on javascript
function deleteRecord($id)
{
$sql = "DELETE FROM user_database WHERE user_id = ".$id;
mysql_query($sql) or die("Can not delete");
}
And on page
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button!");
if (r==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
}
</script>
</head>
<body>
<input type="button" onclick="show_confirm()" value="Show a confirm box" />
You can use a simple yes/no confirmation dialogue with javascript :
<a href="job.php?do=delete&id=1"
onclick="return confirm('Are you sure to delete this record?');">Delete</a>
if user confirmed , you can precessed with your function to delete the record, else nothing going to happen in your phpside.