I'm using the $.ajax call to send data to a PHP page:
$.ajax({
type: 'POST',
url: "ajax_more.php",
data: "userid=1"
});
In ajax_more.php I'm trying to read the value of the userid:
$user_id=$_POST['userid'] ;
However, I'm getting an error as PHP doesn't find a value for the index userid.
What am I doing wrong?
UPDATE
I'm sending another ajax variable in the same manner:
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("div#listednotes").append(html);
$("#more"+ID).remove();
}
});
and it is working fine, so using <?php print_r( $_POST ) ?>, the return value is:
Array ( [lastmsg] => 38 ).
You might have used some .htaccess redirection such as removing or adding the "www" to all web requests. Any change on those affects your POST request parameters.
To get this solved, assure you enter your Ajax url as it should be according to your .htaccess rules.
$.ajax({
type: 'POST',
url: "ajax_more.php",
data: {"userid" :1}
});
I cut and pasted your code exactly and it works. So it doesn't look like you are doing anything wrong in the code provided. If you are running other code before checking the $_POST array, that code could be altering its contents or unsetting it.
Your data should be in Key value pairs. and not the way you specified it.
So:
data: "userid=1"
is wrong, should be:
data: {"something" : "value"}
Related
I am currently trying to pass a json object from ajax to php. But it does not get me anything..Below is my code. I already tried print_r[$_POST] it just returns Array(). What's wrong with this code?
This is my code: screenshot of my code
If ajax is sent with "GET" instead of "POST", you should use $ _GET in your code.
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
Are you sure that the Type parameter is post?
or use
print_r($_GET);
I have an Opencart website, I am currently trying to use ajax at the frontend to pass data to php controller in the backend, but I am unable to get the value from the request in backend
here is the frontend ajax code:
$.ajax({ url: 'index.php?route=checkout/cart/addAll',
type: 'post',
data: 'product_list= test' ,
dataType: 'json',
success: function(json) {});
at the backend controller, I am trying to retrieve variable "product_list" but it is not working
$products = $this->request->post['product_list'];
$logger->write("products to add to cart is"+ strval($products));
the last statement keeps printing 0 to the log file
any help with this ? what is wrong here?
I also tried
$products = json_decode($this->request->post['product_list'], true);
with same results
Ok, fixed, Ajax was not the issue, it was accessing the variable from server side, so I used $_POST
instead of $this->request->post and it is working fine
So I just did this
in file catalog/view/theme/defaulttemplate/common/home.twig I add this code at the end of the file
$(document).ready(function() {
$.ajax({url:'index.php?route=checkout/cart/addAll',
type: 'post',
data: 'product_list= test' ,
dataType: 'json',
success: function(json) {}
});
});
and in file catalog/controller/checkout/cart.php on line 479 I add this
public function addAll(){
print_r($this->request->post);
}
And I see this in my console http://joxi.ru/krDlvPdfKGejar
All I did was correct your js code. hope this helps.
I'm trying to change content of chatt.php file on server, using ajax procedure.
$("#btnsend").click(function(){
var a = $("#write").val();
console.log(a); // that's ok
$.ajax({
type: "POST",
url: "ajax.php",
data: a,
dataType: "json",
success: function () {
alert("323");
}
});
});
ajax.php
$a = $_POST["a"];
$b = "chapters/chatt.php");
file_put_contents($b, $a);
There is no alert, chatt.php is not changed, console is empty.
Any help?
First of all add php error_reporting().
Error Reporting Manual
In php you are getting the Undefined index notice for $_POST['a']. You need to pass a from ajax as:
Modified code:
$("#btnsend").click(function(){
var a = $("#write").val();
console.log(a); // that's ok
$.ajax({
type: "POST",
url: "ajax.php",
data: "a="+a,
dataType: "json",
success: function () {
alert("323");
} });
});
As my other mate mentioned in comments if you solve this issue you will face an another issue like Parse error for this line:
$b = "chapters/chatt.php"); // unexpected bracket
Side note:
Keep in mind error_reporting() is only for development not for productions.
You aren't sending correct data, try:
data: {a: a}
You never bothered naming your data parameter, so $_POST['a'] doesn't exist. PHP expects key=value for POST data, and you're sending over a bare value.
Try
data: { a: a}
^--key
^---value
instead.
And note that you're opening your server up to a total remote compromise. If this chatt.php is inside your site's document root, a malicious user can use your code to write ANY php code they want to the file, and your server will happily execute it for them.
When I send serialised data to a PHP file using ajax, it is sometimes URL Encoded depending on how i do it.
Originally i had the following code which worked fine:
$.ajax({
type: 'POST',
url: 'ajax-process.php',
data: $("#sitestructure-form").serialize(),
success: function(d){$("#structureupdate").html(d);}
});
The data was sent to my PHP file and i could echo it and it looked like this.
[{"id":20,"children":[{"id":21}]},{"id":19},{"id":18,"children":[{"id":14}]},{"id":16},{"id":13,"children":[{"id":11}]},{"id":17},{"id":15},{"id":12}]
I wanted to send more than one piece of data, I called the serialized data 'order' and added 'process' to it so i updated my code to the following:
$.ajax({
type: 'POST',
url: 'ajax-process.php',
data: {
order: $("#sitestructure-form").serialize(),
process: "sitemap-reordernavigation"
},
success: function(d){$("#structureupdate").html(d);}
});
However when I retrieve the serialised data sent in 'order' the output looks like this:
data=%5B%7B%22id%22%3A20%2C%22children%22%3A%5B%7B%22id%22%3A21%7D%5D%7D%2C%7B%22id%22%3A19%7D%2C%7B%22id%22%3A18%2C%22children%22%3A%5B%7B%22id%22%3A14%7D%5D%7D%2C%7B%22id%22%3A16%7D%2C%7B%22id%22%3A13%2C%22children%22%3A%5B%7B%22id%22%3A11%7D%5D%7D%2C%7B%22id%22%3A17%7D%2C%7B%22id%22%3A15%7D%2C%7B%22id%22%3A12%7D%5D
The only way i can think of to fix this problem is to use php to urldecode it and then use str_replace to remove the 'data=' bit at the front, like so.
$data = str_replace("data=","",urldecode($_POST['order']));
How can I get this to work with AJAX though so i dont have to urldecode it?
Ive tried using a variable and setting the processData to false however that didn't seem to work.
var order = $("#sitestructure-form").serialize();
$.ajax({
type: 'POST',
url: 'ajax-process.php',
processData: false,
data: {
order: order,
process: "sitemap-reordernavigation"
},
success: function(d){$("#structureupdate").html(d);}
});
My knowledge of AJAX/Jquery is rather limited so any help would be greatly appreciated.
That's because you are feeding a serialized text string to the data attribute the first time around and jQuery does not convert it to a serialized string. The second you are assigning an object to the data attribute with the "order" attribute having the serialized text string, so jQuery basically double encodes it. For it to act as you'd like you would have to convert the form to an object and assign your "order" attribute to that object. See this post: Convert form data to JavaScript object with jQuery
// taking the example from the above link, you do this instead
order = $("#sitestructure-form"). serializeObject();
Fixed by doing the following:
$.ajax({
type: 'POST',
url: 'ajax-process.php?',
data: $("#sitestructure-form").serialize() + "&action=sitemap-reordernavigation",
success: function(d){$("#structureupdate").html(d);}
});
I got the error:
request failed: URI too long (longer than 8190)
I've seen other posts on StackOverflow for this one. Those posts recommend:
Not changing Apache settings (agree)
Using post, not get
Not using jsonp with post
I'm using jQuery's AJAX to POST:
$.ajax({
url: "test.php",
dataType: "json",
data: paras,
type: "POST",
success: function(ret){callback(ret);}
});
It's my impression you can use json just not jsonp. Correct? If so, why might I still be getting the error?
You should try setting proccessData to false.
From the docs:
By default, data passed in to the data option as an object
(technically, anything other than a string) will be processed and
transformed into a query string, fitting to the default content-type
"application/x-www-form-urlencoded". If you want to send a
DOMDocument, or other non-processed data, set this option to false.
so to prevent the data being added to the url:
$.ajax({
url: "test.php",
dataType: "application/json",
data: paras,
type: "POST",
proccessData: false, // this is true by default
success: function(ret){callback(ret);}
});
Honestly, I thought this was automatic, but since your url is too long it's worth a shot.
I ran into this issue when using jQuery to submit large forms, and was able to solve it by adding this plugin.
For example, using the following code to submit the form after adding the plugin resolved the issue for me:
$(formSelectorHere).ajaxSubmit({
url: myURL,
type: 'post',
contentType: "multipart/form-data",
data: $(this).serialize(),
success: function(data) {
function(data) {
//success code here//
}
});
If you're not using this to submit a form, this may not be relevant to you and won't solve your problem, but that's the most common situation where this issue appears, so I figured it was worth mentioning. (The plugin should also be able to submit a form using JSON, but haven't personally tested it).