我有一个具有多个不同输入的表单。表格的两个部分是一个下拉框和三个联系信息输入框。每当我在下拉框中进行选择时,它都会自动将信息填充到文本框中。我还在文本框旁边有一个保存按钮。一旦我点击保存按钮,这应该允许我更新该字段。因此,例如,如果我将下拉框值硬编码到我的update语句中,它就可以工作,但是如果不对它进行硬编码,我就无法使其正常工作。

我将如何解决这个问题?由于它已经存在,所以我不在SUBMIT上运行它。我只是单击表单中的保存按钮,该按钮会自动更新信息。

下拉框代码:

<div>
<div id="vendor">
<strong>Vendor:</strong>
</div>

<div class="align">
<select name="vendor_dropdown" id="ven" onChange="updateinput();">
    <option value="">Choose a Vendor</option>
        <?php foreach($users->fetchAll() as $user): ?>
            <option
                    data-name="<?php echo $user['MR_POC_N'];?>"
                    data-email="<?php echo $user['MR_POC_E'];?>"
                    data-phone="<?php echo $user['MR_POC_P'];?>"
            >
                <?php echo $user['MR_Name'];?>
            </option>
        <?php endforeach; ?>
</select>
</div>

</div>


自动填充的联系信息代码:

<div>
<div id="contact_info">
<strong>Contact Information:</strong><br>
</div>

<div class="align1">
<table align="left" id="contact">
<tr align="left">
    <th>Name</th>
    <th>Email</th>
    <th>Phone Number</th>
</tr>
    <tr>
        <td><input type="text" id="name" class="name" name="name"></td>
        <td><input type="email" id="email" class="email" name="email"></td>
        <td><input type="tel" id="tel" class="tel" name="number"></td>
        <td><input type="button" class="edit" name="edit" value="Save"></td>
    </tr>
</table>
</div>

</div>


用于编辑信息的Javascript:

// ----- Edit Contact Info -----

$(document).on("click", "#contact .edit", function () {
  var $this = $(this);
  var tds = $this.closest('tr').find('td').filter(function () {
    return $(this).find('.edit').length === 0;
  });
  if ($this.val() === 'Edit') {
   /*if($this.id !== '.mr_id') {
        tds.not('.mr_id').not('.mr_name').prop('contenteditable', true);
   }*/
  } else {
    var isValid = true;
    var errors = '';
    $('#myDialogBox').empty();
    var elements = tds;
    if (tds.find('input').length > 0) {
      elements = tds.find('input');
    }
    var dict = {};
    elements.each(function (index, element) {
      var type = $(this).attr('class');
      var value = (element.tagName == 'INPUT') ? $(this).val() : $(this).text();
      console.log(type);
      // ----- Switch statement that provides validation for each table cell -----
      switch (type) {
        case "ven":
              dict["MR_Name"] = value;
            break;
        case "name":
          if (value == value.match(/^[a-zA-Z\s]+$/)) {
              dict["MR_POC_N"] = value;
            break;
          }
          else {
            isValid = false;
            errors += "Please enter a valid Name\n";
          }
          break;
        case "email":
          if (value == value.match(/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/)) {
              dict["MR_POC_E"] = value;
            break;
          }
          else {
            isValid = false;
            errors += "Please enter a valid Email\n";
          }
          break;
        case "tel":
          if (value == value.match('^[0-9 ()+/-]{10,}$')) {
              dict["MR_POC_P"] = value;
            break;
          }
          else {
            isValid = false;
            errors += "Please enter a valid Phone Number\n";
          }
          break;
      }
    })

    if (isValid) {
        console.log(dict);

    var selIndex = ven.selectedIndex;
    console.log();
    var selName = $( "#ven option:selected" ).text();

      //$this.val('Save');
      //tds.prop('contenteditable', false);
      var request = $.ajax({
      type: "POST",
      url: "update.php",
      data: { ven: selName, MR_Name: dict['MR_Name'], MR_POC_N: dict['MR_POC_N'],  MR_POC_E: dict['MR_POC_E'],  MR_POC_P: dict['MR_POC_P'] },
      success: function(data){
          console.log(selName);
          }
    });

        request.done(function (response, textStatus, jqXHR){
          if(JSON.parse(response) == true){
            console.log("Data saved");
          } else {
            console.log("Data failed to save");
            console.log(response);
            console.log(textStatus);
            console.log(jqXHR);
          }
        });

        // Callback handler that will be called on failure
        request.fail(function (jqXHR, textStatus, errorThrown){
            // Log the error to the console
            console.log(textStatus);
            console.log(jqXHR);
            console.error(
                "The following error occurred: "+
                textStatus, errorThrown
            );
        });

        // Callback handler that will be called regardless
        // if the request failed or succeeded
        request.always(function () {

        });
    } else {
      alert(errors);
    }
  }
});


update.php:

<?php

  $MR_Name = $_POST['ven'];
  $MR_POC_N = $_POST['MR_POC_N'];
  $MR_POC_E = $_POST['MR_POC_E'];
  $MR_POC_P = $_POST['MR_POC_P'];

  $host="xxxxxxx";
  $dbName="xxxxxxxxx";
  $dbUser="xxxx";
  $dbPass="xxxxxxxx";

  $pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);

  $sql = "UPDATE Stage_Rebate_Master SET MR_POC_N = :MR_POC_N, MR_POC_E = :MR_POC_E, MR_POC_P = :MR_POC_P WHERE MR_Name = '$MR_Name'";

  $stmt = $pdo->prepare($sql);
  $stmt->bindValue(':MR_Name', $MR_Name);
  $stmt->bindValue(':MR_POC_N', $MR_POC_N);
  $stmt->bindValue(':MR_POC_E', $MR_POC_E);
  $stmt->bindValue(':MR_POC_P', $MR_POC_P);
  $result = $stmt->execute();
  echo json_encode($result);

?>

最佳答案

尝试使用此代码行而不是您的值声明。

var selName = $("#ven").val();

关于javascript - 在保存时通过Ajax运行UPDATE查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41614663/

10-12 13:45