在提交时从输入读取数据并将数据保存到数据库

时间:2017-01-12 18:17:29

标签: javascript php ajax database sql-insert

我有一个包含多个输入字段的表单。使用AJAX,我希望在提交时将输入保存到数据库中。有三个不同的表需要插入信息,每个表都有一些输入,正如您在insert.php代码中看到的那样。我有很多代码,并且不确定我需要添加什么才能获取输入的信息,并在提交时保存到数据库中。

需要添加/更改哪些内容,以便在我单击提交时,数据将保存到我的数据库中的相关表中?

为了减少我的问题中的代码量,我没有发布我的HTML代码,但如果需要,我可以快速发布。

JavaScript的:

$( function() {

      var emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[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 && checkRegexp( vendor, /^[a-z]([0-9a-z_\s])+$/i, "Please enter a valid vendor name" );
      valid = valid && checkRegexp( prog_name, /^[a-z]([0-9a-z_\s])+$/i, "Please enter a valid program name" );
      valid = valid && checkRegexp( terms, /^(0|[1-9][0-9]*)$/, "Please enter valid terms" );
      valid = valid && checkRegexp( name, /^[a-zA-Z ]*$/, "Please enter a valid name" );
      valid = valid && checkRegexp( email, emailRegex, "Please enter a valid email" );
      valid = valid && checkRegexp( phone, phoneRegex, "Please enter a valid phone number" );
      valid = valid && checkRegexp( purchase, /^(0|[1-9][0-9]*)$/, "Please enter a valid purchase minimum" );
      valid = valid && checkRegexp( multiplier, /^\d*\.?\d*$/, "Please enter a valid multiplier" );
      valid = valid && checkRegexp( uom, /[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]/, "Please enter a valid uom" );
      valid = valid && checkRegexp( retro, /^[0]{1}$/, "Please enter a valid entry for retro" );
      valid = valid && checkRegexp( guaranteed, /^[0-1]{1}$/, "Please enter a valid entry for guaranteed" );
      valid = valid && 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;
    }
});

Insert.php:

<?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->prepare($sql);
  $stmt1 = $pdo->prepare($sql1);
  $stmt2 = $pdo->prepare($sql2);
  $result = $stmt->execute(array($MR_Name, $MR_POC_N, $MR_POC_E, $MR_POC_P));
  $result1 = $stmt1->execute(array($Program_Name, $Program_Year));
  $result2 = $stmt2->execute(array($Terms, $Tier, $Tier_Minimum, $Multiplier, $UOM, $Retro, $Guaranteed, $Pay));
  echo json_encode($result);
  echo json_encode($result1);
  echo json_encode($result2);

?>

0 个答案:

没有答案