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"
Sign up to request clarification or add additional context in comments.

1 Comment

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
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 Comments

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
Carefully follow my given answer. In that answer i changed the index table to t_name.
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.
just check data: {t_name:table } and $myArray = $_REQUEST['t_name'].
0

Try this :

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.