Error in JavaScript confirm message - php

I am new to JavaScript. I cannot find the error of this code.
<td class="paid">
<a href="invoice.php?id=<?php echo $invoice_result['id']; ?>" onclick="confirm()">
<img src="images/view_3.gif"/>
</a>
</td>
<script type="text/javascript">
function confirm() {
if (confirm("Do you realy want do delete this vehicle?")) {
return true;
} else {
return false;
}
}
</script>
help me to find my error..

Please check with this
<td class="paid"><img src="images/view_3.gif"/></td>
<script type="text/javascript">
function confirmChk(){
if(confirm("Do you realy want do delete this vehicle?")){
return true;
}
else{
return false;
}
}
</script>
NB:The problem was with your function name I changed it confirm to confirmChk

You're trying to overload "confirm" function, which is built-in function and thus your code is not working. Change the name of your function from confirm to something else.

try this...
<td class="paid">
<span onclick="return confirm('Do you realy want do delete this vehicle?')">
<img src="images/view_3.gif"/>
</span>
</td>

Change the name of your function to something else than confirm.

Use this
onClick="return confirm('Do you realy want do delete this vehicle?');"
..
<td class="paid"><a href="invoice.php?id=<?php echo $invoice_result['id']; ?>"
onClick="return confirm('Do you realy want do delete this vehicle?');">
<img src="images/view_3.gif"/></a></td>
The error happened to you cos of the reserved word confirm.
chek this too Reserved words in javascript http://www.javascripter.net/faq/reserved.htm

Related

Opening a PHP link in a jQuery UI dialog

