I have a funnction to post and recive form to another php file. I don't know why, but the php file has return empty array.
the function has two attributes "name" and "arg" and wants to send these two values to the file steel_th_dynamic_query.php, then display what it returns in the div "demo". I think the problem is on the line data: { but I don't know why it doesn't work.
function displayPhrase(name, arg){
//document.getElementById("demo").innerHTML = name + ' ARG: ' + arg;
$.ajax({
url: './modules/settings/steel_th_dynamic_query.php',
data: {
MyData: name,
MyARG: arg
},
processData: false,
contentType: false,
type: 'POST',
success: function(data){
document.getElementById("demo").innerHTML = data;
}
});
}
You need to remove the processData and contentType properties, or at least set them back to their default values. Setting them to false is only required when sending binary data in the request, ie. a FormData object.
function displayPhrase(name, arg) {
$.ajax({
url: './modules/settings/steel_th_dynamic_query.php',
type: 'POST',
data: {
MyData: name,
MyARG: arg
},
success: function(data) {
$("#demo").html(data);
}
});
}
Related
My Javascript code is the following:
function on(logged_user) {
alert(logged_user);
$.ajax({
url: "update_stats.php",
type: "POST",
data: logged_user
});
}
update_stats.php contains
<?php
$logged_user = $_POST["logged_user"];
?>
but I can see that $logged_user is just an empty string (I'm inserting it into a database table)
Your data parameter for the $.ajax call is not in the right format. From the manual:
The data option can contain either a query string of the form
key1=value1&key2=value2, or an object of the form {key1: 'value1',
key2: 'value2'}
You should change that line to:
data: { logged_user : logged_user },
or
data: 'logged_user=' + logged_user,
just try this:
function on(logged_user) {
alert(logged_user);
$.ajax({
type : 'POST',
url : update_stats.php,
data : logged_user,
dataType : 'json',
encode : true
});
}
Its not javascript , its a jquery ajax, so please include a jquery library.
and change your function like this
Syntax to pass the values like,
data: '{ "key":' + value+ ', "key":"value" }',
or
data = "key=" + value
or
data: JSON.stringify({ key: value, key: value}),
function on(logged_user) {
var dataString = "logged_user=" + logged_user
$.ajax({
type: "POST",
url: "update_stats.php",
data: dataString,
cache: false,
success: function(result) {
alert(result)
}
})
}
You need to pass data in key - value format to get accesible by $_POST, $_GET and $_REQUEST array variables in php.
data: {'logged_user' : data: logged_user}
You can access raw input like JSON data or text data which not in the key value format you can use file_get_contents("php://input") to access data.
This is my javascript for sending the request to the php page. But I can't get the response when I use the conditional if statement with the $_REQUEST variable
function getNumberOfShareHolders()
{
var dataString = "GNOSH";
alert(dataString);
$.ajax({
url: 'chartJs.php',
dataType: "json",
dataString,
cache: false,
success: function(data) {
if(data)
{
alert(data);
}
else {
}
}
});
}
Here's the php code
if(empty($_REQUEST['GNOSH'])) {
echo json_encode("request");
}
else
{
echo json_encode("No request");
}
You haven't specified a data property at all, so you aren't passing any data for jQuery to put in the HTTP request.
You've included a property named dataString in your options (which is ignored) and given it the value GNOSH.
You need a property called data with a value that is an object, then that object needs a property named GNOSH with some value. What that value is doesn't really matter as you are testing for truthfulness.
dataType: "json",
data: { [dataString]: 1 },
cache: false,
I am trying to pass some HTML form data as well as a global array to PHP via AJAX. I know how to pass an array, and I know how to pass serialized form data. But how do I pass both at the same time? I have tried data: { formData, arrGFormId: arrGFormId }, but it doesn't work.
Edit: The form is just a simple HTML form with some inputs. My array values come from another AJAX call and are pushed into the global array arrGFormId.
function validateForm3(){
jQuery.ajax({
type: "POST",
url: "community_form_add.php",
async: false,
data: { arrAdminList: arrAdminList },
}).done(function(rs){
var sResult = rs.sResult;
var arrFormId = rs.arrFormId;
Array.prototype.push.apply(arrGFormId, arrFormId);
})
})
var arrGFormId = [];
jQuery('#formCreateForm').submit(function(e){
e.preventDefault();
var formData = new FormData(jQuery(this)[0]);
formData.append('sAction', 'submitForm');
jQuery.ajax({
type: "POST",
url: 'community_form_add.php',
data: formData,
cache: false,
contentType: false,
processData: false,
beforeSend:function(){
jQuery('.load_ball').css("display","block");
},
success: function(data)
{
jQuery('.load_ball').css("display","none");
jQuery('.cover').css("display","block");
jQuery('.popUpSubmitSuccess').fadeIn(300);
}
})
});
You need to encode the array, then you can add it to the FormData. You can convert it to JSON.
formData.append('arrGFormId', JSON.stringify(arrGFormId));
Then in PHP you can use json_decode($_POST['arrGFormId']).
you can use hidden input inside the existing form. the value of the hidden inputs are the array. so you only pass formData through ajax.
hello i have this function using AJAX. i need to retrieve the file's name size type and i will have to save it to my database..
here is my code.
function UploadImage() {
var data = new FormData($('#fileName'));
Jquery.each($('#fileName')[0].files, function(i,file) {
data.append(i,file);
}
$.ajax({
type: 'post',
data: data,
url: 'controller/function',
cache: false,
ContentType: false,
ProcessData: false,
success: function(data) {
alert(data);
}
}
when i will retrieve the data coming from the ajax request through my controller, i cant get the data of the files using _$Files[] it has error saying undefined index.
This may work for you
function UploadImage()
{
var data = new FormData(document.getElementById("fileName"));//remember fileName is your form id.
//You dont need this
/*Jquery.each($('#fileName')[0].files, function(i,file) {
data.append(i,file);
}*/
//you don't need to modify the data. data already contains whole form information.
$.ajax({
type: 'post',
data: data,
url: 'controller/function',//better use full path instead of relative path
cache: false,
ContentType: false,
ProcessData: false,
success: function(data) {
alert(data);
}
}
I post array via ajax like:
console.log(srcarray); //["16.png", "17.png", "19.png", "18.png"]
$.ajax({
type: "POST", url: "update.php",
data: {srcarray: srcarray}
})
get error:
TypeError: Type error jquery-1.10.1.min.js:6
I tried add processData: false, contentType: false, seems not work, won't show error but can't post data to php....
How can I fix this?
Appreciate your help. Thanks
UPDATE UPDATE
found the error line:
t = x.isFunction(t) ? t() : null == t ? "" : t, i[i.length] = encodeURIComponent(e) + "=" + encodeURIComponent(t)
UPDATE
the array comes
below code in container each function so console.log(content_img_arr_merge)
will get result :
["16.png", "17.png", "19.png", "18.png"]
["0.png"]
["0.png"]
[]
[]
$.each(content_img_arr_merge, function (index, value) {
content_img_arr_merge[index] = value.replace("u_img/"+id+"/", "");
});
console.log(content_img_arr_merge);
If I change the code test:
$.each(content_img_arr_merge, function (index, value) {
content_img_arr_merge[index] = value.replace("u_img/"+id+"/", "");
});
console.log(content_img_arr_merge);
content_img_arr_merge = ["16.png", "17.png", "19.png", "18.png"];
Then it doesn't shows the error...
Please try adding quotes,
i.e,
$.ajax({
type: "POST", url: "update.php",
data: {'srcarray': srcarray} //added quotes here 'srcarray'
});
or simply pass the data array only,
$.ajax({
type: "POST", url: "update.php",
data: srcarray
});
find the answer here https://stackoverflow.com/a/17876165/1575921
use $.get() data: { "images" : array.get() }
The data arg to $.ajax has to be a string, or a "PlainObject" (basically, a simple array created with "{}" or "new Object()", it appears).
You probably want to pass the srcarray arg through JSON.stringify, such as in this code snippet:
$.ajax({
type: "POST", url: "update.php",
data: {srcarray: JSON.stringify(srcarray)}
})
That way you'll have a string which should be easily converted back to an array on the other end.