Need to get id attribute - php

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

Related

can't delete record in codeigniter using href

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");
}

How to echo a href links with onclick alert php

Problem
When i click this link its alert won't open what should I do ?
My Code
echo "<td><a href='#' onClick='alert('Not an Enumerator!')'>Location</a></td>";
You have to escape quotes \' when necessary, try this:
echo '<td>Location</td>';
Try By Following Example.
<a href="#" onClick='alert("Not an Enumerator!")'>Location</a>
Don't Use Single quote inside alert
Try below example you can do both alert and onclick or either based on return type of function called on click.
<?php
echo "<td><a href='test.php' onclick='return test_click();'>Location</a></td>";
?>
<script type="text/javascript">
function test_click(event){
alert("Inside this function");
return true;
}
</script>

redirect after click button delete

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.

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.

Categories