I am trying to delete a link without a page refresh, and I would like to jQuery to capture and send http request to a specific URL that does the deleting. But, for this to work, I need to be able to use jQuery/Ajax to get the clicked link and send it as an http request.
this is the link:
<a href='http://example.com/delete.php?id=243'> <span class='delete'>delete this</span> </a>
Now, this is the jQuery and Ajax in twig template to get the clicked link and send httprequest to delete.php with the id, so that id can be deleted.
$(document).ready(function() {
$(".delete").click(function(){
$.ajax({
type: "GET",
url: "{{ path('http://example.com/delete.php?id=243'}}",
cache: "false",
dataType: "html",
success: function(result){
$(".success").append(result);
}
});
});
}
But the above function only makes the page navigate to the link when clicked.
You need to preventDefault.
$(".delete").click(function(e){
e.preventDefault()
$.ajax({
..
I don't know if I got you right here, but this may help you: http://api.jquery.com/event.preventDefault/
$( "a" ).click(function( event ) {
event.preventDefault();
//your code
}
Prevent default behaviour of link using jQuery's preventDefault().
Having:
<a class='delete' href='http://site.com/delete.php?id=243'> delete this </a>
Try:
$(".delete").click(function(e){
e.preventDefault();
$.ajax({
type: "GET",
url: "{{ path('http://site.com/delete.php?id=243'}}",
cache: "false",
dataType: "html",
success: function(result){
$(".success").append(result);
}
});
});
Related
Hi I have function in javascript where I delete photos from database and from server. It work fine, I delete photo but this photo is still in my browser. I have question. How can I refresh only on javascript side?
container.addEventListener("click", function(e){
if(e.target.tagName == 'BUTTON'){
var id = e.target.dataset.type;
var r = confirm("Are You sure to delete?");
if (r == true) {
$.ajax({
type: 'POST',
url: 'index.php?r=gallery/deletep&name='+id,
dataType: 'html'
});
}
}
});
Now I treid to add
location.reload();
after ajax but I don't want to refresh all page. How Can i delete dynamically from browser?
OK in this type of situation you have two options. Either you load contents using AJAX request or remove the element from DOM either using class or id of that element.
So what happens is if you have loaded content using AJAX you can load all those contents again after successful deletion of image.
$.ajax({
type: 'POST',
url: 'index.php?r=gallery/deletep&name='+id,
dataType: 'html',
success: function(response) {
$(e.target).remove();
}
});
OR,
$.ajax({
type: 'POST',
url: 'index.php?r=gallery/deletep&name='+id,
dataType: 'html',
success: function(response) {
// use next ajax query to load content on DOM
}
});
you don't need to reload the whole page to remove the image. Use remove() API from jQuery in the success callback in AJAX call.
$.ajax({
type: 'POST',
url: 'index.php?r=gallery/deletep&name='+id,
dataType: 'html',
success: function(response){
$('img#id').remove();
}
});
I'm sure there's a simple explanation for this but I haven't been able to find the right words to use when searching for answers.
When users fill out the form .InvoiceForm it submits via Ajax. After it's submitted remove the .InvoiceForm class and add .UpdateInvoice. When a user submits a .UpdateInvoice form it explains that they are about to make a change and they have to click to say "Yes I want this to be updated".
The issue is that unless I refresh the page so that the form is loaded with the .UpdateInvoice form, I don't get the confirmation which means it's still submitting as a .InvoiceForm form. Is there anything I can do to fix this?
Edit to show code:
Code that runs if there's no record
$('.InvoiceForm').submit(function(e) {
$.ajax({
url: $(this).attr('action'),
type: 'POST',
cache: false,
dataType: 'json',
context: this,
data: $(this).serialize(),
beforeSend: function() {
$(".validation-errors").hide().empty();
},
success: function(data) {
$(this).removeClass('InvoiceForm');
$(this).addClass('UpdateInvoice');
$(this).find('.btn').val('Update');
$(this).find('.id').val(data.invoice_id);
$(this).find('.btn').removeClass('btn-default');
$(this).find('.btn').addClass('btn-danger');
$(this).find('.AddRow').removeClass('hide');
$(this).find('.invoiceDetails').html(data.returnedData);
$(this).parent().next().find('.grade').focus();
}
});
return false;
};
Code that runs if there is a record being updated
$('.UpdateInvoice').submit(function(){
var r = confirm("Are you sure you want to make this update?");
if (r == true) {
$.ajax({
url: $(this).attr('action'),
type: 'POST',
cache: false,
dataType: 'json',
context: this,
data: $(this).serialize(),
beforeSend: function() {
$(".validation-errors").hide().empty();
},
success: function(data) {
alert('This row has been updated');
$(this).find('.total').html(data);
}
});
} else {
}
return false;
});
The function for .UpdateInvoice doesn't run unless I refresh the page.
Thanks for your help.
You bind a click event on '.UpdateInvoce' before it even being created, hence it'll not work. I think you need to use .live() in order to make it works. See document here: jQuery's live()
HTML:
<button id="click_me" class="new">Click Me</button>
<div class="result" />
Script:
$(function () {
$('.new').click(function (e) {
$('.result').text("Im new !");
$(this).removeClass("new");
$(this).addClass("update");
// Bind UpdateInvoice's click event on the fly
$('.update').live(bindUpdate());
});
function bindUpdate() {
$('.update').click(function (e) {
$('.result').text("Update me !");
});
}
});
jsfiddle's demo
Firstly, there have some tag links in my main page. click each one, post value to b.php with jquery.ajax and turn back value in div#result.
b.php have a search box. when search something in it. the result data will still show in the div#result.
my problem is: I know if I will do jQuery ajax in the b.php, I shall write the jQuery code in the first success part. but this only can control one time, when I continue search in the search box, the jQuery not work. I think I met a loop problem. How to solve it?
a.php
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.click').click(function(){
var value1 = $(this).text();
$.ajax({
url: "b.php",
dataType: "html",
type: 'POST',
data: "data=" + value1,
success: function(data){
$("#result").html(data);
$('#search').click(function(){
var value = $('#search1').val();
$.ajax({
url: "b.php",
dataType: "html",
type: 'POST',
data: "data=" + value,
success: function(data){
$("#result").html(data);
}
});
});
}
});
});
});
</script>
<a rel="aa" class="click">aa</a>
<a rel="aa" class="click">bb</a>
<div id="result"></div>
b.php
<?php
echo $_POST['data'];
?>
<form name="form">
<input type="text" value="" id="search1">
<a name="nfSearch" id="search">search</a>
</form>
When a new element is introduced to the page the jQuery .click() method becomes useless because it can only see elements that were part of the original DOM. What you need to use instead is the jQuery .live() method which allows you to bind events to elements that were created after the DOM was loaded. You can read more about how to use it at the below link.
.live() – jQuery API
$('#search').live('click', function(e) {
// Prevent the default action
e.preventDefault();
// Your code here....
});
First of all i think you should attach the ajax call to the click on the link: the way you are doing right now just execute an ajax call as soon as the page is loaded.
$(document).ready(function(){
//when you click a link call b.php
$('a.yourclass').click(function(){
$.ajax({
url: "b.php",
dataType: "html",
type: 'POST',
data: "data = something",
success: function(data){
$("#result").html(data);
var value = $('#search').val();
$.ajax({
url: "b.php",
dataType: "html",
type: 'POST',
data: "data =" + value,
success: function(data){
$("#result").html(data);
}
});
}
});
});
});
In this way, each time a link with the class of "yourclass" is clicked an ajax call to b.php is sent and if it succed, another call is made (always to b.php). I don't understand if this is what you are looking fo, if you post your html my answer can be better.
In b.php of course you need to echo some html that can be used in the callback
It's strange how your attempting to do two ajax requests like that, surely one is enough. If you need to support multiple text boxes then you just adjust your selectors.
Your whole code can be shortended down to something like this:
$(document).ready(function() {
$('#result').load('b.php', { data: $('#search').val() });
});
So if you wanted to search for the value when clicking on a link (for links within #container):
$('#container').delegate('a', 'click', function() {
// .text() will get what's inside the <a> tag
$('#result').load('b.php', { data: $(this).text() });
});
i have this html:
cancel
when the user clicks cancel, i want to remove the hyperlink and i want to replace this with an image i.e.
$.ajax({
context:this,
type: "POST",
url: "actions/cancel.php",
data: "id=" + the_id,
cache: false,
success: function() {
$(this).remove;
// add image
thanks :))
Try:
success: function() {
$( "a#cancel_3" ).replaceWith( "<img...>" );
}
You'll have to fill in the HTML in the replaceWith function with whatever you want to replace the link with. is just a placeholder I used.
How about:
$(this).css('display', 'none').after('<img>');
I'm trying to run .ajax and insert a data element from the onClick of an item from the page. Whats the best way to do this?
Something like this:
function grabinfo(foo){
$.ajax({
url: "infospitter.php",
method: "GET",
data: "id="+foo,
success: function(html){
$(#showstuff).html(html);
}
});
}
<input onClick="javascript:grabinfo(18343)" />
// and on page each item will have this button input
It's usually best to keep your javascript unobtrusive, and out of the DOM.
<input type="button" value="18434"/>
In your javascript file:
$('input[type=button]').click(function(){
$.ajax({
url: 'infospitter.php',
method: 'GET',
data: 'id=' + $(this).attr('value'),
success: function(returnData){
// Do something with returnData
}
});
});
Remove the "javascript:" from your onclick attribute. That's only needed when running code outside a javascript event attribute.