0

I am sending a javascript array by using jquery to a php script. In ajax I have a myarr variable but this variable can't be accessed in php. Error is :

Undefined index myarr

Please Please help me. this is really important.

This is my jquery code:

for (var n = 0; n < arraySubId.length; n++) { var ansArr = []; for (var m = 1; m <= 11; m++) { ansArr[m - 1] = $('#' + arraySubId[n] + '-' + m + '').val(); } * $.ajax({ type: 'POST', data: ({ 'sub_id': arraySubId[n], 'myarr': ansArr }), url: 'Scripts/insert_feedback.php', success: function(data) { if (data == "1") { } else { alert(data); } } }); * } 

When I access myarr variable in php, it's displaying:

Undefined index myarr.

Please Please help me.

And this is my php code:

<?php session_start(); $prn = $_SESSION['username']; $sub_id = $_POST['sub_id']; $ans_arr = $_REQUEST['myarr']; include 'dbclass.php'; $dbclass = new DBClass; $mysqli = $dbclass->connect(); $query = "INSERT INTO ".$sub_id."(student_prn, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, date) values('$prn', '$ans_arr[0]','$ans_arr[1]','$ans_arr[2]', '$ans_arr[3]','$ans_arr[4]','$ans_arr[5]','$ans_arr[6]', '$ans_arr[7]','$ans_arr[8]','$ans_arr[9]','$ans_arr[10]', now())"; $msg = $dbclass->insert($query); echo $msg; ?> 
3
  • This should answer your question: stackoverflow.com/questions/9001526/… Commented Jan 31, 2016 at 19:12
  • You have to serialize arrays and objects before you can send them via ajax Commented Jan 31, 2016 at 19:15
  • how i can do this Please tell me, And it was working perfectly by this way, Please Please help me Commented Jan 31, 2016 at 19:29

1 Answer 1

0

Try to convert your array to Json String and send it in JS, and in PHP decode it using json_decode()

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry but this is not possible in this case because lot of data going to be generated dynamically Sorry
Why you use $_POST['']; for "sub_id" data, and $_REQUEST for "myarr" ?
Yes I treid $_POST[""] both of them BUT not working.. :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.