title: How to do a Ajax Form Post with same Name elements?
body: <p>So I have a part of my form has the same 'Name' values that is going to be stored as an Array.</p>
  
  <p>What I'm trying to do is to send the values into an AJAX and then to PHP to update my database.</p>
  
  <p>The problem here is that I'm not sure on how to send the same input name values into the AJAX and allow my PHP to access these values.</p>
  
  <p>Code is shown below.</p>
  
  <p>EDIT: Note, I'm building a phonegap application so I cannot directly send the form to my php script.</p>
  
  <p>EDIT 2: Added my php script explaining how the information is going to be handled. </p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;form method="post"&gt;
  
      .
      . Other Input Forms
      .
  
      Subject: &lt;br&gt;
      &lt;select id="pref_subject[]" name="pref_subject[]"&gt;
      &lt;option value="0"&gt;Choose a Subject &lt;/option&gt;
      &lt;option value="1"&gt;English&lt;/option&gt;
      &lt;option value="2"&gt;A Math&lt;/option&gt;
      &lt;option value="3"&gt;E Math&lt;/option&gt;
      &lt;option value="4"&gt;Combined Science&lt;/option&gt;
      &lt;/select&gt;
  
      Subject: &lt;br&gt;
      &lt;select id="pref_subject[]" name="pref_subject[]"&gt;
      &lt;option value="0"&gt;Choose a Subject&lt;/option&gt;
      &lt;option value="1"&gt;English&lt;/option&gt;
      &lt;option value="2"&gt;A Math&lt;/option&gt;
      &lt;option value="3"&gt;E Math&lt;/option&gt;
      &lt;option value="4"&gt;Combined Science&lt;/option&gt;
      &lt;/select&gt;
  
      Subject: &lt;br&gt;
      &lt;select id="pref_subject[]" name="pref_subject[]"&gt;
      &lt;option value="0"&gt;Choose a Subject &lt;/option&gt;
      &lt;option value="1"&gt;English&lt;/option&gt;
      &lt;option value="2"&gt;A Math&lt;/option&gt;
      &lt;option value="3"&gt;E Math&lt;/option&gt;
      &lt;option value="4"&gt;Combined Science&lt;/option&gt;
      &lt;/select&gt;
  
      .
      . Other Input Forms
      .
  
      &lt;button id="submit"&gt;Submit&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>JQuery/Javascript:</p>
  
  <pre><code>$(document).ready(function(){
  
    $("#submit").on("click", function(e){
      e.preventDefault();
  
      $.ajax({
              url: serverURL + "/php/sendRequestProcess.php", 
              type:"POST", 
              data:{
                  sd_given_name : $("#sd_given_name").val(),
                  sd_family_name : $("#sd_family_name").val(),
                  sd_email : $("#sd_email").val(),
                  gender : $("#gender").val(),
                  pref_edu_level : $("#pref_edu_level").val(),
                  pref_subject :  $("#pref_subject[]").val(),
                  pref_area : $("#pref_area").val(),
                  estate : $("#estate").val(),
                  requestPrice : $("#requestPrice").val(),
                  lesson_hrs : $("#lesson_hrs").val(),
                  lesson_amt : $("#lesson_amt").val()
              }
            })
  
    });
  
  });
  </code></pre>
  
  <p>PHP:</p>
  
  <p>Before the posting the values of 'pref_subject' I will be posting the other values in the Request table in order to the 'last_id'.</p>
  
  <p>After getting the last id, I will then post the 'pref_subjects' into a second table.</p>
  
  <pre><code>    $connection = mysqli_connect($servername, $username, $password, $dbname);
  
    // Check connection
    if ($connection-&gt;connect_error) {
      die("Connection failed: " . $connection-&gt;connect_error);
    }
  
    $sd_given_name = addslashes($_POST["sd_given_name"]);
    $sd_family_name = addslashes($_POST["sd_family_name"]);
    $sd_email = $_POST["sd_email"];
    $gender = $_POST["gender"];
    $pref_edu_level = $_POST["pref_edu_level"];
    $pref_area = $_POST["pref_area"];
    $estate = $_POST["estate"];
    $requestPrice = $_POST["requestPrice"];
    $lesson_hrs = $_POST["lesson_hrs"];
    $lesson_amt = $_POST["lesson_amt"];
  
    $request_id;
    $pref_subject = $_POST["pref_subject"];
  
    $newRequest = "INSERT INTO Request (SD_Given_Name, SD_Family_Name, SD_Email, Gender, 
                                 Pref_Edu_Level_ID, Pref_Area_ID, Estate, 
                                 Request_Price, Lesson_Hrs, Lesson_Amt) 
            VALUES ('$sd_given_name', '$sd_family_name', '$sd_email', '$gender', 
                     $pref_edu_level, $pref_area, '$estate',
                     $requestPrice, $lesson_hrs, $lesson_amt)";
  
  
  
    if ($connection-&gt;query($newRequest) === TRUE) {
      $request_id = $connection-&gt;insert_id;
      //echo "New record created successfully. Last inserted ID is: " + $last_id;
    } 
  
    else {
      echo "Error: " . $newRequest . "&lt;br&gt;" . $connection-&gt;error;
    }
  
    for ($i = 0; $i &lt; count($pref_subject); $i++) {
      if ($pref_subject[$i] &gt; 0) {
        $addSubject = "INSERT INTO Request_Subject (Request_ID, Subject_ID)
        VALUES ($request_id, $pref_subject[$i])";
  
        $sendSubject = ($connection-&gt;query($addSubject));
  
        if ($sendSubject === FALSE) {
          echo "Error: " . $addSubject . "&lt;br&gt;" . $connection-&gt;error;
        }
      }
    }
  
    $connection-&gt;close();
  </code></pre>