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 have the following code to pass a Javascript array to PHP using ajax :

in HTML :

echo "<input type=\"hidden\" id= \"que_id\" name= \"que_id[]\" value=".$questions['que_id'].">";

This is inside a loop.

in Javascript :

var que_id_array = new Array();
    $('input[name="que_id[]"]').each(function(){
       que_id_array.push($(this).val());
    });

AJAX Call :

$.ajax({
    type:"POST",
    url: 'questionmastermodify.php',
    data: { que_id:que_id_array},
    success: function(data) {
        $('.my_update_panel').html(data);
        $('#overlay').fadeOut();
    }
});

in PHP :

$que_id = $_REQUEST['que_id'];

echo count($que_id);    

The count displays 1 and not the size of the array, whereas in Javascript the console shows :

console.log(que_id_array);

output :

["151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199", "200"]

I am stuck as i need this array in PHP but unable to pass this array from JS to PHP.

Thanks in advance....

Sandy505

share|improve this question
    
Pass array from JS to PHP, use JSON –  vaibhavmande Oct 14 '13 at 9:00
    
Why are you reading the value with $_REQUEST instead of $_POST? –  Daniele Brugnara Oct 14 '13 at 9:01
    
I need to pass several such arrays and i am suing JSON only.data: { que_id:que_id_array, qtype:qtype_array, que_desc:que_desc_array, ans1:ans1_array, ans2:ans2_array, ans3:ans3_array, ans4:ans4_array, true_ans:true_ans_array } –  Sandy505 Oct 14 '13 at 9:01
    
tried with $_REQUEST and $_POST both ... Same results :-( –  Sandy505 Oct 14 '13 at 9:02
    
What does var_dump($que_id) return? –  Kyra Oct 14 '13 at 9:02

3 Answers 3

up vote 2 down vote accepted

I made a quick test with your code... and with a few changes, It work's for me.
Take a look at your code, specially the loop when you create the <input> fields...

And also.. in the loop you have an ID in the <input>... that's not good at all!.. change it for class for example...

As an example, this is what I tried:

Main PHP

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
    <script type="text/javascript">
        jQuery(document).ready(function() {
            $('#theForm').submit(function(event) {
                event.preventDefault();
                createTheArray();
            });
        });
        function createTheArray(){
            // Create the Array
            var que_id_array = new Array();
            $('input[name="que_id[]"]').each(function(){
                 que_id_array.push($(this).val());
            });
            console.log(que_id_array); // output ["151", "152", "153"] 
            sendTheForm(que_id_array); // do the ajax call
        }
        function sendTheForm(que_id_array){
            // AJAX Call
            $.ajax({
                    type:"POST",
                    url: 'questionmastermodify.php',
                    data: { que_id:que_id_array},
                    success: function(data) {
                            $('.my_update_panel').html(data);
                    }
            });
        }
    </script>
</head>
<body>
    <form id="theForm">
        <?php
            // your original Array
            $arrayName = array(
                array('que_id' => '151'),
                array('que_id' => '152'),
                array('que_id' => '153')
            );
            foreach ($arrayName as $key => $questions) {
                echo "<input type='text' class='que_id' name='que_id[]' value='{$questions['que_id']}'>";
            }
        ?>
        <input type="submit" value="send" />
    </form>
    <div class="my_update_panel">result will be loaded here...</div>
</body>
</html>

And your questionmastermodify.php PHP file

<?PHP
    $que_id = $_REQUEST['que_id'];
    echo '<pre>';
    print_r($que_id);
    echo '</pre>';
?>

Result

After form submit.. the HTML will print out:

<pre>Array
(
    [0] => 151
    [1] => 152
    [2] => 153
)
</pre>

And in the console.log();

["151", "152", "153"]

Give a try and good luck!

share|improve this answer

The problem has been sorted out :

The culprit was the version of the jquery i was using. I was using jQuery JavaScript Library v1.3.2 - which caused this problem.

Using the latest : jquery-1.9.0.min.js solve the problem.

I am accepting the answer provided by #gmo as it brought me near to the problem solving and also about that helpful tip about not using id attribute in the Loop...

Thanks all...

share|improve this answer

You could encode the array into JSON:

data: { que_id: JSON.stringify(que_id_array)},

In the PHP, decode it:

$que_id = json_decode($_REQUEST['que_id']);
echo count($que_id);  
share|improve this answer
    
already tried this serialization method.... gives 0 –  Sandy505 Oct 14 '13 at 9:07

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.