javascript - AJAX jquery json sending array to php -
i'm trying send associative array via ajax $.post php. here's code:
var request = { action: "add", requestor: req_id, ... } var reqdetails = $("#request_details").val(); switch(reqdetails){ case 1: request[note] = $("#note").val(); break; ... } if(oldrequest()){ request[previousid] = $("old_id").val(); } $('#req_button').toggleclass('active'); $.post("scripts/add_request.php", { request_arr: json.stringify(request) }, function(data){ console.log(data); $('#req_button').toggleclass('active'); }, 'json');
and i'm trying read received data in php script:
echo json_decode($_post["request_arr"]);
but it's not working. i'm newbie js, can't figure out i'm doing wrong.
check below link reference
sending array php javascript/jquery
step 1
$.ajax({ type: "post", url: "parse_array.php", data:{ array : json.stringify(array) }, datatype: "json", success: function(data) { alert(data.reply); } });
step 2
you php file looks this:
<?php $array = json_decode($_post['array']); print_r($array); //for debugging purposes $response = array(); if(isset($array[$blah])) $response['reply']="success"; else $response['reply']="failure"; echo json_encode($response);
step 3
the success function
success: function(data) { console.log(data.reply); alert(data.reply); }
Comments
Post a Comment