I have an issue with JSON requests. I have a page with 2 different request, first one works as intended, but the second keeps coming up with an unexpected token o error that I don't understand. first ajax call:
$.ajax({
type:'POST',
dataType: 'json',
url: 'inc/getloads.php?q='+boxnum,
success:function(data){
if (data.l_id && data.u_id) {
var t_id = data.t_id;
var d_id = data.d_id;
var b_id = data.b_id;
var bcont = data.bcont;
var po_num = data.po_num;
var f_date = data.f_date;
var t_date = data.t_date;
var depart = data.depart;
var dest = data.dest;
var miles = data.miles;
var p_amt = data.p_amt;
var tpm = data.tpm;
var d_total = data.d_total;
}
This works just fine, all data is as they are supposed to, however here is the second call:
$.ajax({
type:'POST',
dataType: 'json',
url: 'inc/getfunctions.php?q='+boxnum+'&func=load_po',
success:function(data){
if (data.po_num) {
var e_id = data.l_id;
var load_num = data.load_num;
var expense = data.expense;
var ex_cost = data.ex_cost;
var po_num = data.po_num;
}
This one bombs with the unexpected tokens, I have read about not parsing it twice, is that a solution? the php for the 2nd call:
$func = $_REQUEST['func'];
$_SESSION['func']= $func;
switch ($func) {
case 'load_po':
$q = intval($_REQUEST['q']);
require_once("medoo.min.php");
$database = new medoo();
$database->select("Expenses", "*", array(
"id" => $q));
if (count($datas)>0) {
foreach($datas as $data){
$l_id=$data['id'];
$po_num=$data['po_num'];
$load_num=$data['load_num'];
$expense=$data['expense'];
$ex_cost=$data['ex_cost'];
}
$rdata = array(
'l_id'=> $l_id,
'po_num'=> $po_num,
'load_num'=> $load_num,
'expense'=> $expense,
'ex_cost'=> $ex_cost
);
echo json_encode($rdata); //$datas['first_name']
} else {
echo 'no datas';
}
break;
}
$datas is never initialized and you have a foreach that can be executed multiple times, but you only assign the result once:
foreach($datas as $data){
$l_id=$data['id'];
$po_num=$data['po_num'];
$load_num=$data['load_num'];
$expense=$data['expense'];
$ex_cost=$data['ex_cost'];
}
$rdata = array(
'l_id'=> $l_id,
'po_num'=> $po_num,
'load_num'=> $load_num,
'expense'=> $expense,
'ex_cost'=> $ex_cost
);
so maybe if you only want to retrieve one row from datas, do something like:
$data = $datas[0];
$l_id=$data['id'];
$po_num=$data['po_num'];
$load_num=$data['load_num'];
$expense=$data['expense'];
$ex_cost=$data['ex_cost'];
$rdata = array(
'l_id'=> $l_id,
'po_num'=> $po_num,
'load_num'=> $load_num,
'expense'=> $expense,
'ex_cost'=> $ex_cost
);