jquery - Array passes through ajax but received as string in php -
the following jquery code:
e_val = []; e_val = ['enroll','reject']; $.ajax({ url: 'myurl.php', type: 'post', datatype: 'text', data: { reason: 'insert', eval: e_val }, success: function (suc) { row.remove(); alert(suc); } });
myurl.php:
$reason = $_post['reason']; $eval = array(); if($reason == "insert"){ $eval = $_post['eval']; echo $eval[1]; }
and alert is:
n
that's because n @ index value 1 in e_val[0]. can know why array converted string in php page , i'm not able access array elements? in advance.
you can use implode() implode() function returns string elements of array.
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <script> var e_val = []; var e_val = ['enroll','reject']; console.log(e_val); $.ajax({ url: 'test2.php', type: 'post', datatype: 'text', data: { reason: 'insert', eval: e_val }, success: function (suc) { alert(suc); } }); // alert("dddd"); </script> </body> </html>
test2.php
<?php $reason = $_post['reason']; $eval = array(); if($reason == "insert"){ $eval = $_post['eval']; echo implode(', ', $eval ); // echo "dddd"; } // echo "dddd"; ?>
wiki
Comments
Post a Comment