I'm starting a web programming, because so far I used only to web design.
My problem is that I´ve done a survey that show a form with 12 questions per department. The number of departments depends on a previous selector called "sections".
Thus, the form can show 12 questions fields if "section" has 1 department, or 120 if "section" has 10 departments.
I serialize and encode data to Base64 before submit:
// Setup call
$.ajax({
type: 'GET',
url: 'survey_actions.php&action=save',
data: { parameters: Base64.encode($("#mySurvey").serialize()) },
dataType: 'text',
beforeSend:function(){
// Load waiting panel
loader("on");
},
success:function(response){
[...]
I´m sure this is not the clean method and for that I am experiencing the following problem: when the number of fields to fill is low (for example 12), all works OK, but when the number is higher (for example 72), no all data is received by server.
The fields are named as follows: "sectorID_questionID". So on post, params string is:
...&1_0=X&1_1=X&1_2=X&1_3=X...
(X is the value that the user has given to the question).
So the question: what is the most appropiated way to solve it and send huge data to the sever using AJAX without get it trimmed?
Sincerely, thanks for your help :)
Change the ajax type to POST, rather than GET and get the data from $_POST. Your data could be going above the maximum query string length.
Ajax can only take data in a query string. So, if large data is not being sent properly, it may be a limitation of your server/application.platform. You may very well look into increasing the max_input_vars. And also make sure you use POST and not GET
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
Just use the above syntax an I don't think you need to serialize your data
Related
I'm not the best to explain problems but i'll try my best.
So i'm developping a website with php and javascript and bootstrap.
I got a list of equipment that i want to be able to edit with a modal form.
What i'm doing is that i write the id of the equipment (from mysql) into the id of the picture that i'm supposed to click to edit the equipment.
Like this
When i'm clicking on it it goes to a JS page that execute an ajax request to get all the infos from the database.
Now the main problem: when i transfer the id with the ajax request (with $_POST) and i var_dump it to see if i really have it it shows like that: Array ( [1] => )
The number is actually the ID but i have no idea how to use it as a string to get my infos.
If I get you right, your question is "How can I fix the ajax request to process it in PHP?"
Without seeing the specific code-part that you need help with, it's difficult to tell what exactly needs to be changed.
From your description, I assume that your Ajax request in JS looks like this
// your code is either this:
jQuery.post( 'https://example.org?' + id );
// or this:
jQuery.post( 'https://example.org', id );
This will give you the URL https://example.org?1, which is equal to the URL https://example.org?1= (i.e., param "1" is an empty string)
You need to give the ID a param name. Like this:
// This will work (add the string before the parameter value "id=")
jQuery.post( 'https://example.org?item=' + id );
// This is even better - set the post data as object:
jQuery.post( 'https://example.org', { 'item': id } );
In PHP you can then access the product id via $_POST['item'].
Of course, you can use any other param instead of "item" and even pass multiple values inside the post object.
Update based on new comment
The problem is, that you only pass the ID to the post-request, without assigning a name to it: data: id
Change this to data: {item: id} and you have $_POST['item'] in PHP. Here's the full code:
$.ajax({
url: "getInfos.php",
type: "POST",
data: {item: id}, // <-- change this line!
success:function(response){
$('#bodyEdit').html(response);
$('#myModalEdit').modal('show');
},
error:function (resultat, statut, erreur){
console.log(resultat, statut, erreur );
}
})
EDIT: the answer above works better, it seems i've got more than one problem for this issue and the answer above helped me correct it for good
thanks to everyone that answers my question,
i found out about the problem.
First i was the ajax function built in jquery:
$.ajax({
url: "getInfos.php",
type: "POST",
data: {item: id}, // <-- change this line! credit to Philipp
success:function(response){
$('#bodyEdit').html(response);
$('#myModalEdit').modal('show');
},
error:function (resultat, statut, erreur){
console.log(resultat, statut, erreur );
}
});
I was sending the id with data: but the value was missing .serialize after it, wich adapt the value to the url query if i understood correctly.
working code:
$.ajax({
url: "getInfos.php",
type: "POST",
data: id.serialize(),
success:function(response){
$('#bodyEdit').html(response);
$('#myModalEdit').modal('show');
},
error:function (resultat, statut, erreur){
console.log(resultat, statut, erreur );
}
});
I'm New in AJAX.
When passing ID to update product. Any explanation for this. Thank you in advance.
This
$.ajax({
type: 'post',
url: 'my_controller/update_product_exe/' + id, //This line
dataType: 'json'
});
to this...
$.ajax({
type:'post',
url: 'my_controller/update_product_exe',
dataType: 'json',
data: {id: id} // this line
});
The difference is the url itself. Appending id to the first url will change it and therefore send the request to that specific url. But, it's not sending any data during request. Example:
// let's say id = "1234"
$.ajax({
type: 'post',
url: 'my_controller/update_product_exe/' + id, // This will be 'my_controller/update_product_exe/1234'
dataType: 'json'
});
And for the second one:
$.ajax({
type:'post',
url: 'my_controller/update_product_exe',
dataType: 'json',
data: {id: id} // This will be {id: "1234"}
});
On the second one, you are passing data; on the first one, you are just modifying your url by appending some string to it.
If you just want to know about the difference in both ajax requests than:
In first request, you are not passing the data in ajax request but sending an ID in URL, in CI controller, you will get this id by using URL Segments.
In Second request, you are sending the data in ajax request, so you can get the data in controller by using $_POST
Now, which one is the better, both of them having difference, when you need to pass some input values using ajax than you can choose second one. You can send multiple data in this request.
You can also use second request for achieving the first request target, in this case you can just pass the ID in ajax data. You can send multiple data but you must need to take of segement URLs.
Conceptually you are using a GET in the the first example and a POST for the second. HTTP verbs have a meaning and POST is meant to send information to a server. Even if you can get the id by using a GET this does not make it semantically correct. For the moment you only have an id which is limited in size and is only one parameter, but even in a small application one usually sends to a server several parameters and maybe some kb of data. GET parameters are limited in size and POST is better suited for this.
For all this reasons the second version that is using POST is the correct one.
Here are some extra resources on the differences between GET and POST.
http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post
http://www.diffen.com/difference/GET-vs-POST-HTTP-Requests
What is the difference between POST and GET?
When should I use GET or POST method? What's the difference between them?
I have a Javascript variable that is an object with keys and arrays (shortened version shown below). I scripted in HTML a form so that a users with access (after they login) can delete, add, or alter any of the Keys for their genealogy. What I need to know is how to take this new, altered variable and save it to the server as say, userNameGenealogy.js so that another page can use it. I have done some searching, but do not know if what I need is only PHP oriented, or if I will need to look into AJAX or JSON as well.
var genealogy = {};
genealogy["Johnson"] = {
"Ron": new Array(
{"nickname":"Ronny","dob":"06/20/88"},
{"nickname":"Ronald","dob":"03/15/54"}),
"Scott": new Array(
{"nickname":"Scotty","dob":"01/21/42"})
};
This is just a short snippet, but I'd prefer not to have to change the format of the Javascript, as the rest of the site is coded to work with it as is. Any help / point in the right direction would be greatly appreciated.
You need to use ajax for that. For example with ajax() and pass data parameter to it. Then you need to handle that in PHP script in which you have to read $_POST (in case below) parse it as you like it, save to file and echo response.
JavaScript code:
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
I am sending form data with 2k+ parameters, but server only recieves less than half of it.
$.ajax({
url: 'inner.php?option=param',
type: 'POST',
data: $('#form').serialize(),
dataType: "text",
success: function(data) {
//success action
},
error:function (xhr, ajaxOptions){
//error action
}
});
Some of the paramerters are posted by Ajax are-
1190583_1306134[] 1
1226739_1343934[]
My application is written in PHP. Thanks in advance.
I think you need not to post the empty elements.
Replace data: $('#form').serialize()
with data: $('#form :input[value!='']').serialize().
Hopefully it will work for you.
Just wasted 2h on exactly the same thing - partial $_POST data in php backend (~1000 out of ~3500).
Found this in apache logs:
Warning: Unknown: Input variables exceeded 1000. To increase the limit change max_input_vars in php.ini. in Unknown on line 0, referer: http://app1.local/full_path
That was more than sufficient to uncomment max_input_vars in php.ini, change it to 10000 and restart apache. All was working again after that ;)
I had the same problem. I don't know why but $.ajax trucantes post data passed as string.
To solve this use object data instead.
For example
$data=$('form').serialize();
$data=JSON.parse('{"' + decodeURI($data.replace(/&/g, "\",\"").replace(/=/g,"\":\"")) + '"}');
$.ajax({
url:$url,
data:$data,
...
});
Hope this will help ;)
For anyone finding the error Request has been truncated when using direct form pickup via data: new FormData(this) on the Firefox debuger-console, the whole data may have actually been posted and the error seems to be false. I had to spend several hours only to realize that Google Chrome does not report the error and on actual check-up of the image being posted, it was actually being uploaded.
By the way, a direct form pickup as such does not require the serialization and can upload even images.
I am using Wordpress Shopping cart. I want to append or modify the product name based on 3 variations the same thing goes for the price to.
I have tried to implement a simple Jquery on click event to an image using the AJAX $.post method within the single product page. It's not sending the data value even if I encode it with JSON. I must say that I'm new with JSON and PHP. Thanks in advance !
In the single product page
$(function() {
$('#div_img_link').click(function() {
$.ajax({
type: 'POST',
data: "test value",
// dataType : "json",
url: '../wpsc-cart_widget.php',
cache:false
});
});
});
In wpsc-cart_widget.php
$name=wpsc_cart_item_name();
if (isset($_POST['data'])){
$value1 = $_POST['data'];
}else{
$value1 = "";
}
echo "$name $value1";
I know that are some similar posts out there I've read them all can't figure it out. I want to know if the WPSC can perform the jQuery POST function without refreshing the page. Any help will be greatly appreciated!!
schematics
EDIT
SCHEMATICS 2 - to be more concise !
What I'm trying to accomplish
SEA, you're missing a function to handle the response that comes back from the server.
I would expect to see something like this :
$(function() {
$('#div_img_link').click(function() {
$.ajax({
url: '../wpsc-cart_widget.php',
type: 'POST',
data: "test value",
dataType : "JSON",
cache:false,
success: function(data) {
//do something here with the data.
//eg.
alert(data);
},
error: function() {
//do something here if an error occurs.
//eg.
alert("an error occurred");
},
});
});
});
The best place to learn more about ajax is the ajax() page in the jQuery API documentation.
You also need to ensure that the server response and the client expectation are compatible. With a client expectation of dataType: "JSON", you must ensure that the server returns a json-encoded response. Json-encoding is only necessary for multiple data in a single response. A single data item can be sent unencoded.
EDIT:
After seeing your Schematic 2, I think your ajax request data should look something like this :
data: {
'pid': '12345678',
'quantity': '1'
),
where pid is the "product id" and quantity is how many of the product the user wants to add to his/her basket.
But surely the WPSC documentation includes examples of this stuff? You shouldn't have to work it out for yourself.