1

I am using the Jquery function,

function ajax_fun(id,val)
{
$.ajax({
    type: "GET",
    url: "data.php",
    dataType: "json",
    data: { pid: 1, cart_id: id, val: val },
    success: function (returndata){
       console.log(returndata);
    }  
});
}

Call

ajax_fun(1,100);

data.php

<?php
if(isset($_GET['pid']) && $_GET['pid']==1)
{

  $cart_id = $_GET['cart_id'];
  $val  = $_GET['val']+0.06;
  $arr = array('cart_id'=> $cart_id, 'total' => $val);
  json_encode($arr);
}
?>

Console returns 'null' . It didn't return cart_id and total.

Can anyone help?

.

1
  • You didn't echo the output.. Commented Aug 9, 2012 at 6:49

3 Answers 3

3
json_encode($arr);

shoule be

echo json_encode($arr);

You forget to echo the result.

Full code:

<?php
if(isset($_GET['pid']) && $_GET['pid']==1) {
  $cart_id = $_GET['cart_id'];
  $val  = $_GET['val']+0.06;
  $arr = array('cart_id'=> $cart_id, 'total' => $val);
  echo json_encode($arr); // write echo before json_encode($arr)
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

RE-Write this line as...

echo json_encode($arr);

Comments

1

You need to add 'echo' before json_encode which return the data

<?php
if(isset($_GET['pid']) && $_GET['pid']==1)
{

  $cart_id = $_GET['cart_id'];
  $val  = $_GET['val']+0.06;
  $arr = array('cart_id'=> $cart_id, 'total' => $val);
  echo json_encode($arr);
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.