If ajax call ok than a href not working or if i stop ajax call than href ok, But i want both work on click
View
Ajax Request
$("body").on("click", ".btnad", function(e){
e.preventDefault();
details_id = $(this).attr('id');
$.ajax({
url: 'assets/php/process.php',
type: 'post',
data: { details_id: details_id },
success:function(response){
data = JSON.parse(response);
$("#getID").text(data.id);
$("#getTitle").text(data.ad_title);
$("#getUser_coin").text(data.user_coin);
}
});
});
If I understand correctly, you want to initiate an AJAX request and follow the link. You can't reliably do both an an asynchronous thing (ie an AJAX request) and still allow the browser to follow a link to a new page.
Instead, you should drop your use of AJAX, and instead allow your browser to perform a normal request to assets/php/process.php, but hae that page redirect the browser to the page you want to show next.
Related
I have different links on my website which lead to other websites. I want to count the amount of clicks on the different links with my MySQL database.
I thought about doing a redirection over a php file which adds it in the database, but I would prefer to not redirect the user. Is it possible that the user just clicks on the tag gets immediately to the external website, and I can still count the click?
Thanks for you answers,
Till
yes you can run an ajax request and when the request is completed then you can redirect user to where ever you want to.
in the ajax request you will count the clicks and save it to the database.
you can do something like
$("a").click(function(){
$.ajax({url: "scriptthatwillcountclick.php", success: function(result){
window.open('redirect_here');
}});
});
Use ajax.
var data = {link:"value"};
$.ajax({
url : "your_file.php",
type: "POST",
data : data,
success: function(data, textStatus, jqXHR) {
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("error");
}
});
You can run this code inside .click function when the link is pressed.
And in your php file just increment the count.
You can use jQuery Ajax funcionality to accomplish your goal, just create an on click even for your "a" element and execute your Ajax to save the click information on your database.
$('a').on('click', function(){
$.ajax({
method: "POST",
url: "save-my-click.php",
data: { 'extrainfo' : "Some extra info" }
});
});
Do not return false, or event.preventDefault() on your click event, because it will brake the redirection.
UPDATE:
You don't need to set the success and error functions because it is going to redirect anyway and you are not going to handle them.
I wonder tho, how can I embed an http request url like myurl.php in an html form and then push the values onto the server using buttons
So I want to put (url.php), but I don't want the page to redirect to the url
Each time I press submit. I just want the data to get to the specified server.
And no I do not own the website in question.
You can do this by ajax use this sample code
$("formId").submit(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: 'url.php',
data: $("#formId").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
})
});
or you can use Ajax Form plugin
I have a question. I'm using a Fancybox instance to show categories of tags. In this fancybox, users can edit the categories, delete them or create new ones.
The script that is executed in the fancybox is called with AJAX. That means that a PHP-file (called categories.php) is being executed while the browser doesn't redirect (it stays on tags.php). And here's where the problem kicks in:
I have a form on categories.php. If the user presses submit, then jQuery cancels the default event and send an AJAX request. This AJAX request should be send to this code (e.g. the code in categories.php).
How can I do that?
$(document).ready(function() {
$('form').on('submit', function(event) {
// cancel the default event (i.e. POST the form)
event.preventDefault();
var postData = $(this).serialize();
var postUrl = this.href;
var createCategory = $.ajax({
url: postUrl,
type: 'POST',
data: postData
});
createCategory.done(function(result) {
// code to reload the page
// .replaceWith()?
$('.testdiv').append(result);
console.log('done');
})
});
});
Should the postUrl be $(this).href ? or Should I change that to point to the categories.php?
I think you should try to point at categories.php.
I’m facing a situation with Ajax request on back button.
I created a form which posts values and returns results via ajax request according the given filters and loads on a specific div. When I click on any of the link on page, it opens the new url as expected. On clicking on browser back button it opens the previous form page with default values. How can I enable browser state functionality so that I have results with last posted values with browser back button. Note that type of ajax is POST.
One solution I got is that to modify this form type to GET instead of POST, but this would take much time to do changes in server side code.
var page_url = $(this).attr('href');
page_url = page_url.split(':');
var page = page_url['1'];
$form = $('#form);
$.ajax({
type: 'POST',
data: $form.serialize(),
url: webroot + 'controller /action/page:'+page
}).done(function (result){
})
I want to know the possible solution.
You should set cache to false:
$.ajax({
dataType: "json",
url: url,
cache: false,
success: function (json) {...}
});
Source https://stackoverflow.com/a/25230377
you can use Jquery .unload() function or try cookies you could read it from here How do I set/unset cookie with jQuery?
Once you submitting that time dont go for new page, just hide the form elements and display new content in other div. Once you click on back button just show previously hidden div and hide current showing div
I am attempting the following:
When a user clicks a link, I want to update a specific column of a specific table in the database via ajax. BUT I still want the user to be redirected to the href="<url>" of the link.
I tried jQuery without return false; but then the ajax doesn't work.
I tried with return false; but then the page obviously doesn't redirect to the url as I wanted.
Thanks in advance for your help.
Do your AJAX call, then set document.location when done.
$('a').click(function(e){
var href = this.href; // get href from link
e.preventDefault(); // don't follow the link
$.ajax({
url: '/path/to/site',
data: {some: data},
success: function(){
document.location = href; // redirect browser to link
}
});
});
You need to serialize these actions yourself. In the event handler first run your AJAX request, then lookup the href from the tag and use a location redirect to take the client to that URL.
Put the redirect code in the success callback:
$.ajax({
...
success: function(){
$(location).attr('href',url); // You URL inserted
}
});