Reputation: 209
Okay prior to my previous question I have tried a bit of modification to my code but somehow I am still lagging.
This is jquery I have created
$(document).ready(function()
{
var array_ids = [];
$('.add').click(function()
{
array_ids.push($(this).parent().siblings('.row_id').html().trim());
alert(array_ids);
});
$('.show').click(function(e)
{
//e.preventDefault();
var jsonString = JSON.stringify(array_ids);
$.ajax(
{
method: 'POST',
url: 'addsale.php',
data: {data : jsonString},
cache: false,
dataType: "json",
success: function()
{
console.log(data.reply);
alert(data.reply);
}
});
});
});
And addsale.php
if(isset($_POST['push'])) //tried it commenting also!
{
$data = array();
$data = json_decode(stripslashes($_POST['data']));
foreach($data as $d){
echo $d;
}
}
Can anyone tell me what is missing to access the array and get the html from addsale.php to current page?
Upvotes: 3
Views: 120
Reputation: 396
No data
argument in your success function. Modify your addsale.php
$(document).ready(function()
{
var array_ids = [];
$('.add').click(function()
{
array_ids.push($(this).parent().siblings('.row_id').html().trim());
alert(array_ids);
});
$('.show').click(function(e)
{
//e.preventDefault();
var jsonString = JSON.stringify(array_ids);
$.ajax(
{
method: 'POST',
url: 'addsale.php',
data: {data : jsonString},
cache: false,
dataType: "json",
success:function(data)
{
console.log(data.reply);
alert(data.reply);
}
});
});
});
addsale.php
<?php
if(isset($_POST['data'])) //tried it commenting also!
{
$data=json_decode(stripslashes($_POST['data']));
echo json_encode(['reply'=>$data]);
exit();
}
Upvotes: 0
Reputation: 3579
i think you have everything correct but missing something right data: {data: ... }
, you should also add something like below-
data : {data : jsonString, 'push':'push'},
and in php code you try to decode the an array which is not in json format, your code just need to be like this-
if(isset($_POST['push'])) //tried it commenting also!
{
foreach($_POST['data'] as $d){
echo $d;
}
}
Upvotes: 0
Reputation: 838
$(document).ready(function()
{
var array_ids = [];
$('.add').click(function()
{
array_ids.push($(this).parent().siblings('.row_id').html().trim());
alert(array_ids);
});
$('.show').click(function(e)
{
//e.preventDefault();
//prefer parse function
var jsonString = JSON.stringify(array_ids);
$.ajax(
{
method: 'POST',
url: 'addsale.php',
data: {"data" : jsonString},
cache: false,
dataType: "json",
//e is the response text from your PHP code
success: function(e)
{
//I don't know why this code
//console.log(data.reply);
//alert(data.reply);
}
});
});
});
in your PHP code try
if(isset($_POST['data'])) //tried it commenting also!
{
$data = array();
$data = json_decode(stripslashes($_POST['data']));
foreach($data as $d){
echo $d;
}
}
Upvotes: 1
Reputation: 4584
echo with foreach
will not good handling for json response ! You should loop that json after response to ajax success
function ...
addsale.php
if(isset($_POST['data'])) //tried it commenting also!
{
$data = array();
$data = json_decode(stripslashes($_POST['data']));
echo json_encode($data);
}
access json resposne in ajax
$.ajax(
{
method: 'POST',
url: 'addsale.php',
data: {"data" : jsonString},
cache: false,
dataType: "json",
success: function(d)
{
//I don't know why this code
console.log(d);
}
});
Upvotes: 0