title: Cannot parse string variable with dynamically created JSON code in Jquery to be decoded and read in PHP
body: <p>I've created a script in jQuery that 
  1. automatically grabs specific values from elements that are dynamically created on the page by the user
  2. prepares these values into a query format for an insert by mysql 
  3. formats into JSON format to be send to php script via ajax where we will manipulate these JSON objects and perform the insert of these values into the mysql database.</p>
  
  <p>This is what I can't seem to figure out --</p>
  
  <p>When I manually set a variable string in javascript with the intended JSON format using test values, the values are sucessfully sent php script via ajax and the values are successfully inserted into the database.</p>
  
  <p>However, when I try to dynamically construct my string variable using the values from the dynamic content on the page, the php script throws an error :</p>
  
  <p>'Trying to get property of non-object'</p>
  
  <p>Error is being thrown at the 'foreach' loop in the php script where the '$theArray->questions' syntax is being used.</p>
  
  <p>Which is weird because when I use the 'alert' function in jquery using both methods (manually setting JSON and dynamically constructing the JSON, I get EXACTLY the same output. Also, when I set manually I am able to use the $.parseJSON function but when I use dynamic method using this function breaks the script so when I try dynamically I have to comment out this function. </p>
  
  <p>I suspect something funky is happening in the background of these variable based on the way I am assigning the values (manually and dynamically using a variable). Here is pertinent code :</p>
  
  <hr>
  
  <h2>JAVASCRIPT (JQUERY)</h2>
  
  <pre><code>//Manually create JSON string to send to php to insert values into mysql db
  //var questionsquestion = '{"questions":[{"question":"test16","type":"radio", "html":"&lt;input&gt;"},{"question":"test2","type":"checkbox", "html":"&lt;input&gt;"},{"question":"test3","type":"checkbox", "html":"&lt;input&gt;"}]}';
  //questionsquestion = $.parseJSON(questionsquestion);
  
  //set arrays
  var questionstype = [];
  var questionshtml = [];
  var questionsquestion = [];
  
  //Store all 'questions', 'question types', and 'html' for each fieldset into 3 separate arrays
  for(var i=1;i&lt;=formpreviewid;i++)
  {
   questionsquestion.push($("#formelement_"+i + " legend").text());
   questionstype.push($("#formelement_"+i).attr("class"));
   questionshtml.push($("#formelement_"+i)[0].outerHTML);
  };
  
  //format values for each fieldset into values format for mysql
  var questionsvalues = [];
  var index = 0;
  for(var i=1;i&lt;=formpreviewid;i++)
  { 
  questionsvalues.push('{"question":"'+questionsquestion[index]+'","type":"'+questionstype[index]+'","html":"'+questionshtml[index]+'"}');
  index++;
  };
  
  //dynamically format mysql values into JSON format
  var questionsvaluesjson = '{"questions":['+questionsvalues+']}';
  
  //questionsvaluesjson = $.parseJSON(questionsvaluesjson);
  
  var jsonArray = JSON.stringify(questionsvaluesjson);
  
  $.ajax({
  type: 'POST',
  url: '/project/templates/yoo_nano2/php/saveform.php',
  data: {'formpreviewhtml': formpreviewhtml, 'jsonArray': jsonArray},
  beforeSend:function(){
  // this is where we append a loading image
  $("#formpreview").append('&lt;div style="z-index:3000;" id="divoverlaycontainer"&gt;&lt;div id="loaderoverlay"&gt;&lt;div class="center" id="loader"&gt;&lt;img src="/project/templates/yoo_nano2/images/loader.gif"&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;');
  }, success:function(data){
  alert(data + "Saved form successfully.");
  //$('#viewresults').empty();
  $('#divoverlaycontainer').remove();
  // successful request; do something with the data
  //$('#viewresults').html(data);
  }, error:function(){
  // failed request; give feedback to user
  alert("Save was unsuccessful. Please try again.");
  $('#divoverlaycontainer').remove();
  }
  });
  });
  });
  </code></pre>
  
  <h2>    </h2>
  
  <h2>PHP</h2>
  
  <pre><code>&lt;?php 
  //create object/array from json data
  $theArray = json_decode($_POST['jsonArray']);
  
  //connect to database
  require_once('connect.php');
  $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
      if ($mysqli-&gt;connect_error) {
              die('Connect Error (' . $mysqli-&gt;connect_errno . ') '
              . $mysqli-&gt;connect_error);
  }
  //set query to drop table           
  $query = "DROP TABLE registrationquestions;";
  //execute the query
  $mysqli-&gt;query($query);
  //set query to create table 
  $query = "CREATE TABLE registrationquestions(
  id INT NOT NULL AUTO_INCREMENT ,
  PRIMARY KEY (id) ,
  question TEXT,
  type CHAR(255) ,
  html TEXT)";
  //execute the query
  $mysqli-&gt;query($query);
  //insert new questions
  
  $insert = "";
  
  foreach ($theArray-&gt;questions as $data)
  {   
    $insert .= '("' . $data-&gt;question . '", "' . $data-&gt;type . '", "' . $data-&gt;html . '")';
  }
  $insert = str_replace(")(","),(",$insert);
  
  $query = "INSERT INTO registrationquestions (question, type, html) VALUES " . $insert; 
  $mysqli-&gt;query($query);
      ?&gt;
  </code></pre>