Javascript delete confirmation box in codeigniter - javascript

first of all, i already trying maybe all of the possible answer in this site but nothing is working for me.
i want to show pop up windows confirmation before delete my data. i use this
Delete
Maybe you need my controller
public function delete($id = 0) {
$id OR redirect(site_url('admin/barang'));
$this->barang_m->delete($id);
redirect(site_url('admin/barang'));
}
My model barang_m
public function __construct(){
parent::__construct();
parent::set_table('barang','idBarang');
}
public function delete($id = 0) {
if($data = parent::get($id)) {
parent::delete($id);
return true;
}
return false;
}
but when i click cancel it still delete the data even when i clicked 'x' to close the confirmation window. i'm so frustated please help me. Or maybe you can tell me why this problem occurs ?
**i alrady tried this possible answer Codeigniter when i click delete i want pop up notification if if click yes or not
jQuery delete confirmation box
Confirm box in CodeIgniter anchor link to delete record
and many more

try
Delete
and your function like this
function isconfirm(url_val){
alert(url_val);
if(confirm('Are you sure ?') == false)
{
return false;
}
else
{
location.href=url_val;
}
}
You could also try
Delete
And js would be
$('.confirmClick').click(()=> {
var sure = confirm('Are you sure ?');
if(sure){
return true;
}
return false;
})

You need to stop page from opening link when you press cancel.
But as seen in code its looks like you missed condition to check so.
Try this may solved you problem.
Delete
<script>
function isconfirm(){
if(!confirm('Are you sure ?')){
event.preventDefault();
return;
}
return true;
}
</script>

It's because you are running it out of the button itself and returning it. So you're saying, delete the button and return the confirmation.
Probably deleting the return will work, or make a function and make sure it doesn't return it but puts what you want in a variable or something.

<a href="javascript:;" onclick="return confirm_Action('<?php echo base_url(); ?>index.php/admin/delete_department/<?php echo $dept['Dept_id']; ?>');" ><button> DELETE</button></a>
Then, Your function confirm_Action could be like this:
//THis Allows Me To Control The Delete Actions
function confirm_Action(url){
// alert(url);
if(confirm('Are You Sure, This Action Can\'t Be Reversed?') == false)
{
return false;
}
else
{
location.href=url;
}
}
just like how #M. Deepak did it...
Thanks... Hope this helps

Related

Deleting after confirmation in php

