1

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

Sandy505
  • 888
  • 3
  • 15
  • 26
  • Pass array from JS to PHP, use JSON – vaibhavmande Oct 14 '13 at 09:00
  • Why are you reading the value with $_REQUEST instead of $_POST? – Daniele Vrut Oct 14 '13 at 09: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 09:01
  • tried with $_REQUEST and $_POST both ... Same results :-( – Sandy505 Oct 14 '13 at 09:02
  • What does `var_dump($que_id)` return? – Mina Oct 14 '13 at 09:02
  • based on this generated array : ["2603", "2604", "2605", "2606", "2607", "2608", "2609", "2610", "2611", "2612", "2613", "2614", "2615", "2616", "2617", "2618", "2619", "2620", "2621", "2622", "2623", "2624", "2625", "2626", "2627", "2628", "2629", "2630", "2631", "2632", "2633", "2634", "2635", "2636", "2637", "2638", "2639", "2640", "2641", "2642", "2643", "2644", "2645", "2646", "2647", "2648", "2649", "2650", "2651", "2652"] it returns string(4) "2652" i.e. the last item in the array !! – Sandy505 Oct 14 '13 at 09:04
  • Did you tried with $.post? Examples: http://hayageek.com/jquery-ajax-post/ ps: you should check *$_POST* content with a *print_r($_POST)* – Daniele Vrut Oct 14 '13 at 09:07
  • yes i had tried that... referred this post : but still same results.. http://stackoverflow.com/questions/2013728/passing-javascript-array-to-php-through-jquery-ajax – Sandy505 Oct 14 '13 at 09:09
  • Use Firebug / Chrome Developer Tools / Dragonfly / etc to inspect the HTTP requests. Is the data you expect being sent? How is it formatted? – Quentin Oct 14 '13 at 09:19
  • this is what print_r($_POST) returns : Array ( [data] => que_id%5B%5D=19&qtype%5B%5D=single&que_desc%5B%5D=Dsiplays+information+about+system&ans1%5B%5D=info&ans2%5B%5D=sys&ans3%5B%5D=uname&ans4%5B%5D=sysname&true_ans%5B%5D=C&que_id%5B%5D=35&qtype%5B%5D=single&que_desc%5B%5D=Linux+do+not+require+any+anti+virus+software.&ans1%5B%5D=TRUE&ans2%5B%5D=FALSE&true_ans%5B%5D=A&que_id%5B%5D=36&qtype%5B%5D=single&que_desc%5B%5D=Linux+can+run+Oracle+software.&ans1%5B%5D=TRUE&ans2%5B%5D=FALSE&true_ans%5B%5D=A ) – Sandy505 Oct 14 '13 at 09:39
  • the above output is if the data is serialized and sent to php using var values = $("#form1").serialize(); – Sandy505 Oct 14 '13 at 09:42

3 Answers3

2

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!

gmo
  • 8,860
  • 3
  • 40
  • 51
0

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);  
MrCode
  • 63,975
  • 10
  • 90
  • 112
0

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

Sandy505
  • 888
  • 3
  • 15
  • 26