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 );
}
});
Related
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've been trying the code at: https://gist.github.com/Exeu/4674423 this is a code that connects to amazon wsdl and retrieve product information. so far I have not been able to make it work. The code consist of a index.html, ama_funtion.js, search.php, AmazonECS.class.php. The index file loads and you insert the item name you want choose the category and other params, then when you click in the search link it goes to the ama_function.js and it make and ajax request to the search.php which have the passwords and keys to connect to the wsld, then it request the info from Amazon and return it as JSON to ama_function.js.
I have test ama_funtions.js to see if the index.html file connects to it, and it does, I did put an alert to show the input in the search box at index.html. Also I have test the search.php, I have manualy put the values in the search.php file and it connects to amazon and retrieve the product information as JSON object. the problem is that there is not a success callback as no data return to ama_funtions.js from search.php to be rendered. I know this as I tested it by adding an error function
$.ajax({
url: "search.php",
dataType: 'json',
type: 'GET',
error: function () {
alert("NO DATA");
},
and it did show me the alert, also I have tried to change the ->
data: "q="+value+"&category="+cat+"&country="+country+"&page="+page,
for ->
data: {
q : value,
category : cat,
country : country,
page : page }
nothing works I am crazy looking to solve this, I will appreciate someone that help us with this. Please see a working example which belong the person sharing the code -> http://amazonecs.pixel-web.org. This is the way is suppose to work but it doesn't.
Regards
$.ajax({
url: "search.php",
dataType: 'json',
type: 'GET',
error: function () {
alert("NO DATA");
},
data: "q="+value+"&category="+cat+"&country="+country+"&page="+page,
data: {
q : value,
category : cat,
country : country,
page : page
}
})
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
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.
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"}