I am trying to delete particular record using href. But it's not working properly when i click on button its passing in URL like this
http://localhost/project_x/Organisation/Organisation/%3C?php%20echo%20base_url();?%3EOrganisation/delete_org?id=3
I don't know where is the mistake can anyone help me.It's passing id in URL is correctly.
Here is my controller:
function delete_org() {
// Pass the $id to the org_delete() method
$this->Org_model->org_delete($id);
redirect('Organisation/organisation');
}
here is my redirect organisation function
public function organisation() {
$data['organisations'] = $this->Org_model->getOrganisations();
$this->load->view('template/header');
$this->load->view("organisation", $data);
$this->load->view('template/footer');
}
Here is my model:
function org_delete($id) {
$this->db->where('id', $id);
$this->db->delete('organisation');
}
And Here is my view buttons:
<?php
echo "<td class='text-center'><div class='btn-group btn-group-xs'><a href='#' data-logid='" . $org->id . "' data-target='#editGroups' data-toggle='modal' title='Edit' class='open_modal btn btn-default'><i class='fas fa-edit'></i></a>";
echo "<a href='<?php echo base_url();?>/Organisation/delete_org?id=$org->id' class='btn btn-danger confirmation'><i class='fas fa-times'></i></a></div></td></tr>";
?>
Can anyone help me where i did mistake.
Thanks in advance.
Issue is you are adding php tag again in echo. change your delete anchor tag as below:
echo "<a href='".base_url()."/Organisation/delete_org?id=$org->id' class='btn btn-danger confirmation'><i class='fas fa-times'></i></a></div></td></tr>";
Also in controller define $id
function delete_org() {
// Pass the $id to the org_delete() method
$id = $_GET['id'];
$this->Org_model->org_delete($id);
redirect('Organisation/organisation');
}
Hope this will help you :
Change view buttons like this:
<?php
echo "<td class='text-center'><div class='btn-group btn-group-xs'><a href='#' data-logid='" . $org->id . "' data-target='#editGroups' data-toggle='modal' title='Edit' class='open_modal btn btn-default'><i class='fas fa-edit'></i></a>";
echo "<a href='".base_url('Organisation/delete_org?id='.$org->id)."' class='btn btn-danger confirmation'><i class='fas fa-times'></i></a></div></td></tr>";
In controller :
function delete_org() {
$id = $this->input->get('id');
$this->Org_model->org_delete($id);
redirect('Organisation/organisation');
}
function delete_org($id= NULL) {
// Pass the $id to the org_delete() method
$id = $this->input->post('id');
$this->Org_model->org_delete($id);
redirect('Organisation/organisation');
}
hope this helps you
You may try this deleting record using codeigniter framework by data-attribute as bellow.
html
<a class="deletedata" data-id="<?php echo $data['id] ?>">delete</a>
js
$(".deletedata").on("click",function(){
var id=$(this).data('id');
var baseurl = $("#base_url").val();
$.post(baseurl+"Products/dltProduct/"+id,function(id){
window.location.reload();
});
});
Here my Controller name is Products. you can define your name and
than after define your method name.
Controller
public function dltProduct($id) {
$this->db->where("id",$id);
$this->db->delete("tbl_name");
}
Related
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.
I have A VERY OLD SCRIPT that I need to modify. You will notice from the code it is using mysql_query() which is outdataed however that is not my issue.
I have a text link in a file named surveycommentslist.php. The link opens a jquery model window that I need to capture a user inputed text in and then save it to a mysql along with the value of the links data-id which tells me which unique user ID to connect the comment to. My issue is I am not able to get the value of data-id using the code I have below.
Here is my code
<html>
<head>
<!-- common scripts -->
<!-- jQuery library -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.js"></script>
<!-- jQuery migrate -->
<script src="js/jquery-migrate-1.2.1.min.js"></script>
<!-- bootstrap framework plugins -->
<script src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<script>
$(document).on('click', '.reply', function()
{
//get acommentuid
var val = $(this).attr('data-id');
$.post('surveycommentslist.php', {acommentuid: val}, function(data)
{
console.log(data);
});
});
</script>
<?php
//lets list comments
$scommentsql = mysql_query("SELECT * FROM survey_comments
WHERE surveyid=$surveyid ORDER BY commentdate asc");
while($scrows = mysql_fetch_array($scommentsql))
{
extract($scrows);
$the_comment = stripslashes($the_comment);
$commentdate = date("m/d/Y h:ia", strtotime($commentdate));
if($touid > 0) { $indent = "margin-left:35px"; }
//get name of person making the comment
$nsql = mysql_query("SELECT fname, lname, userlevel, id AS scommentid FROM users WHERE id=$uid LIMIT 1");
$namerow = mysql_fetch_array($nsql);
$commenters_fname = stripslashes($namerow['fname']);
$commenters_lname = stripslashes($namerow['lname']);
if($namerow['userlevel'] > 19)
{
$adminicon = "<img src='./img/admin_smicon.png' alt='admin'>";
}
echo "<div class='ch-message-item clearfix' style='$indent'>
<div class='ch-content'>
$the_comment
</div>
<p class='ch-name'>
<strong>$adminicon $commenters_fname $commenters_lname</strong>
<span class='muted'>$commentdate</span>";
if($touid == 0)
{
echo " <a href='#switch_modal' class='reply btn btn-default id='$scommentid' btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
}
echo "</p>
</div>";
}
$(document).on('click', '.reply', function(e)
{
e.preventDefault();
//get acommentuid
var val = $(this).data('id');
$.post('surveycommentslist.php', {acommentuid: val}, function(data)
{
console.log(data);
});
});
Class attribute is not closed properly. Quotes issue.
Change this
echo " <a href='#switch_modal' class='reply btn btn-default id='$scommentid' btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
to
echo "<a href='#switch_modal' class='reply btn btn-default' id='$scommentid' btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
Maybe there's just an issue with the single and double quote in the REPLY. You said $scommentid has a value, so I assume there's just an echo problem. Try these:
//codes
.
.
//RE-POSITION class values from id value
echo "<a href='#switch_modal' class='reply btn btn-default btn-small' id='$scommentid' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
//or by concatenation if I am wrong with the above solution
echo "<a href='#switch_modal' class='reply btn btn-default id=".'"$scommentid"'." btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
//or by using the " if I am wrong with the above solutions
echo "<a href='#switch_modal' class='reply btn btn-default id="$scommentid" btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
.
.
//codes
When I copy-paste your code in a file & execute that in a browser, it shows the "data-id" attribute as blank: http://screencast.com/t/pasXI14dp0x . That is due to the incorrect HTML pointed out in earlier messages.
Even after the corrections, there is still the 'btn-small' text which is not in any attribute.
The incorrect HTML is:
echo " <a href='#switch_modal' class='reply btn btn-default id='$scommentid' btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
The correct HTML should be:
echo " <a href='#switch_modal' class='reply btn btn-default btn-small' id='$scommentid' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
Another alternative you could try is hardcode some value in the "data-id" attribute & find out if it's being passed. If that works, then the issue is with the HTML & should be fixed with above corrected HTML code.
Your HTML markup is wrong
You can check that by doing a view source
Change the following code
echo " <a href='#switch_modal' class='reply btn btn-default id='$scommentid' btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
with
echo "<a href='#switch_modal' class='reply btn btn-default btn-small' id='$scommentid' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
I would suggest to you to return the data from MySQL as JSON using return json_encode($data) which $data is an array of key-value pairs and when you get the data in your script tag / JavaScript, just build the HTML there and add it to a div instead of returning HTML from the server.
When you build your HTML in your script tag / JavaScript, you add the data from the JSON response and loop over the comments.
Maybe that's just me but I like clean code :)
I'm fairly new to coding, but have managed to create a function that gets all my elements in the database and echos them out in a list. However, I want each element to have an individual anchorpoint so that i can use the "#" in my link to refer to a specific element.
My whole function currently looks like this (Please excuse some strange names as they're in norwegian):
function visAktiveForslagFlestStemmer()
{
echo "<section id='innhold'><h2>Aktive forslag</h2>";
$db = returnerDB();
if(!$db)
{
die("Kunne ikke knytte til databasen");
}
$sql = "SELECT Tittel, Tekst, COUNT(stemme.ForslagID) AS Antall FROM forslag, stemme WHERE stemme.ForslagID = forslag.ForslagID AND forslag.Aktiv = 1 AND forslag.Besvart IS NULL GROUP BY stemme.ForslagID ORDER BY Antall DESC";
$resultat = $db->query($sql);
$i=1;
$index=1;
while ($rad = mysqli_fetch_array($resultat, MYSQLI_ASSOC))
{
echo "<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js'></script>
<script>
$(document).ready(function(){
$('#beskrivelse".$index."').hide();
$('#visSkjulButton".$index."').click(function(){
$('#beskrivelse".$index."').toggle(500, 'swing');
});
});
</script>";
if ($i%2==0)
{
$tall="repeteres";
$ftall="forslag";
$sstall="stemmeOgSosialt";
$stall="stemme";
$sotall="sosialt";
}
else
{
$tall="repeteres2";
$ftall="forslag2";
$sstall="stemmeOgSosialt2";
$stall="stemme2";
$sotall="sosialt2";
}
echo "<a id='anchor".$i."'><div class='".$tall."'><div class='".$ftall."'><p><h3>".$rad["Tittel"]."</h3></p><div id='beskrivelse".$index."'>
<p id='forslagbeskrivelse'>".$rad["Tekst"]."</p></div><button class='btn btn-info' id='visSkjulButton".$index."'>Vis/ Skjul</button></div><div class='".$sstall."'><div class='".$stall."'>
<p><div class='btn btn-primary btn-lg center-block disabled'>".antallStemmer($rad["ForslagID"])."</div></p>
<br><br><div class='btn btn-primary btn-lg center-block'>Avgi Stemme</div></div>
<div class='".$sotall."'><a href='http://facebook.com' class='image1'></a><a href='http://twitter.com' class='image2'></a><a href='http://plus.google.com' class='image3'></a>
<div class='btn btn-warning btn-md' id='rapporterButton'>Rapportér</div></div></div></a>";
$i++;
$index++;
}
mysqli_close($db);
This function is called in a separate page, which is included in my index.php-page. My index.php-page also includes a navigation bar, assigning [mywebpage.com/index.php]?page=1 to the end of the url where the function is being called.
The issue I'm having is that adding for example "#anchor1" to this link does not make my browser window jump to the first (or any) element that this function gets from the database.
Can anyone please pont to where my error lies?
In case anyone else has issues wit this, the simple solution was to echo this:
echo "<a id='forslag".$rad["ForslagID"]."'><div class='".$tall."'></a><div class='".$ftall."'><p><h3>".$rad["Tittel"]."</a></h3></p><div id='beskrivelse".$index."'>
<p id='forslagbeskrivelse'>".$rad["Tekst"]."</p></div><button class='btn btn-info' id='visSkjulButton".$index."'>Vis/ Skjul</button></div><div class='".$sstall."'><div class='".$stall."'>
<p><div class='btn btn-primary btn-lg center-block disabled'>".antallStemmer($rad["ForslagID"])."</div></p>
<br><br><div class='btn btn-primary btn-lg center-block'>Avgi Stemme</div></div>
<div class='".$sotall."'>
<a href='https://www.facebook.com/sharer/sharer.php?u=localhost/TestM/index.php?page=1#forslag".$rad["ForslagID"]."' target='_blank' class='image1'></a>
<a href='https://twitter.com/home?status=St%C3%B8tt%20forslaget%20til%20NHHS%20her%3A%20http%3A//localhost/TestM/index.php?page=1%23".$rad["ForslagID"]."' target='_blank' class='image2'></a>
<a href='https://plus.google.com/share?url=nhhs.no' target='_blank' class='image3'></a>
<div class='btn btn-warning btn-md' id='rapporterButton'>Rapportér</button></div></div></div>";
In other words to just surround my first <div> with the anchor point.
i've passed some parameters from a view to a controller function. i see them in the url like : .../view/a1312014031
but i'm not able to use them in the function
here is the view
<a href="<?php echo base_url() ?>visiteur/view/<?php echo $id; echo $mois; ?>" class='btn btn-primary'>Mettre en paiement </a>
here is the controller function
public function view($id = '', $mois = '') {
......
$page = 'visiteur_liste';
$this->load->view('visiteur/' . $page, $data);
....
}
after clicking the link i get url with the parameters.../view/a1312014031
is there a solution to use those parameters in the function without using the session?
thanks for your time
Ok, then
You'l need to separate your parameter value in your hyperlink by "/", like:
HTML
<a href="<?php echo base_url() ?>visiteur/view/<?php echo $id.'/'.$mois; ?>" class='btn btn-primary'>Mettre en paiement </a>
PHP
public function view($id = '', $mois = '') { //function in CodeIgniter Controller (visiteur)
......
$page = 'visiteur_liste';
$this->load->view('visiteur/' . $page, $data);
....
}
ANOTHER WAY: PASS DATA USING GET PARAMETERS:
<a href="<?php echo base_url() ?>visiteur/view?id=<?php echo $id.'&mois='.$mois; ?>" class='btn btn-primary'>Mettre en paiement </a>
GET IN CodeIgniter
public function view() { //function in CodeIgniter Controller (visiteur)
$id=$this->input->get("id"); // or, $id=$_GET["id"];
$mois=$this->input->get("mois"); // or, $mois=$_GET["mois"];
}
try this
<a href="<?php echo base_url() ?>visiteur/view/<?php echo $id;?>/<?php echo $mois; ?>"
class='btn btn-primary'>Mettre en paiement </a>
I have this php code to populate with links:
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $result)
{
if($result['tip']=='1')
{
$camere=" - ".$result['camere'];
}
else
{
$camere="";
}
$view[]="
<tr>
<td>".$result['id_anunt']."</td>
<td>".$result['den_tranzactie']."</td>
<td>".$result['den_prop'].$camere."</td>
<td>".$result['judet']." ".$result['oras']." ".$result['zona']."</td>
<td><a href='#' id='".$result['id_anunt']."' onclick='view();'>View</a> <a href='#' id='".$result['id_anunt']."'>Edit</a> <a href='#' id='".$result['id_anunt']."'>Arhivare</a> <a href='#' id='".$result['id_anunt']."'>special</a></td>
</tr>";
}
I need to get the id attribute for "View" link on click it with this function:
function view()
{
alert ($(this).prop('id'));
}
but I receive this: "object HTMLinputelemnt".
How can I get the id?
Thanks a lot.
Put a Class on your link.
Eg:
<a href="#" class="viewLink" id="....
Then in jquery:
$(".viewLink").click(function() {
var id = $(this).attr('id');
alert(id);
});
Modify your
onclick='view();'
to
onclick='view(this);'
And the view function as
function view(link)
{
alert ($(link).attr('id'));
}
use
$view[]="
<tr>
<td>".$result['id_anunt']."</td>
<td>".$result['den_tranzactie']."</td>
<td>".$result['den_prop'].$camere."</td>
<td>".$result['judet']." ".$result['oras']." ".$result['zona']."</td>
<td><a href='#' id='".$result['id_anunt']."' onclick='return view(this);'>View</a> <a href='#' id='".$result['id_anunt']."'>Edit</a> <a href='#' id='".$result['id_anunt']."'>Arhivare</a> <a href='#' id='".$result['id_anunt']."'>special</a></td>
</tr>";
Javascript
function view(element){
alert (element.id);
return false;
}
OR
alert ($(link).attr('id'));
Make sure you pass the link object as a parameter to view(), like this: onclick='view(this);'
Then in pure javascript:
function view(el)
{
alert(el.id);
}
"Don't use a canon to kill a mosquito". -- Confucius