ajax call to php function without changing url - php

I am working with Symfony2 and I am using Dropzone.js with the bundle OneUploader to upload several images at a time.
Dropzone.js has the option of deleting an image that has been selected to upload, but the problem is that when I click on 'Remove' the image is only removed on the client side, not on the server. So what I need is to make an ajax call in mi dropzone.js to a function in my php that deletes the file from the server. The question is: how can I make that call in order that the url is not changed? I am using this one but it is not working, because the url is changing:
$.ajax({
url: 'delete.php',
method:'POST',
data: {name: file.name}
});
Thanks in advance.

Finally I was doing something wrong... I was making the ajax call in a .js file, so I could not use the path form in the url in the ajax call (twig language). So I have solved it making the ajax call in a .html.twig file and using a path in the url, that calls a routing, and it calls an action in a Controller that makes the deletion.
By the way, thanks for your help!

Related

Where should I place my php script file for AJAX request in wordpress?

I have a form in my index file in my theme folder.
I also have the script called find-locations-ajax.php in the same theme folder which Im trying to call. When I try to call the script file it keeps loading the entire site.
Here is my code
jQuery(function($) {
$.ajax({
url:'find-locations-ajax.php',
type:'POST',
data:querystring,
success: function(msg){
}//ends success code
});//ends ajax
})//ends submit handler
});//ends ready function
First time working in wordpress so please go easy on me.
EDIT. This is not for ADMIN functionalities
New edit:
You're doing AJAX a bit wrong for WordPress.
WordPress got a file to handle all of the AJAX requests, called admin-ajax.php and actions, to which you should bind your code to process these requests.
I suggest you read up documentation to get some understanding.

How can I check how many times the local HTML document was opened?

So briefly, assume that we have a standalone HTML document file, that is sent to recepient.
Is there any solution (probably with using 3d-party counter services) to check that the document was opened. And it would be better if that action was hidden from user.
As I suppose placing some PHP script on "friendly" server and making AJAX calls to it is useless because of cross-site request. Or am I wrong and this can be fulfilled? Really I'm not the best in it.
Any ideas? Please help, and thank you.
* The simpliest way is the best.
EDIT: notifying "action" should be performed, when user has viewed a certain section of the document. Generally, if some element was clicked
The usual solution is a "web bug". You put a 1-pixel image on the page, whose src points to your server. Include a unique ID in the URL, something like:
<img src="http://yourserver/images/empty.jpg?id=123456789" height="1" width="1">
You can then scan your log to see if the image was accessed, and use the id parameter to associate it with a specific recipient. You should disable caching so that every time they open the document they'll have to download the image again.
I think AJAX calls should work fine using jQuery crossDomain setting
$.ajax({
crossDomain:true,
type: "POST",
url: "http://domain.com/ajax.php",
data: {
var1: var1,
var2: var2
},
success: function(data){
}
});
http://api.jquery.com/jquery.ajax/

How to upload a file from a page called already through Ajax?

The form upload works if not called through Ajax first, but if the tab/page is loaded like
$.get(page+".php", {type: type}, function(d){ $('#mainContent').html(d); });
Then the Malsup jquery plugin doesn't work and will redirect to the url listed in the forms action section.
How do it get it to work the same way if I was directly on that page?
I believe on success of ajax request you have to re-bind Malsup plugin so that the DOM gets updated. You would have to add the rebinding in the success handler.This may solve your problem.

Jquery - Getting 403 error when submitting form via ajax

I have a php page called form.php. When you go to this page it shows a form, when that form is submitted I have jquery code that posts the form to itself via ajax. This works for other pages, however this specific page/code is giving me a 403 error when the page is submitted. The main difference is the other pages that work are located in a sub folder while form.php is in the root directory. Here is the jquery code:
$.ajax({
type:'POST',
cache:false,
url:'/form.php?action=add',
data:formData,
dataType:'json',
success: function(data) {})
});
I understand this is a server error, my question is why would it fail pointing to itself via the root folder but not fail if posted to a page in a sub folder?
try using
url:'.../form.php?action=add',
use the full URL to the file http://blah.com/form.php.
Ok I figured it out. One of the fields I was storing 2 variables in one value like so:
1<>2
I guess the <> part was triggering some issues, once I removed that it worked without any issues. Thanks everyone for the help!

Execute a PHP function from image OnClick event

I have a PHP class in which there is a delete function. When I click on the image I want to call delete, but this line is not working:
echo "<img src='icon/del.gif' onClick='.delete().'/>"
I have tried using the href tag but it doesn't work.
Considering you can use jQuery, you need to do the following:
first give your img a class, for example:
<img src="/icon/del.gif" class="delete" />
then bind this using jquery:
$('.delete').click(function(){
$.ajax({
type: 'POST',
url: '/url/to/your_php_script.php',
data: {'id':'45'}
}
});
});
your php file gets executed and $_POST['id'] gets send to it.
does this help? can you do the rest yourself? if not, give us more code :)
Create cross browser XHR.
Request a URL which calls delete().
Repeat with me: JavaScript runs on the client; PHP runs on the server.
PHP can echo JavaScript and JavaScript can make HTTP requests.

Categories