I have a contact form.
Now when I need to delete a record, I need a confirmation box to ask for user's confirmation. I've achieved this, so far so good. But the record gets deleted for both OK and Cancel buttons.
My code is as follows:
<td>Edit / Delete </td>
<script>
function check(){
if(confirm("Are you sure you want to delete?")){
window.location.href = "delete.php?id=<?php echo $row["first_name"]; ?>";
return true;
}
else{
header("Location: http://localhost/test/display.php?show_details=Show+Details");
return false;
}
}
</script>
What could I do to delete the records only after clicking OK button in the confirmation box and return to display.php(same page) on clicking Cancel?
It should navigate to delete.php only on clicking OK and stay in display.php on clicking Cancel.
I'm new to PHP.. Help please...
An HTML anchor has a default action for clicks on it: open a link. Now if you add another click handler in JavaScript, you will have to prevent the default action for that element and thus prevent the link from being opened.
To prevent the default event action from within an inline event handler (one that is setup using the attribute onclick in contrast to use addEventListener() in JS) you must return false in that event handler.
See also: How to prevent default event handling in an onclick method?
The following two snippets fix that issue and are also re-using the href-attribute, so it must not be hardcoded in JS again.
variant 1:
<p>
Edit
/
Delete
</p>
<script>
function check(anchor) {
/* Only open the delete-link, if the confirm returns TRUE */
if (confirm('Are you sure you want to delete?')) {
window.location.href = anchor.href;
}
/* Return FALSE to prevent the default link action */
return false;
}
</script>
variant 2:
<p>
Edit
/
<!-- Return FALSE to prevent the default link action -->
Delete
</p>
<script>
function check(anchor) {
/* Only open the delete-link, if the confirm returns TRUE */
if (confirm('Are you sure you want to delete?')) {
window.location.href = anchor.href;
}
}
</script>
variant 3A:
Not returning false, but using event.preventDefault(). (Inspired by the comment of #ths)
<p>
Edit
/
Delete
</p>
<script>
function check(event, anchor) {
/* Prevent the default link action */
event.preventDefault();
/* Only open the delete-link, if the confirm returns TRUE */
if (confirm('Are you sure you want to delete?')) {
window.location.href = anchor.href;
}
}
</script>
variant 3B:
Not returning false, but using event.preventDefault(). (Inspired by the comment of #ths)
<p>
Edit
/
Delete
</p>
<script>
function check(anchor) {
/* Only open the delete-link, if the confirm returns TRUE */
if (confirm('Are you sure you want to delete?')) {
window.location.href = anchor.href;
}
}
</script>

How to prevent closing blockUI if there is input on textarea

Please I need your help in this..
I have built a $.blockUI modal and I have an 'X' button to close it.
On button click, if there is some value on textarea I want to display a message that there is input and prevent closing the modal.Any ideas how can I do this?
close_modal function:
function close_modal(event){
$.unblockUI();
if (document.getElementById("comments").value.length > 0){
alert("There is input!");
//Here must be the code to prevent closing modal
.
.
.
//End of code
}
}
You can try something like this
function close_modal(event){
if (document.getElementById("comments").value.length > 0){
if(confirm("There is input!")) $.unblockUI();
} else $.unblockUI();
}
$.unblockUI() is the function to close the modal window. If you won't run it, the modal will stay opened.
function canCloseTheModal() {
return document.getElementById("comments").value.length > 0;
}
function close_modal(event){
if (canCloseTheModal()){
$.unblockUI();
} else {
// inform the user what to do
...
}
}

javascript onclick return confirm then submit

I need to confirm delete before submit form using javascript, i tried this code but it didnt work :
Delete
You should confirm first before submitting the form. And you need to set a condition whether it returned true or false
Delete
Submit form only when user confirm it otherwise set return false. So your form will not be submitted.
function confirmdelete() {
if (confirm("Are you sure?")) {
// submit form
}
return false;
}
Please check the comments against the code.
$(function () {
//Attach click event to the link. In this case all links
//You might want to update this, make it more specific by using id or name or class of the a tag
$('a').on('click', function (event) {
//prevent the default action of the tag
event.preventDefault();
//confirm
var conf = confirm('Confirm Delete');
if (conf) {
//you action if true
}
else {
return;
}
});
});

"Leaving website" alert

I have the following href on my site:
a href="http://www.google.com" name="external" onclick="confirmExit(this.name)
I just can't figure out how to make user stay on my website if he/she doesn't want to leave by clicking "cancel" (incomplete code below).
function confirmExit(name){
if(name == "external"){
if(confirm("Go to external site?")){
}
}
}
jQuery/Ajax is not an option.
Just return false.
function confirmExit(name){
if(name == "external"){
if(!confirm("Go to external site?")){
return false;
}
}
}
If you want this to happen on all links, or even when the user closes the tab, check out #megawac's answer.
Use window.onbeforeunload to show a confirm dialog before a user leaves the page
$(window).bind("beforeunload", function() {
if(true) { //display confirm dialog?
return "Are you sure you want to leave?";
}
});
You don't need a function. I suggest you to use this:
$(function () {
$('a[name="external"]').click(function () {
if (!confirm("Go to external site?")) {
event.preventDefault();
}
});
});
If you use this, you don't need to add onclick="confirmExit(this.name)" to everything. Just add the above and your work will be done.
You can do this if you want behaviour of JSFiddle:
window.onbeforeunload = function(){ return "Are you sure you want to leave the page?"; }
Use the return false inside the confirmExit() function and also use return at inline javascript like
HTML
Some Link
//Use return here also ---^
JS
function confirmExit(name){
if(name == "external"){
if(!confirm("Go to external site?")){
return false;
}
}
}
DEMO

Javascript confirm box, if cancel is clicked, it will not reload the page

Is there away that the confirm box appeared, if i clicked "ok" it will go to another page and if i clicked "cancel" it will just stay and the current page will not reload again? THANK YOU.
function reload()
{
var r=confirm("Do you want to leave page!");
if (r)
{
//write redirection code
window.location = "http://www.yoururl.com";
}
else
{
//do nothing
}
}
call this function when you want to confirmation from user.........
You can use confirm() for this, which returns true on ok or false on cancel.
function myFunction(){
if(confirm("Would you like go to other page?")){
window.location = "http://yahoo.com";
}else{
alert('fine, if not want');
}
}
myFunction();
Updated
DEMO
UPDATED2
<button onclick="return logout()" >logout</button>
<script>
function logout(){
if(confirm("Would you like go to other page?")){
window.location = "failed.php";
}else{
//do your stuff on if press cancel
}
}
</script>
You may try doing
<script>
function myfunction(){
if(confirm("The confirm message")){
youDoTheThingsHere();
return false;
}else{
console.log("I cancelled the dialog!");
return false;
}
}
</script>
I'm not sure about your certain situation and the code that is involved when you call the confirm, but this has worked for me. The other option you may look at as a last resort is using something like a bootstrap modal to trigger a "confirm" modal. With the bootstrap modal you can then style it how you want... hope I could help!
Use "return None"
function myFunction(){
if (confirm("Confirm to reset your content!")){
location.reload();
}else{
return None;
}
}

Categories

Resources