Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
use echo json_encode($myArray); –  Think Different May 27 at 15:57
    
Then in your ajax call add success: function(result){ .... }. BTW your ajax call need tweaking. –  Think Different May 27 at 15:58
    
api.jquery.com/jquery.ajax/#example-0, data: { table: table } –  KA_lin May 27 at 15:58

3 Answers 3

up vote 2 down vote accepted

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

share|improve this answer
    
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 –  Nadim Akram May 27 at 16:36
1  
Carefully follow my given answer. In that answer i changed the index table to t_name. –  user3659034 May 27 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. –  Nadim Akram May 27 at 16:44
2  
just check data: {t_name:table } and $myArray = $_REQUEST['t_name']. –  user3659034 May 27 at 16:46
    
ok...it works correctly..thanks a lot. –  Nadim Akram May 28 at 8:01

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"
share|improve this answer
    
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 –  Nadim Akram May 27 at 16:37

Try this :

 $.ajax({
        type : "POST",
        url: 'moslem3.php',
        cache:false, 
        async:true,
        global:false,
        data : {
        "table": table }
        }).done(function(msg) {
alert(msg);
})
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.