title: Read and Save Data from Inputs to Database on Submit
body: <p>I have a form with multiple input fields. Using AJAX, I want the inputs to be saved into a database on submit. There are three different tables that the information will need to be inserted into, with each table taking a few of the inputs as you can see in my insert.php code. I have a lot of code and am not sure what I need to add in order to get the information that is entered, saved to the database on submit.</p>
  
  <p>What needs added/changed so that when I click submit, the data will be saved to the related tables in my database?</p>
  
  <p>I did not post my HTML code for the sake of cutting back on the amount of code in my question, but I can post it quickly if needed.</p>
  
  <p>JavaScript:</p>
  
  <pre><code>$( function() {
  
        var emailRegex = /^[a-zA-Z0-9.!#$%&amp;'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
        phoneRegex = /^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/,
        vendor = $( "#ven" ),
        prog_name = $( "#program_name1" ),
        prog_period = $( "#period" ),
        terms = $( "#year" ),
        type = $( "#type" ),
        name = $( "#name" ),
        email = $( "#email" ),
        phone = $( "#tel" ),
        tier = $( "#tier" ),
        purchase = $( "#purchase_min" ),
        multiplier = $( "#multiplier" ),
        uom = $( "#uom" ),
        retro = $( "#retro" ),
        guaranteed = $( "#guarenteed" ),
        paid = $( "#paid" ),
        allFields = $( [] ).add( vendor ).add( prog_name ).add( prog_period ).add( terms ).add( type ).add( name ).add( email ).add( phone ).add( tier ).add( purchase ).add( multiplier ).add( uom ).add( retro ).add( guaranteed ).add( paid );
    console.log(allFields);
  
      function checkRegexp( o, regexp, n ) {
        if ( !( regexp.test( o.val() ) ) ) {
          o.addClass( "ui-state-error" );
          return false;
        } else {
          return true;
        }
      }
  
     function submitInfo() {
        var valid = true;
        allFields.removeClass( "ui-state-error" );
  // ----- Validation for each input in add row dialog box -----
        valid = valid &amp;&amp; checkRegexp( vendor, /^[a-z]([0-9a-z_\s])+$/i, "Please enter a valid vendor name" );
        valid = valid &amp;&amp; checkRegexp( prog_name, /^[a-z]([0-9a-z_\s])+$/i, "Please enter a valid program name" );
        valid = valid &amp;&amp; checkRegexp( terms, /^(0|[1-9][0-9]*)$/, "Please enter valid terms" );
        valid = valid &amp;&amp; checkRegexp( name, /^[a-zA-Z ]*$/, "Please enter a valid name" );
        valid = valid &amp;&amp; checkRegexp( email, emailRegex, "Please enter a valid email" );
        valid = valid &amp;&amp; checkRegexp( phone, phoneRegex, "Please enter a valid phone number" );
        valid = valid &amp;&amp; checkRegexp( purchase, /^(0|[1-9][0-9]*)$/, "Please enter a valid purchase minimum" );
        valid = valid &amp;&amp; checkRegexp( multiplier, /^\d*\.?\d*$/, "Please enter a valid multiplier" );
        valid = valid &amp;&amp; checkRegexp( uom, /[-!$%^&amp;*()_+|~=`{}\[\]:";'&lt;&gt;?,.\/]/, "Please enter a valid uom" );
        valid = valid &amp;&amp; checkRegexp( retro, /^[0]{1}$/, "Please enter a valid entry for retro" );
        valid = valid &amp;&amp; checkRegexp( guaranteed, /^[0-1]{1}$/, "Please enter a valid entry for guaranteed" );
        valid = valid &amp;&amp; checkRegexp( paid, /^[a-z]([0-9a-z_\s])+$/i, "Please enter a valid entry for paid" );
        console.log(allFields);
        if ( valid ) {
          var dict = {};
          var errors = "";
          $.each(allFields, function(){
            var type = $(this).attr('id');
            var value = $(this).val();
            console.log(type + " : " + value);
            // ----- Switch statement that provides validation for each table cell -----
            switch (type) {
              case "ven":
                  dict["MR_Name"] = value;
                break;
              case "program_name1":
                  dict["Program_Name"] = value;
                break;
              case "period":
                  dict["Program_Year"] = value;
                break;
              case "year":
                  dict["Terms"] = value;
                  break;
              case "type":
                  dict["Coded"] = value;
                  break;
              case "name":
                  dict["MR_POC_N"] = value;
                  break;
              case "email":
                  dict["MR_POC_E"] = value;
                  break;
              case "tel":
                  dict["MR_POC_P"] = value;
                  break;
              case "tier":
                  dict["Tier"] = value;
                break;
              case "purchase_min":
                  dict["Tier Minimum"] = value;
                break;
              case "mulitplier":
                  dict["Multiplier"] = value;
                  break;
              case "uom":
                  dict["UOM"] = value;
                  break;
              case "retro":
                  dict["Retro"] = value;
                  break;
              case "guaranteed":
                  dict["Guaranteed"] = value;
                  break;
              case "paid":
                  dict["Pay"] = value;
                  break;
              }
          });
          console.log(dict);
  
      var selIndex = ven.selectedIndex;
      console.log();
      var selName = $("#ven").val();
  
          var request = $.ajax({
            type: "POST",
            url: "insert.php",
            data: { ven: selName, MR_Name: dict['MR_Name'], Program_Name: dict['Program_Name'], Program_Year: dict['Program_Year'], Terms: dict['Terms'], Coded: dict['Coded'], MR_POC_N: dict['MR_POC_N'],  MR_POC_E: dict['MR_POC_E'],  MR_POC_P: dict['MR_POC_P'], Tier: dict['Tier'], 'Tier Minimum': dict['Tier Minimum'], Multiplier: dict['Multiplier'], UOM: dict['UOM'], Retro: dict['Retro'], Guaranteed: dict['Guaranteed'], Pay: dict['Pay'] }
          });
  
  // Callback handler that will be called on failure
          request.fail(function (jqXHR, textStatus, errorThrown){
              console.error(
                  "The following error occurred: "+
                  textStatus, errorThrown
              );
          });
  
          // Callback handler that will be called regardless
          // if the request failed or succeeded
          request.always(function () {
  
          });
  
        }
        return valid;
      }
  });
  </code></pre>
  
  <p>Insert.php:</p>
  
  <pre><code>&lt;?php
  
    $MR_Name = $_POST['MR_Name'];
    $Program_Name = $_POST['Program_Name'];
    $Program_Year = $_POST['Program_Year'];
    $Terms = $_POST['Terms'];
    $Coded = $_POST['Coded'];
    $MR_POC_N = $_POST['MR_POC_N'];
    $MR_POC_E = $_POST['MR_POC_E'];
    $MR_POC_P = $_POST['MR_POC_P'];
    $Tier = $_POST['Tier'];
    $Tier_Minimum = $_POST['Tier Minimum'];
    $Multiplier = $_POST['Multiplier'];
    $UOM = $_POST['UOM'];
    $Retro = $_POST['Retro'];
    $Guaranteed = $_POST['Guaranteed'];
    $Pay = $_POST['Pay'];
  
    $host="xxxxxxxxx"; 
    $dbName="xxxx"; 
    $dbUser="xxxxxxxxxx"; 
    $dbPass="xxxxxxxx";
  
    $pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);
  
    $sql = "INSERT INTO Stage_Rebate_Master (MR_Name, MR_POC_N, MR_POC_E, MR_POC_P) VALUES (?, ?, ?, ?)";
    $sql1 = "INSERT INTO Stage_Rebate_Programs (Program_Name, Program_Year) VALUES (?, ?)";
    $sql2 = "INSERT INTO Stage_Rebate_Agreements (Terms, Tier, Tier Minimum, Multiplier, UOM, Retro, Guaranteed, Pay) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
  
    $stmt = $pdo-&gt;prepare($sql);
    $stmt1 = $pdo-&gt;prepare($sql1);
    $stmt2 = $pdo-&gt;prepare($sql2);
    $result = $stmt-&gt;execute(array($MR_Name, $MR_POC_N, $MR_POC_E, $MR_POC_P));
    $result1 = $stmt1-&gt;execute(array($Program_Name, $Program_Year));
    $result2 = $stmt2-&gt;execute(array($Terms, $Tier, $Tier_Minimum, $Multiplier, $UOM, $Retro, $Guaranteed, $Pay));
    echo json_encode($result);
    echo json_encode($result1);
    echo json_encode($result2);
  
  ?&gt;
  </code></pre>