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>
Related
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.
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 have a small problem, I made a delete button with a PHP while loop which looks like this:
while($something = mysql_fetch_array($sql_something)){
$id = $something['id']
echo '<button onclick="delconfirm()">Delete</button>
}
this echo's a few delete buttons for some content. However I need user confirmation for deleting first, this is where onclick="delconfirm()" comes in.
my confirm looks like this:
function delconfirm()
{
var r=confirm("Are you sure you want to delete this content?");
if (r==true){
// ...do nothing i guess? it needs to redirect using the PHP echo'd link...
}
else{
window.location = "edit.php";
}
}
However, whether you press cancel or ok, it'll delete it anyway. How can I fix this?
Change it to this:
while($something = mysql_fetch_array($sql_something)){
$id = $something['id']
echo '<button onclick="return delconfirm();">Delete</button>
}
And then your function:
function delconfirm()
{
return confirm("Are you sure you want to delete this content?");
}
EDIT: If you want a more unobtrusive solution:
while($something = mysql_fetch_array($sql_something)){
$id = $something['id']
echo '<input type="button" value="Delete" data-id="$id" />';
}
And then some javascript to bind the event:
function bindButtons() {
var buttons = document.getElementsByTagName("input");
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].type == "button") {
buttons[i].onclick = function () {
location.href='somewhere.php?id=' + this.getAttribute("data-id");
}
}
}
}
and bind it to the window.onload, as per Ian suggestion:
window.onload = bindButtons;
Note: If you were using jQuery this solution would be easier and more elegant.
Working jsFiddle
If the user presses cancel then you need to stop the event from doing what it would normally do. Try this, for example:
function delconfirm(e) {
e = e || window.event;
if (!confirm("Are you sure you want to delete this content?")) {
e.preventDefault();
// This will prevent the event from bubbling up to the <a>.
e.stopPropagation();
return false; // For the ancient/crappy browsers still out there.
}
return true;
}
You need to stop/delete the current click event. After your code is executed the event sinks to the anchor and triggers a click. With MooTools just add 'new Event().stop();'. I think jQuery has also something like this.
EDIT: Hanlet EscaƱo is right. You can return true (the browser will redirect to the URL in the href, or false to let the browser do nothing)
In order to prevent to the HTML link to work, you have to return false in your js function or event.preventDefault() where event is an argument which is passed to the click event function
I did thin when putting a click event on the a element and not on an element inside the a tag. But it might work.
Is it Possible to call php page under Javascript function?
I have an javascript function and I want to call php page if someone press okk
Here is my code so far
function show_confirm()
{
var r=confirm("Do You Really want to Refund money! Press ok to Continue ");
if (r==true)
{
alert("You pressed OK!");
return true;
}
else
{
alert("You pressed Cancel!");
}
}
and this Js function i m using here
<td align="center"><input name="Refund" onclick="show_confirm()" type="submit" value="Refund" /></td>
now i want if user press okk then call other php page ...
if you want to redirect to a page:yourphppage.php if the user pressed ok.
function show_confirm()
{
var r=confirm("Do You Really want to Refund money! Press ok to Continue ");
if (r==true)
{
window.location="yourphppage.php";
return true;
}
else
{
alert("You pressed Cancel!");
}
}
If you are looking for page direction:
window.location="index.php";
There are plenty other methods to redirect: http://grizzlyweb.com/webmaster/javascripts/redirection.asp
Place this where you want your redirect to happen:
location.href = "newpage.php";
Checkout
http://www.w3schools.com/xml/xml_http.asp
Then have a look at
http://mootools.net/docs/core/Request/Request
Any of the JS libraries prototype Jquery etc. have a wrapper to make requests and your life easier.
Then just have a button and set its onClick to be the request call to the server and listen for the response.
Some tips you can append data to the request URL to pass your PHP script variables.
Checkout
http://www.w3schools.com/tags/ref_urlencode.asp
If you only want to redirect the page to the "refund" page without using AJAX you can do something along the lines of the following:
(assumes $transaction_id variable is the id number you are looking to refund)
HTML Lines:
<td align="center">
<input name="Refund" onclick="show_confirm(<?php echo $transaction_id ?>)" type="submit" value="Refund" />
</td>
Javascript lines:
function show_confirm(transaction_id)
{
var r = confirm("Do You Really want to Refund money! Press ok to Continue ");
if (r == true)
{
alert("You pressed OK!");
location.href = "refund.php?transaction_id="+transaction_id;
return true;
}
else
{
alert("You pressed Cancel!");
}
}
i have tried:
<?php include("delete.php") ?>
<?php
....
....
....
if($result=mysql_query($sql))
{
echo "<table><th>Id</th><th>Name</th><th>Description</th><th>Unit Price</th>";
while($row = mysql_fetch_array($result))
{
echo "<tr><td>".$row['Id']."</td><td>".$row['Name']."</td><td>".$row['Description']."</td><td>".$row['UnitPrice']."</td>
<td><a href='delproduct($row[Id])' onclick = 'return MsgOkCancel()'>Delete</a></td></tr>";
echo "<br/>";
}
}
?>
following javascript is in the same page:
<script type="text/javascript" language="javascript">
function MsgOkCancel() {
if (confirm("Are You Sure You Want to Delete?"))
{ return true }
else
{return false}
}
</script>
where delproduct is a javascript function in delete.php
written like:
<script type="javascript">
function delproduct(Id)
{
alert('Id '+ Id);
}
<script>
** after ** clicking Delete a okcancel message-box appear asking conformation
** but ** after clicking 'ok' it should execute statements inside delproduct function but it doesn't
it gives error like:
Object Not Found :The requested URL was not found on this server.
what would be the problem?
pls help,
thanks
A URI without a scheme (such as http:) is treated as a relative URI.
You appear to be looking for javascript: (which should never be used for anything other than creating bookmarklets).
What you should be doing is something along the lines of:
onclick="if (MsgOkCancel()) { delproduct($row[Id]); return false; } else { return false; }"
However, you should have something that works in the href, but since this appears to be making a significant change on the server, you should be using POST not GET, so a link is the wrong tool.
What you probably should have is:
<form action="/delete" method="post" onsubmit="return delete(this);">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($row[Id]); ?>">
<input type="submit" value="Delete">
</form>
Combined with:
function delete(form) {
if (confirm("Are You Sure You Want to Delete?")) {
delproduct(form.elements.id.value);
}
return false;
}
Better yet, get rid of the onsubmit attribute and assign the event using JavaScript.
I think you need a different setup.
First of all, if you are going to call javascript functions in an href attribute, you need to prepend it with javascript: like so href="javascript:delproduct(...)". But calling javascript from an href attribute is not recommended. That attribute is intended for urls.
I would advice you to create a function that displays the messagebox and based on the action of the user, calls the delproduct function. Something like:
function confirmDelProduct( id )
{
if( msgOkCancel() )
{
delproduct( id );
}
// return false is meant to stop the href url from being called
return false;
}
And in your html:
<a href="#" onclick="return confirmDelProduct(' . $row[ 'id' ] . ')"> ... etc
What about this one:
PHP:
<a href="javascript:void(0);" onclick=\"delproduct({$row[Id]})\">
JS:
function delproduct(Id){
if(MsgOkCancel()) alert('Id '+ Id);
}