OK, If you need me to post any more of my code I have come up with then let me know. I am not sure this is even possible. But what I want to do is stop PHP from making a Zip file when the user selects a cancel button / link.
Now what I have set up is all the files upload per user are listed in a form, the user then can selected which files they wish to zip and download. Once the files are selected and the download button clicked, the div holding the form is changed via a Jquery AJAX call, which works fine no issues.
But I have a 'Cancel' / 'Stop' button / link which sort of works. Once clicked I call the .abort on the AJAX request and then loads new HTML content saying the user cancelled the zip making progress. However, all the message works fine, it seems like the server is still making the zip in the background and any links will not respond (most are also AJAX requests to load in new users requests) until the zip has been made.
The base of my site is using the CakePHP framework. But I am sure this is just standard Jquery / PHP issue.
Any ideas on how to get my AJAX request to stop PHP from making the zip?
This is how I am building my AJAX :
$(document).ready(function() {
$(".MAKEMYZIP").click(function(e) {
e.preventDefault();
$('.DIV-HOLDER').load('/PAGE-PREVIEWER');
var MakeNewZipFile = $.ajax({
url: '/makezip',
type: 'post',
dataType:'html', //expect return data as html from server
data: $('.FILE_DATA').serialize(),
success: function(response, textStatus, jqXHR){
$('.DIV-HOLDER').html(response); //select the id and put the response in the html
},
error: function(jqXHR, textStatus, errorThrown){
console.log('error(s):'+textStatus, errorThrown);
}
}); //End of AJAX call
//Below line makes the 'text' link visible once the make zip but clicked.
$('.ZipLinkHolder').css("visibility","visible");
$( ".Can_Zip_This_Zip" ).click(function() {
MakeNewZipFile.abort();
$('.DIV-HOLDER').html('<br/><br/><div class="CenterTxt">User has canceled Zip making process.</div>');
});
});
});
Related
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.
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.
Im creating a pseudo payment system where the user completes a form then the form redirects him to an external url to complete the checkout process.
When the user submits the order details i want them to be passed along to the external url so i can display them sort of like a receipt.
Question
What's the best way of doing it?
Instead of loading the page in iframe i would recommend you to do an ajax call to the external url (as you have control over it).
Here is the ajax for your operation
$.ajax({
type:'POST',
url:'http://alexanderkap.esy.es/yourprojectfolder/file.php',
crossDomain: true,
data:'yourdata=anyofyourdata',
success: function(data){
console.log(data);
},
error:function(data){
console.log(data);
}
});
Note :
You shall use any click event to call this ajax
To send your entire form data you shall use form serialize
I have just put the success and failure case in the console which you shall do that in a div
I have a php page where I display few images with radio buttons. On start two images are pre selected and I merge those and show the combinations. But user can choose a different image and click on MergeImages button, which should display the combination of two images selected.
I am doing this in one php file, where I pass the selected contents via POST to the same file through AJAX.
$.ajax({
url: 'CreateImage.php',
type: 'POST',
data: {imagename : newImageName},
success: function(data) {
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
I am getting a success and now I would like to display the data so that I can see the newly created images. How do I do this?
You'll want to send with the ajax call the "dataType: 'image'," so the ajax call knows what to do with the returning data -- other options that I've used are dataType:'json' or 'text' or 'html', etc.
http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests
actually it won't take anything but a string, so send back the image src to display. (I was thinking you wanted to return the actual image itself).
Since you are running AJAX, which uses HTTP protocol for communication, HTTP relies on MIME type. Identify the callback by using 'image' as dataType.
dataType:'image',
If you want to display the data in a DIV, you can do this:
html:
<div id="myemptydiv"></div>
javascript:
$("#myemptydiv").append(data);
php (if you are in the same file, you have to clear the buffer 1st but you should use another php file for ajax requests instead):
<?php
if(isset($_POST['imagename'])){
while(#ob_end_clean());
//do stuff here
echo $data;
exit;
}
?>
My adventures continue...
On my page, I want to display a registration form in colorbox, allow the user to submit the form which is processed by a php script and then display a thank you style message in the colorbox which the user will then close.
At the moment I have the processing script in the same page as the form and this works on it's own outside of colorbox.
I've seen similar questions here that suggests to post the form using an ajax call
$('form').live('submit', function(e){
var successHref = this.action,
errorHref = "formError.php";
e.preventDefault();
$('#cboxLoadingGraphic').fadeIn();
$.ajax({
type: "POST",
url: "processForm.php",
data: {someData: $("#someData").val()},
success: function(response) {
if(response=="ok") {
console.log("response: "+response);
$.colorbox({
open:true,
href: successHref
});
} else {
$.colorbox({
open:true,
href: errorHref
});
}
},
dataType: "html"
});
return false;
});
I'm a bit confused with this....
I think I'm ok with sending my form via $,ajax (although any clarity appreciated) but I'm not clear how to handle the response from the form. What do I need my php script to output (and how) so that a thank you message is displayed? Is it simply an echo statement from the php script?
Should I also separate out my processing script from the form )i did it this way as I kept getting path errors and it was easier at the time.
Thanks
Success!
I've managed to get it to work - all logical in the end - shame I'm not very logical..
First move was to separate the php form process script out into a separate file.
Colorbox link to the form all works ok
Used an ajax post to the php script and waited for a message back from the php script.
The php script produces two html messages depending on success or failure which are then displayed in the colorbox by replacing the form html in its div
Yay!