redirect after click button delete - php

I have problem when I click button edit, I hope I can redirect to
http://localhost/activity/edit_activity/172
where 172 is activity_id but actually i redirect activity_detail so here my code
My HTML
<a href='<?php echo site_url();?>activity/delete_activity_edit/<?php echo $row->activity_detail_id;?>' onclick='return confirm()' class="btn btn-sm btn-danger">
<i class="glyphicon glyphicon-trash"></i>Delete</a>
My Controller
public function delete_activity_edit()
{
$id=$this->uri->segment(3);
$this->activity->remove($id);
redirect('activity/edit_activity/'.$id);
}
My Model
public function remove($id)
{
$where_array = array(
'activity_detail_id'=>$id);
$this->db->where($where_array);
$this->db->delete('t_trx_activity_detail');
}
Where is my wrong code. And how to resolve it?

You can use below code format:
<i class="glyphicon glyphicon-trash"></i>Delete
Add this javascript function:
<script type="text/javascript">
var url="<?php echo base_url();?>";
function confirm_delete(id){
var r=confirm("Do you want to delete this?")
if (r==true)
window.location = url+"activity/delete_activity_edit/"+id;
else
return false;
}
</script>
Now your controller redirection works.

Related

Alert before deleting data from Mysql

I want an alert to display to make sure the user will double confirm if they want to delete the data.
<a onclick="return confirm('Are you sure?')" href="del.php?masId=<?php echo $row['masId'] ?>" >delete</a>
My php code:
$masId = $_GET['masId'];
$res=$MySQLiconn->query("Delete FROM masinflation WHERE masId=".$masId);
However, the data still will be removed even I click cancel.
You can use below code which will prevent default behavior of anchor tag if you click on cancel button.
<a class="confirmation" href="del.php?masId=<?php echo $row['masId'] ?>" >delete</a>
<script type="text/javascript">
var elems = document.getElementsByClassName('confirmation');
var confirmIt = function (e) {
if (!confirm('Are you sure?')) e.preventDefault();
};
for (var i = 0, l = elems.length; i < l; i++) {
elems[i].addEventListener('click', confirmIt, false);
}
</script>
<a class="btn btn-large btn-danger" onclick="return checkDelete()" href="del.php?masId=<?php echo $row['masId'] ?>" >delete</a>
<script language="JavaScript" type="text/javascript">
function checkDelete(){
return confirm('Are you sure?');
}
</script>

call php function with onclick event

php_file:-
class class_name{
function method1(){
//some code
}
function method2(){
//some code
}
}
in html:-
<i class="fa fa-angle-right" onclick=""></i>
How can i call function from php file when the icon clicked
A jquery demo is as follows:-
abc.php:- (you can change extension to .html also if you are not going to use any php code in this file)
<button class="fa fa-angle-right" onclick="myFunction();">Click Me Please!</button>
<script src= "https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
function myFunction(){
$.ajax({url: "demo_test.php", success: function(result){
alert(result);
}});
}
</script>
demo_test.php:-
<?php
include_once('def.php');
$obj = new myClass();
$result = $obj->method1();
echo $result;
?>
def.php:-
<?php
class myClass{
function method1(){
echo "demo";
}
function method2(){
echo "demo2";
}
}
Output:- http://prntscr.com/clw7hu and http://prntscr.com/clw7sl
Note:-
All three files must be at same working location(same directory)
It's a very small and easy sample to understand what is exactly need and how it can performed. Thanks
You can't, you need jQuery to do this.
Example:
<i class="banana"></i>
<script type="text/javascript">
$(document).ready(function() {
$(".banana").click(function() {
// write action you want to do here
});
});
</script>

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.

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>

Categories