Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pandiyan's avatar

How to pass array from jquery ajax to php

hi i tried to pass array value from jquery to php using ajax post method but its not working.

the ajax code:

  QB['Title']=title;
  QB['Qtype']=Qtype;
  QB['Parrent']=Parrent;
  QB['Child']=Child;
  QB['options']=option;
     $.ajax({
              type:'post',
              async:false,
              url:'includes/feedback.php',
              data:{QB:QBarray,function:'Save_Question'},
              datatype:'json',
              beforesend:function()
              {
                    $(this).html('Please Wait...');
              },
              success:function(data)
              {
                 let json=JSON.parse(data)
                 toast_message(json.Result,json.Message);
              },
              error:function(data)
              {
                    console.log('Error');
              },
              complete:function()
              {
                    $(this).html('Submit');
              },

and PHP code is:

  elseif($_REQUEST['function']=='Save_Question')
  {
        // $QB_array[] = $_REQUEST['QB'];

        echo '<pre>';print_r(json_decode($_REQUEST['QB']));exit;

and got error like

Notice: Undefined index: QB in C:\xampp\htdocs\feedback\includes\feedback.php on line 33

0 likes
5 replies
design_studio's avatar

Can you print out the $_REQUEST ?

Also, maybe the problem is the data you are passing? shouldn't it be:

data:{QB:QB, function:'Save_Question'},
sauravs012's avatar

use JSON.stringify to convert an array to string in jQuery

QB['Title']=title;
QB['Qtype']=Qtype;
QB['Parrent']=Parrent;
QB['Child']=Child;
QB['options']=option;
var myJSON = JSON.stringify(QB);


data:{QB:myJSON,function:'Save_Question'}

Use json_encode in your controller function

dd(json_decode($_REQUEST['QB']));

Please or to participate in this conversation.