0

I would like to know how I can pass a Javascript array from Javascript to PHP through Jquery/Ajax. In fact I created two files: t1.html and moslem3.php . The code of each file is below:

The code of t1.html:

 <html>
<head>

</head>
<body>
<script type="text/javascript">
    $(document).ready(function() {
        var table = ["actor", "subject", "object"];
        $.ajax({        
        type: "POST",
        url: 'moslem3.php',
        data: table 
        });
    });
</script>
</body>
</html>

The code of moslem3.php:

<?php
$myArray = $_REQUEST['table'];
echo $myArray;
?>

The file moslem3.php displays this message:

Notice: Undefined index: table in C:\wamp\www\test1\moslem3.php on line 2  

So my question is: where is the wrong here exactly?..Is there any one who has any idea?

Thanks in advance.

3
  • use echo json_encode($myArray); Commented May 27, 2014 at 15:57
  • Then in your ajax call add success: function(result){ .... }. BTW your ajax call need tweaking. Commented May 27, 2014 at 15:58
  • api.jquery.com/jquery.ajax/#example-0, data: { table: table } Commented May 27, 2014 at 15:58

3 Answers 3

3

PHP doesn't inherently know anything about the JavaScript variables being used, such as var table. Its insights for the $.ajax() request are completed limited to the data structure.

So, for PHP to know about a 'table' parameter in $_REQUEST, the data needs to include it that as a named value:

data: { table: table }

This defines the data as an Object with a property table assigned to the value of the variable by the same name.


You can witness the difference using $.param(), which $.ajax() uses:

var table = ["actor", "subject", "object"];

console.log($.param(table));
// "undefined=&undefined=&undefined="

console.log($.param({ table: table }));
// "table%5B%5D=actor&table%5B%5D=subject&table%5B%5D=object"
1
  • I did that but the file moslem3.php still display this message: Notice: Undefined index: table in C:\wamp\www\test1\moslem3.php on line 2 Commented May 27, 2014 at 16:37
2

Change Jquery

<script type="text/javascript">
$(document).ready(function() {
    var table = ["actor", "subject", "object"];
    $.ajax({        
    type: "POST",
    url: 'moslem3.php',
    data: {t_name:table },
    success:function(data){
        console.log(data);
    }
    });
});
</script>

Change in moslem3.php

<?php
    $myArray = $_REQUEST['t_name'];
    print_r($myArray);
?>

Output:

enter image description here

4
  • I did that but the file moslem3.php still display this message: Notice: Undefined index: table in C:\wamp\www\test1\moslem3.php on line 2 Commented May 27, 2014 at 16:36
  • 1
    Carefully follow my given answer. In that answer i changed the index table to t_name. Commented May 27, 2014 at 16:39
  • yes, I follow your answer exactly, the file t1.html displays exactly what it is in you screenshot above, but the file moslem3.php still displays that message. Commented May 27, 2014 at 16:44
  • 2
    just check data: {t_name:table } and $myArray = $_REQUEST['t_name']. Commented May 27, 2014 at 16:46
0

Try this :

 $.ajax({
        type : "POST",
        url: 'moslem3.php',
        cache:false, 
        async:true,
        global:false,
        data : {
        "table": table }
        }).done(function(msg) {
alert(msg);
})

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.