Jquery - Getting 403 error when submitting form via ajax - php

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!

Related

Calling a function in php with link in html

I'm trying to call a function with a link in html. I found the following example:
click to run function!
if(isset($_POST['runfunction'])){
}
This works perfectly fine, the problem is that when I click the link, "?runfunction" keeps standing in my url bar. So when I submit a form on my page it goes totally wrong (it's way to long to upload here). I do some SQL queries and I'm getting weird values in my SQL database. When I type in just my normal url it works fine. So I'm pretty sure that's the problem. I found another example with ajax :
$("a").click(function(){
jQuery.ajax({
url: "path/to/controller",
type: "POST",
dataType: 'json',
data: {'mentod':'ExportExcel'},
success: successCallback,
error:failureCallback
});
});
I don't fully understand this example (because I never use AJAX) but my php script is included in the html page "include("")". So I can't type in url because it has to be the same page. Can someone give a little bit of info about this, or give an example of how I can fix this? Thanks in advance!
You can add a callback method then remove it from the url by javascript
function successCallback () {
url = window.location.href;
window.location = url.replace("runfunction", "");
}

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.

jQuery Mobile form submission over multiple pages with CakePHP

I hope I am missing something simple here. I have a CakePHP web site I am using jQuery mobile with. I think CakePHP might have something to do with it, but I am not sure.
Anyway, I have a form I've created on my view page for adding comments. The Ajax call is working as expected on the first page that loads, but navigating to any other page prevents the data from being submitted. The console still logs 'data' each time I press the button (after using 'pagebeforeshow' as recommended somewhere else), however it seems to be the data from the original loaded page (I know this because I am currently debugging $this->request->data on the Form action page).
Clearly, I must need to "reset" the form somehow when moving across pages, but I am not sure if this is possible without refreshing the page. I do know about "data-ajax"="false" and "rel"="external" which can be used as a last resort, but I want to avoid refreshing the page if I can.
Any suggestions? Thank you.
Here is the JS I am using for the Ajax call
//<![CDATA[
$(document).on('pagebeforeshow', function(){
$(document).off('click', '#comment_add').on('click', '#comment_add',function(e) {
$.ajax({
async:true,
data:$("#sCommentViewForm").serialize(),
dataType:"html",
success:function (data, textStatus) {
//$('#comments').remove();
//$('<div id="comments"></div>').appendTo('#comments_container');
$("#comments").html(data).trigger('create');
//$('#comments_box').remove();
//$('<div id="comments_box"></div>').appendTo('#comments_container');
console.log(data);
},
type:"POST",
url:"commentsUsers/comment_add/<? echo $template['Template']['id']; ?>"});
return false;
});
});
//]]>
</script>
It was my basic lack of understanding. After lots of searching this simple post was most helpful:
Jquery Mobile Javascript not working on ajax load
Basically, I was using IDs for everything - when I switched to class names it was smooth sailing.

ajax call to php function without changing url

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!

How to update php variable with javascript

Not sure of I should be looking for, here is what I am needing to accomplish.
I'm creating a stock trading simulation game.
I have created a php function that appears in my page code as (where AAPL is the symbol):
<?php displayQuoteDetailed(AAPL); ?>
I would like to have a text input and a submit button located in my sidebar to change the symbol on the php function to display quote without the entire page reloading.
From my google searching, it seems this could be accomplished by using javascript but I have been unable to locate exactly how to change the symbol used in the php function and how to get the stock quote to update without page reload. Thanks for any help! I'm learning as I go.
You need to use AJAX (XMLHttpRequest unless using IE). If you're new and want your app to work across most web browsers use jQuery:
http://api.jquery.com/jQuery.post/
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType
});

Categories