Ajax发布值返回空

时间:2018-12-22 23:57:53

标签: php jquery ajax

我需要一些帮助,因为我在使用ajax发布值时遇到问题。当我选择该值并单击一个按钮时,当我定义了变量$email并尝试使用$_POST['emails']时,它将发布空值。我在代码中添加了<form action="" method="post">,但没有任何区别,因此我的按钮代码或ajax中的某个地方出了问题。

我已经尝试过了:

if(isset($_POST['submit']))
{
    $email = $_POST['emails'];
}

<button name="send_email" id="send_email" type="submit" class="btn btn-primary" style="margin-left: 36%;" data-auto-id="<?php echo $autoid; ?>" data-send-email ="<?php echo $email; ?>">Send Test</button>

这:

<button name="send_email" id="send_email" type="submit" class="btn btn-primary" style="margin-left: 36%;" data-auto-id="<?php echo $autoid; ?>" data-send-email ="<?php echo $_POST['emails']; ?>">Send Test</button>

当我尝试使用$_POST['emails'];echo $email;时,该值仍显示为空。

这是完整的代码:

<?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', 'mydbname');
    $param_username = $_SESSION['username'];
    $auto_campaign = $_SESSION["auto_campaign"];
    $auto_sql = "SELECT id, subject, day_cycle, enabled, delaysend FROM auto WHERE campaign ='$auto_campaign' AND username ='$param_username'";
    $autoid = '';
    $email = '';

    $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"];
        }
    }

    if(isset($_POST['submit']))
    {
        $email = $_POST['emails'];

    }





?>

<!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="/delete_id.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='myemail@gmail.com'>myemail@gmail.com</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; ?>" data-send-email ="<?php echo $email; ?>">Send Test</button>
          </form>
        </div>
      </div>
    </div>
  </div>
<?php
}
?>

<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('auto-id');
    var EMAILS = $(this).data('send-email');

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

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

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))
{
    $param_username = $_SESSION['username'];
    $param_campaign = $_SESSION['auto_campaign'];
    $id = $_POST['ID'];
    $email = $_POST['EMAILS'];
    echo "your email address is";
    echo $email;
} 
?>

请问给我一个示例,当我单击按钮后,如何使用正确的方法获取值并使用ajax将其发布?

谢谢。

1 个答案:

答案 0 :(得分:0)

我看到的最明显的事情(假设您已经覆盖了响应/请求部分)是处理AJAX请求的代码中的这一行

if(isset($_POST['submit']))

因为您仅发送{ID : ID , EMAILS : EMAILS }“ ID”和“ EMAILS”,所以将永远不会设置“提交”,并且将永远不会执行分配$email的代码。

UPDATE1

这也会跳出来:

  $email = $_POST['emails'];

现在,如果您还记得,这是我们发送的{ID : ID , EMAILS : EMAILS },因此键EMAILSemails数组键检查是不同的,区分大小写。这应该是

 //in PHP7+
 $email = $_POST['EMAILS'] ?? false;

 //in PHP < 7
 $email = isset($_POST['EMAILS']) ? $_POST['EMAILS'] : false;