how to convert Javascript string array to PHP, it will processed in the server not client side

<?php
$str = 'var str_array = ["aa", "bb"]';
$str = str_replace("var ", '$', $str); 
eval($str);
print_r($str_array);
share|improve this question
    
but js is client side after converting its not print of array using print_r – Maninderpreet Singh May 5 '16 at 5:50
1  
Possible duplicate of how to convert javascript array to php array – Abrar Jahin May 5 '16 at 5:52
    
What are you trying to do? – Sagar Guhe May 5 '16 at 5:53
    
Update eval($str); into eval("$str;"); – Narendrasingh Sisodia May 5 '16 at 5:53
    
I need js string to process in the server, it not for client side. – uingtea May 5 '16 at 5:54
up vote 1 down vote accepted

Eval() should end with ;

<?php
$str = 'var str_array = ["aa", "bb"]';
$str = str_replace("var ", '$', $str); 
eval($str.";");
print_r($str_array);

Output:

Array ( [0] => aa [1] => bb )

But I don't know why you opt for this.

share|improve this answer
1  
thanks its work, also thanks for who downvoted without giving solution. will accepted this answer soon. – uingtea May 5 '16 at 5:57

Post your javascript array as a JSON string via ajax and process it server-side.

Javascript

var str_array = ["aa", "bb"];
var request = $.ajax({
  url: "test.php",
  method: "POST",
  data: { myData : JSON.stringify(str_array) },
  dataType: "html"
});
request.done(function( msg ) {
  // ajax response
});
request.fail(function( jqXHR, textStatus ) {
  alert( "Request failed: " + textStatus );
});

PHP (test.php)

$json = $_POST['myData'];
$myDataArray = json_decode($json,true);
print_r($myDataArray);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.