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

asked Oct 14, 2013 at 8:57
11
  • Pass array from JS to PHP, use JSON Commented Oct 14, 2013 at 9:00
  • Why are you reading the value with $_REQUEST instead of $_POST? Commented Oct 14, 2013 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 } Commented Oct 14, 2013 at 9:01
  • tried with $_REQUEST and $_POST both ... Same results :-( Commented Oct 14, 2013 at 9:02
  • What does var_dump($que_id) return? Commented Oct 14, 2013 at 9:02

3 Answers 3

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!

answered Oct 14, 2013 at 10:09
Sign up to request clarification or add additional context in comments.

Comments

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); 
answered Oct 14, 2013 at 9:01

Comments

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

answered Oct 15, 2013 at 6:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.