why this code give error like "Warning: json_decode() expects parameter 1 to be string, array given in <b>E:\wamp\www\datefun\show_cart_update.php</b> on line". Please help me. I am new in php and json

<html>
<head>
<title>Untitled Document</title>
<script src="js/jquery-1.9.1.min.js"></script> 
</head>

<body>
<script>
function update_cart_detail()
{
    var aData = new Array();
    aData[0]={
                "session_id":"64qu9ng2o8o996s21bjt9321h1",
                "order_id":"26",
                "design_id":"21",
                "order_pcs":2,
                "item_rate":1000,
                "item_amt":2000
              }
    aData[1]={
                "session_id":"64qu9ng2o8o996s21bjt9321h1",
                "order_id":"26",
                "design_id":"25",
                "order_pcs":1,
                "item_rate":2000,
                "item_amt":2000
              } 
    var jTableData = JSON.stringify(aData);
    return  jTableData;
}

$(document).ready(function(){
    $("#update_cart").click(function(){
        //my jsons data prepare like 
        var jSonsData=update_cart_detail();
    $.post('show_cart_update.php', jSonsData, function(data){
        alert( data );
    })
    .fail(function() {
        alert( "Posting failed." );
    });
    return false;

    });
});

</script>
<button type="submit" name="update_cart_action" value="update_qty" id="update_cart" title="Update Cart" class="button btn-update" onClick="update_cart_detail()"><span><span>Update Cart</span></span></button>
</body>
</html>

Dani AI

Generated

The PHP warning means json_decode() was handed an array (for example PHP’s $_POST) instead of a JSON string. In this thread is correct that responses should be sent with json_encode(), but the immediate problem is on the input side: either the AJAX request produced parsed form data (so PHP already has an array), or the PHP code called json_decode($_POST) by mistake. As noted, seeing show_cart_update.php would make the exact cause obvious; here are safe patterns and quick checks.

If the intent is to POST raw JSON, send it as JSON and read php://input in PHP:

// send raw JSON
$.ajax({
  url: 'show_cart_update.php',
  method: 'POST',
  contentType: 'application/json; charset=utf-8',
  dataType: 'json',
  data: JSON.stringify(cartItems) // cartItems = JS array/objects
});
// receive raw JSON
$raw = file_get_contents('php://input');
$data = json_decode($raw, true); // now $data is an array
if (json_last_error() !== JSON_ERROR_NONE) {
  // handle bad JSON
}

If sending form-encoded data is easier, post a named field and decode that, or let PHP parse nested arrays and skip json_decode entirely:

// send as form field (jQuery will serialize nested objects)
$.post('show_cart_update.php', { cart: cartItems }, function(resp){ /* ... */ }, 'json');
// on server
if (isset($_POST['cart'])) {
  // if jQuery serialized as nested fields, $_POST['cart'] is already an array
  // if it was sent as a JSON string, decode:
  $cart = is_string($_POST['cart']) ? json_decode($_POST['cart'], true) : $_POST['cart'];
}

Quick troubleshooting: inspect the Network tab to see Request Payload vs Form Data; add var_dump($_POST) and file_get_contents('php://input') temporarily to log what PHP receives; check json_last_error_msg() after decode. Also follow ’s tip and remove redundant inline onClick to avoid accidental double calls. Finally, when returning JSON, set header('Content-Type: application/json') and echo json_encode($response) as recommended.

Recommended Answers

All 5 Replies

I think your show_cart_update.php probably is just return the array of results. What you need to do is echo json_encode($your_result_array) so that the data returned is in json format.

Member Avatar for Member #120589

You quote a php error yet we see no php code. Why is that?

Post your show_cart_update.php code. So It will also help the other to find an right answer for you. If your question is related to JavaScript / DHTML / AJAX please post in that forum and you will get answer as soon..

Also, I think you should omit onClick="update_cart_detail()" since you trigger function and whole AJAX process by clicking id="update_cart" which is that button.

Member Avatar for Member #120589

OK, shall we wait for the OP to return?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.