0

arraytest.html

<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>

<input type="button" id="btn" value="go to php" onclick="show()">
<br><br>
<script type="text/javascript">
var a = new Array(6,5);
function show(){
   $.ajax({        
    type: "POST",
    url: "arra.php",
    data: {test : JSON.stringify(a)},
    success: function(data) {
        alert("success" +data);
        }
    }); 
}
</script>

arra.php

<?php 
$arr = json_decode($_POST['test'], true);
$v = $arr[0] + arr[1] ;
echo $v ; 
?>

i couldn't add the array values in php file. anybody could help me with this ? am getting the error as: Parse error: syntax error,unexpected '[' in c:\wamp\www\arra.php on line 3

7
  • What problem are you having? The PHP looks fine. The only issue I can see is that the success: function doesn't display the response anywhere. Commented Feb 8, 2015 at 7:07
  • i coudn't add the array values in php that has posted from javascript. how could i make that ? Commented Feb 8, 2015 at 7:16
  • Why do you think you couldn't add them? Commented Feb 8, 2015 at 7:18
  • data in the succes function shows parse error and syntax error on line "$v = $arr[0] + arr[1] ;" Commented Feb 8, 2015 at 7:26
  • 1
    You're missing a $ on arr[1]. Commented Feb 8, 2015 at 7:45

2 Answers 2

1

Your success function isn't showing the response. The callback function should take an argument, and then do something with it.

function show(){
    $.ajax({        
        type: "POST",
        url: "arra.php",
        data: {test : a},
        success: function(response) {
            alert("success, the answer is: " + response);        
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

try changing it to $_POST. notice you are sending POST.

<?php 
    $arr = $_POST['test'];
    $v = $arr[0] + $arr[1];
    echo $v; 
?>

2 Comments

$_REQUEST contains everything that's in $_POST and $_GET. Why don't you think it will work?
i coudn't add the array values in php that has posted from javascript. how could i make that ?

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.