提取ID以在Ajax中发布

时间:2018-12-22 20:05:44

标签: php jquery mysql ajax

我需要一些帮助,因为我在定义变量以将其发布到Ajax时遇到问题。我正在使用PHP,因为我正在从mysql数据库中获取数据以在PHP中输入信息,因此我想将ID发布到ajax中。

我已经在while循环之外定义了变量$autoid,但是当我使用jquery时我无法定义它们,因为当我尝试将它们发布到Ajax时它将显示空数据。

当我尝试这样做时:

var ID = $(this).data('deleteid');

$.ajax({
  type: 'POST',
  url: "sendtest.php",
  data: {ID : "deleteid"
  },

  success: function(resultData) {
    alert(resultData)
  }

这是完整的代码:

<?php
// Initialize the session
session_start();

// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login.php");
    exit;
}
else
{
    $link = mysqli_connect('localhost', 'mydbusername', 'mydbpassword', 'mydbpassword');
    $param_username = $_SESSION['username'];
    $autocampaign = $_SESSION["auto_campaign"];
    $autor_sql = "SELECT id, subject, day_cycle, enabled, delaysend FROM auto WHERE campaign ='$autocampaign' AND username ='$param_username'";
    $autoid = '';

    $results = mysqli_query($link, $auto_sql);

    if (mysqli_num_rows($results) > 0) 
    { 
        while ($row = mysqli_fetch_array($results))
        {
            $autoid = $row["id"];
            $autosubject = $row["subject"];
            $autodaycycle = $row["day_cycle"];
            $autoenabled = $row["enabled"];
            $autodelay = $row["delaysend"];
        }
    }
?>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<style type="text/css">


.calendar-content {
    float: left;
    width: 100%;
    min-height: 112px;
    border: 1px solid #ccc;
    position: relative;
    margin-top: -1px;
}
</style>


<div class="calendar-content">
  <div style="margin-left: 40px;margin-top: 20px; height: 40px;">
    <span id="autosubject" style="font-size: 25px;color: #0c7ac0;float: left;">Subject: <?php echo $autosubject ?> </span><br>
    <div style="margin-left: 35px; margin-top: 15px; height: 40px;">
      <form action="" method="post">
        <span style="font-size: 15px; color: #ccd5d9; float: left; margin-top: -1px"><a name="autoid" id="autoid" href="#contactModal" role="button" data-toggle="modal">Send a test</a> | <a name="deleteid" id="deleteid" href="/auto/delete_auto.php?id=<?php echo $autoid; ?>"> Delete </a> | Copy to Draft | Settings</span>
      </form>
    </div>
  </div><br>

  <!-- email Modal -->
  <div id="contactModal" class="modal fade" style="margin-top: 12%;" tabindex="-1" role="dialog">
    <div class="modal-dialog">
      <!-- Modal content-->
      <div class="modal-content" style="height: 250px;">
        <div class="modal-header" style="margin-bottom: 10px;">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h3 class="modal-title">Send A Test Email</h3>
        </div>
        <div class="modal-body" style="height: 65%;">
          <form action="" method="post">
            <label class="label-control" value="">Send test message to email address:</label>
            <select name="emails" id="emails" value="" style="width: 400px; margin-top: 10px; margin-bottom: 18px; margin-left: 60px;">
              <option selected="selected" value='title'>Title</option>";
            </select><br>
            <button name="send_email" id="send_email" type="submit" class="btn btn-primary" style="margin-left: 36%;" data-auto-id="<?php echo $autoid; ?>">Send Test</button>
          </form>
        </div>
      </div>
    </div>
  </div>


<script type="text/javascript">
$(function() {
  $('#send_email').click(function(e) {
    $(this)
      .html("<span><i class='fa fa-spinner fa-spin'></i> Sending...</span>")
      .prop("disabled", true);
    var ID = $(this).data('deleteid');

    $.ajax({
      type: 'POST',
      url: "sendtest.php",
      data: {ID : "deleteid"
      },

      success: function(resultData) {
        alert(resultData)
        $('#contactModal').modal('hide');
        $("#send_email")
          .html("Send Test")
          .prop("disabled", false);
      }
    });
  });
});
</script>
<?php
}
?>

这是sendtest.php:

<?php
// Initialize the session
session_start();

// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login.php");
    exit;

}
else if(isset($_POST))
{
    $id = $_POST[$autoid];
    echo "hello your id is...";
    echo $id;
}
?>

我想要实现的是,当我单击发送测试超链接时,将显示一个模式,并且当我在下拉列表框中选择后单击按钮时,我想从“删除”超链接中获取ID,所以我可以将它们发布在ajax中。我想在ajax中发布ID的原因是,当我想从mysql数据库中获取信息时,我希望停留在同一页面上。

能否请您举一个例子,说明如何从“删除”超链接中获取ID并将其发布到ajax中?

谢谢您了解我要做什么。

1 个答案:

答案 0 :(得分:1)

您的代码中存在几个问题:

第一:data: {ID : "deleteid"},应该是data: {ID : ID },(您要发送var,而不是字符串“ deleteid”)

2nd:var ID = $(this).data('deleteid');应该是var ID = $(this).data('auto-id');,因为数据属性是data-auto-id =“ ...

3rd:$id = $_POST[$autoid];将引发错误(未定义的变量)和通知(未定义的索引)。应该为$id = $_POST['ID'],因为您在此处将参数称为“ ID”:data: {ID : varname},

第四:您在这里缺少;alert(resultData)

这是更正的ajax部分:

var ID = $(this).data('auto-id');
$.ajax({
  type: 'POST',
  url: "sendtest.php",
  data: {ID : ID },

  success: function(resultData) {
    alert(resultData);
    $('#contactModal').modal('hide');
    $("#send_email")
      .html("Send Test")
      .prop("disabled", false);
  }
});