$(function() {
$("#register").dialog({
autoOpen:false,
show: {
effect:"",
duration: 1000
},
hide: {
effect:"",
duration: 1000
}
});
$("#register-opener").click(function() {
$("#register").dialog("open");
});
});
<td><a id="register-opener" href="?id=<? echo "$members_row[id]"; ?>"><i class="glyphicon glyphicon-pencil" title="Edit"></i></a></td>
So what I'm trying to accomplish is to click on a link to be able to edit a certain users information, the problem is I can only get the popup to occur when replacing the href with href="#". Can someone assist. Thanks.
You need to make some changes in jQuery to achieve this.
$("#register-opener").click(function(e) {
e.preventDefault();
$("#register").dialog("open");
});
Above script will make sure that when ever user clicks on link it doesn't go to URL mentioned in href and execute following codes.
https://api.jquery.com/event.preventdefault/
If e.preventDefault() is called then default action of the
event(click in above case) will not be triggered.
There seems to be problem with dialogue function you have. Use prompt instead and be sure to remove icon inside a tag
jQuery("#register-opener").click(function() {
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.getElementById("#yourid").innerHTML =
"Hello " + person + "! How are you today?";
//Or do your thing
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<a id="register-opener" href="?id=<?php echo '$members_row[id]'; ?>">
icon<i class="glyphicon glyphicon-pencil" title="Edit">
</i>
</a>
</div>
This might be better accomplished by putting your ID in a data- attribute rather than using href, which is meant to be used for specifying a URL for the web browser to follow.
<td><a id="register-opener" href="#" data-id="<? echo $members_row[id]; ?>"><i class="glyphicon glyphicon-pencil" title="Edit"></i></a></td>
You can then access the value of the data-id attribute in jQuery by using
$("register-opener").attr("data-id");

Delete Confirmation when click on link

I have this code inside a while loop for fetching data from the database. I want to show a confirmation popup when the Delete link is clicked.
echo "<td><a href='delete.php?id=" . $row['serial_no'] . "'>Delete</a></td>";
How can this be done?
Try out confirm function from JavaScript, learn jQuery as fast as possible and try to seperate javascript from html ;).
http://www.w3schools.com/jsref/met_win_confirm.asp
<td>
<a href="delete.php?id=<?php echo $row['serial_no'] ?>" id="a_id">
Delete
</a>
</td>
<script type="text/javascript">
$(function() {
$('td a#a_id').click(function() {
return confirm("Are You sure that You want to delete this?");
});
});
</script>
add onClick attribute for anchor link and call the function on it. Or simply show confirm.
<a onClick = "confirm ('Are You Sure?')"
Complete Example
function disp_confirm()
{
var r=confirm("Press a button!")
if (r==true)
{
alert("You pressed OK!")
}
else
{
alert("You pressed Cancel!")
}
}
<a onclick="disp_confirm()"
try this
function DeleteClick(serial_no)
{
if(confirm('Are you sure to delete ' + serial_no + '?'))
{
alert('Data deleted');
return true;
}
return false;
}
echo "<td><a href='delete.php?id=" . $row['serial_no'] . "' onclick="return
DeleteClick(\''. $row['serial_no'].'\')">Delete</a></td>";
echo "<td>Delete</td>";
try this code,hope it will work-
<td>
<a href="#" onclick="conf("delete.php?id=".<?php echo $row['serial_no']; ?>.")">
Delete
</a>
</td>
<script type="text/javascript">
function conf(str) {
if(confirm("Your Message") == true){ location.replace(str);}
}
</script>
and in the delete.php page-just grab the id from url and run the delete query.

confirmation delete message box for an image?

The scenario is when I click on the X image next to a record in the tour_manage.php, this deletes the record by which is executed by another php page called delete_tour.php
I have managed to add the pop up box to the X image and when I click on the X, the message box comes up saying OK to delete or to CANCEL. However, the problem is when I click on OK and/or CANCEL, both buttons delete the record.
Help please!
These are the 2 scripts I got.
First script is tour_manage.php
Second script is delete_tour.php
<script type="text/javascript">
function confirm() {
if(r == true) {
window.location='delete_tour.php;
} else {
window.location='tour_manage.php';
}
}
</script>
<a href="delete_tour.php?tid=<?php echo $tid ?>" onclick="return confirm('Are you sure you want to delete this tour?');" ><img src="images/delete
logo icon.png" width="15" height="15"/></a></td>`
This is the delete_tour.php script
<?php
session_start();
include('../config.php');
$id=$_GET['tid'];
$sql=mysql_query("delete from tour where tid='$id' ");
if($sql)
{
header('location:tour_manage.php');
}
?>
UPDATE
<a onclick="return confirm('Are you sure you want to delete this tour?')" href="delete_tour.php?tid=<?php echo $tid ?>">aaa</a>

Confirm box in CodeIgniter anchor link to delete record

I am using the following code to delete a record from a database. But I am facing a problem: when I click on "cancel" in the confirm box then it deletes the record. If I click cancel it returns false but how does it delete the record?
What am doing wrong?
Javascript code:
function ConfirmDialog() {
var x=confirm("Are you sure to delete record?")
if (x) {
return true;
} else {
return false;
}
}
PHP code in view:
<?php
echo anchor('user/deleteuser/'.$row->id, 'Delete', array('class'=>'delete', 'onclick'=>"return ConfirmDialog();"));
?>
Everything in one line
<?=anchor("user/deleteuser/".$row->id,"Delete",array('onclick' => "return confirm('Do you want delete this record')"))?>
Try This:
Delete
and use this in your script:
<script type="text/javascript">
var url="<?php echo base_url();?>";
function delete(id){
var r=confirm("Do you want to delete this?")
if (r==true)
window.location = url+"user/deleteuser/"+id;
else
return false;
}
</script>
Are you getting any Javascript errors in the console? I suspect that some other Javascript may be interfering. This simple test page works fine:
<?php echo anchor('user/deleteuser/'.$row->id, 'Delete', array('class'=>'delete', 'onclick'=>"return confirmDialog();")); ?>
<script>
function confirmDialog() {
return confirm("Are you sure you want to delete this record?")
}
</script>
In your View:
<?= anchor("admin/delete_article/{$article->id}", 'Test', ['name'=>'submit', 'value'=>'Delete', 'class'=>'btn btn-danger', 'onclick'=>'return confirm()']);
?>
OR
<?=
form_open('admin/delete_article'),
form_hidden('article_id', $article->id),
form_submit(['name'=>'submit', 'value'=>'Delete', 'class'=>'btn btn-danger', 'onclick'=>'return confirm()']),
form_close();
?>
JavaScript in your View:
<script>
function confirm(){
job=confirm("Are you sure to delete permanently?");
if(job!=true){
return false;
}
}
</script>
See webGautam answer Here
I use This in my code
<a href='<?php site_url('controler/function/$id');?>' onClick='javascript:return confirm(\"Are you sure to Delete?\")'>Delete</a>
Try this
Delete
Then your function will be like:
function isconfirm(url_val){
alert(url_val);
if(confirm('Are you sure you wanna delete this ?') == false)
{
return false;
}
else
{
location.href=url_val;
}
<?=
form_open('admin/delete_noti'),
form_hidden('noti_id',$notification->id),
form_submit('submit','Delete',array('onclick' => "return confirm('Do you want delete this record')",'class'=>'btn btn-danger float-center')),
form_close();
?>
This is the deletion code. first of all we open form admin is our controller name and delete_noti is the function inside admin controller. We are sending id as hidden form. Here $notification is the variable which is passed through method which is available in controller.
Finally, if you want javascript confirm: so when the user clicks, it asks to confirm and then goes to delete if the user clicks okay. You're asking confirmation when the row has already been deleted.
In View Page :-
<td>DELETE</td>
Controller Code:-
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class user extends CI_Controller {
public function deleteuser($userid)
{
$this->db->delete('user', array('user_id'=>$userid));
$this->session->set_flashdata('success','User deleted Successfully!!!');
redirect(base_url().'admin/user');
}
}
?>
Then Show Success Message on View:-
<div class="row col-md-12">
<?php
if($this->session->flashdata('success')){
?>
<div class="alert alert-success " style="display:none;">
<?php echo $this->session->flashdata('success'); ?>
<?php
} else if($this->session->flashdata('error')){
?>
<div class = "alert alert-danger" style="display:none;">
<?php echo $this->session->flashdata('error'); ?>
</div>
<?php } ?>
</div></div>

yes no option with javascript delete function for a link

In 1stpage I have a table with full of data, and end of every row there is a delete link. When i click on that link, the link goes to another delete page where mysql delete query delete that particular data from database. That delete page redirect with the 1st page (location:1stpage). Now when i want to apply a javascript delete yes/no checking, every time it pops up with "Are you sure?" either i press yes or no the data is deleted from my table. I think the return false cannot working in this case. The code are in below. Please help me what should i do exactly.
Sorry for my bad english.
Thanks in advance.
1st page:
<?php
include "include.php";
?>
<script type="text/javascript">
function show_confirm()
{
var con = confirm("Are You Sure");
if (con ==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
}
</script>
<table>
<tr>
<td align="center">EDIT OR DELETE DATA</td>
</tr>
<tr>
<td>
<table border="1">
?php
if(isset($_POST['submit']))
{
$order = "UPDATE books SET author='$_POST[author]', title='$_POST[title]', price='$_POST[price]' WHERE isbn='$_POST[isbn]'";
mysql_query($order) or die (mysql_error());
}
$order = "SELECT * FROM books ORDER BY author" or die (mysql_error());
$result = mysql_query($order);
while ($row=mysql_fetch_array($result))
{
echo ("<tr>
<td>$row[isbn]</td>
<td>$row[author]</td>
<td>$row[title]</td>
<td>$row[price]</td>
<td> Edit </td>
<td> <a href=\"delete.php?isbn=$row[isbn]\" onclick='show_confirm()'> Delete </a> </td>
</tr>");
}
?>
</table>
</td>
</tr>
</table>
the page where mysql delete query run :
<?php header("Location: editordeletedata.php"); ?>
<p> <title>delete</title> </p>
<?php
include "include.php";
?>
<?php
if(isset($_GET['isbn']))
{
$order = "DELETE FROM books WHERE isbn = '$_GET[isbn]'";
mysql_query($order) or die (mysql_error());
}
?>
Look at this simple code snippet I wrote, should get you fixed up right.
<html>
<head>
<script type="text/javascript">
function show_confirm() {
return confirm("Are You Sure");
}
</script>
</head>
<body>
<a href="http://www.google.com" onclick='return show_confirm();'> Google </a>
</body>
</html>
Fiddle
I don't see any reason why return false will not work.
function show_confirm()
{
if (confirm("Are You Sure"))
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
return false;
}
}
Disable the delete link and move the redirection to the javascript function. The link should look like this:
<a href=\"javascript:void(0)\" onclick='show_confirm($row[isbn])'> Delete </a>
And the javascript function just like this:
function show_confirm(isbn)
{
if (confirm("Are You Sure"))
{
alert("You pressed OK!");
location.replace('delete.php?isbn='+isbn);
}
else
{
alert("You pressed Cancel!");
}
}

Categories