Hi everyone
After one week searching what could be the problem I have not discovered what could fix it.
I already have done some jquery ajax calls before and I am currently trying to realize a Wordpress pluging.
There I am trying to pass an array in Javascript using Ajax Jquery to a Php page using POST request. Ajax request is correctly achieved ( success and done events are triggered ) but when I am accessing the page $_POST is empty.
I even tried in the code below to simply put simple string data but I can't access it in php page.
This is my Jquery code :
$('#Enregistrer').click(function(){
saveArray= new Array();
var i=0;
$('.q1').each(function(){
if ($(this).hasClass('tablesorter-filter-row')==true){
// doing nothing
}
else{
saveArray[i]=new Array();
for (var j=0; j<6; j++){
if ($(this).children().eq(j).hasClass('m1') || $(this).children().eq(j).hasClass('m2')){
var value = $(this).children().eq(j).children('select').val();
}
else{
var value = $(this).children().eq(j).html();
}
saveArray[i][j]= value;
}
i++;
}
});
console.log(saveArray);
var data = 'test';
$.ajax({
url:'../api/send_form.php',
method:'POST',
data: { 'data': data },
success: function(resp){
console.log('ok');
window.location='../api/send_form.php';
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});
If you need more details don't hesitate, this is my first post on stackoverflow.I try to be as precise as possible.
Thanks
maybe your attempt is missing the type: 'POST', also added a few more elements
$.ajax({
dataType: 'html',
type: 'POST',
url:'../api/send_form.php',
method:'POST',
data: { data: data },
processData: false,
contentType: false,
success: function(resp){
console.log('ok');
window.location='../api/send_form.php';
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
Related
I'm stuck here when passing or sending multiple parameters on Ajax. It only works when I pass one. Here the Ajax code:
$.ajax({
type: "POST",
url: "dataCartas.php",
data: {valorXY: valorXY,
CentroX: CentroX},
success: function(msg){
// return value stored in msg variable
console.log(valorXY + CentroX)
}
});
And the PHP code:
$valorXY = $_POST['valorXY'];
$CentroX = $_POST['CentroX'];
include "configX.php";
if ($conn){
$sql="EXEC sp_InsertaComID #ComID = '".$valorXY."', #DR =
'".$CentroX."'";
if ($rs=sqlsrv_query($conn,$sql)){
}else{
echo (print_r(sqlsrv_errors(), true));
}
}else{
die(print_r(sqlsrv_errors(), true));
}
Sorry for my bad english :(
You can serialize a form
$.ajax({
type: 'post',
url: 'include/ajax.php',
data: $('#form').serialize(),
success: function (response) {
// do something
},
error: function(jqxhr,textStatus,errorThrown){
console.log(jqxhr);
console.log(textStatus);
console.log(errorThrown);
}
});
you can also use form data https://stackoverflow.com/a/8244082/3063429
I found a lot of questions with this topic, but can't figure out what I'm doing wrong.
I try to pass data to a new PHP-page, but i always get NULL.
Here is my JS code:
submitHandler: function() {
$.ajax({
method: 'POST',
url: 'newPhp.php',
data: { name: "13" },
success: function(name) {
console.log(name); // is displayed correctly
window.location.replace("newPhp.php");
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
} //no error occurs for me
});
}
And my PHP-File just does this:
$passedName = $_POST["name"];
var_dump($passedName);
I want to use this to pass Data from a input form to the server, which sends a confirmation mail.
I am using an ajax call like the following:
$(function () {
$.ajax({
type: 'POST',
url: 'frmHistoryReportFinal.aspx/GetDataTable',
data: json_data,
contentType: 'application/json;charset=utf-8',
dataType: 'json',
success: function (responseText) {
if (responseText.d == "")
return;
parsedData = JSON.parse(responseText.d);
alert(parsedData);
count = 0;
},
error: function (xhr, textStatus, errorThrown) {
//process the error
alert(xhr);
alert(textStatus);
alert(errorThrown);
}
});
});
When I have a small set of data returned from my C# codebehind. The ajax call functions well. But when I have a json string with larger data, say 200 records returned then the ajax call gives error("Internal Server Error").
Please help me resolve the issue as I usually need to handle large datasets.
alright so here is my problem i have got a jquery function and a php script
jquery function:
function set_session(para){
//Send Ajax request
var dataString = 'waarde='+ para ;
jQuery.ajax({
url: '../js/set_session_side.php',
type: 'POST',
data: dataString,
cache: false,
success: function(data, textStatus, xhr) {
console.log(data); // do with data e.g success message
},
error: function(xhr, textStatus, errorThrown) {
console.log(textStatus.reponseText);
}
});}
set_session.php:
<?php
$_SESSION['side'] = $_POST['waarde'];
?>
when i run the jquery function there is a succes, but no data, and my php script is not working/ or not being triggerd, because $_SESSION['side'] is not changing.
any help would be appriciated.
thanks in advance
First make sure your requesting script is correct.
add sesstion_start() at the beginning of your set_session.php
( i believe your problem is here);
I'm trying to call a function when I get success from my ajax call, but it's not working. This is what I've tryed so far.
function dtMRPReasonCode(dt) {
var data = null;
jQuery.ajax({
type: "POST",
data: {},
url: "Index.aspx/getMRPReasonCodeReport",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
if (msg.d) {
console.log(dt);
console.log(msg.d);
buildTableBody(dt, msg.d);
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert("Error: dtMRPReasonCode");
}
});
return false;
}
function buildTableBody(dt, obj) {
dt.fnClearTable();
data = [];
$(obj).each(function(index, value) {
element = [];
element.push(value.Metric);
element.push(value.Region);
element.push(value.Plant);
element.push(value.Customer);
element.push(value.IMAC);
element.push(value.NotFilled);
element.push(value.Filled);
element.push(value.Total);
data.push(element);
});
dt.fnAddData(data);
}
Thanks in advance!
Edit #1
I used console.log in order to show you what I got from dt and msg.d (Image)
Edit #2
If I paste the commands from buildTableBody function in the success: handler instead of calling buildTableBody function in the success: handler it actually works:
function dtMRPReasonCode(dt) {
var data = null;
jQuery.ajax({
type: "POST",
data: {},
url: "Index.aspx/getMRPReasonCodeReport",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
dt.fnClearTable();
data = [];
$(msg.d).each(function(index, value) {
element = [];
element.push(value.Metric);
element.push(value.Region);
element.push(value.Plant);
element.push(value.Customer);
element.push(value.IMAC);
element.push(value.NotFilled);
element.push(value.Filled);
element.push(value.Total);
data.push(element);
});
dt.fnAddData(data);
},
error: function(xhr, ajaxOptions, thrownError) {
alert("Error: dtMRPReasonCode");
}
});
return false;
}
But it makes no sense to me, since this actually should work in both ways.
Pretty sure you have a typo on your function call
buildTableBody(td, msg.d);
should be
buildTableBody(dt, msg.d);
Also what is the return type from Index.aspx/getMRPReasonCodeReport? If it is string, you've got to unescape the string before you can treat it as JSON.
Try removing contentType : "application/json utf-8" from your AJAX call. That is the type of the data sent to the server. It is likely that you want the default content type.
Unless your server-side resource was configured to accept json it likely accepts application/x-www-form-urlencoded
http://api.jquery.com/jQuery.ajax/