Ajax选择带有实时消息的帖子

时间:2018-07-25 21:21:34

标签: php jquery ajax

我正在尝试从此POST获取实时消息和响应,但不适用于我。我想显示来自 timezone.php -errorsuccess的Ajax文本的地方?

HTML

<form id="timezone_form">
  <select name="timezone_select">
    <option value="1">test 1</option>
    <option value="2">test 2</option>
  </select>

  <input type="submit" value="Submit"/>
</form>

<div id="settings_ajax_message"></div>

JS

$(document).ready(function() {

  $("#timezone_form").submit(function() {

    $.ajax({
      type: "POST",
      url: "timezone.php",
      data: $(this).serialize()
    })

    .done(function(data) {
      $("#settings_ajax_message").html(data);
    })

    .fail(function() {
      $("#settings_ajax_message").html(data);
    });

    return false;
  });

});

timezone.php

<?php

  $timezone = $_POST['timezone_select'];
  if ($timezone == 1) {
    echo '<div class="ch-mes-fixed-warning-animation"><div class="ch-mes-warning">error</div></div>';
  } else {
    echo '<div class="ch-mes-fixed-success"><div class="ch-mes-success">success</div></div>';
  }

?>

2 个答案:

答案 0 :(得分:0)

首先,您必须了解为什么调用失败函数:

$(document).ready(function(e) {
  // Prevent the form from actually submitting to the server
  e.preventDefault();

  $("#timezone_form").submit(function() {

    $.ajax({
      type: "POST",
      url: "timezone.php",
      data: $(this).serialize()
    })

    // Status code 200-299 was returned. The data was successfully returned from the server.
    // PHP by default will always return 200 (Which signifys that the server processed the data, and is returning it)
    .done(function(data) {
      $("#settings_ajax_message").html(data);
    })

    // Status 300+ was returned from the server. This usually indicates an error on the server (ie. 404 not found, or 500 server error)
    .fail(function() {
      $("#settings_ajax_message").html(data);
    });

    return false;
  });

});

您有2种方法来处理此问题。 1,让您的服务器实际返回错误(通常是API的首选路由):

header("HTTP/1.0 422 Error");

在大多数情况下,您应该使用.fail捕获严重错误,就像服务器崩溃一样。让您的服务器返回200(这将处理完函数),但是请检查错误属性:

<?php
  // Tell javascript this is a json response:
  header("Content-type: application/json");
  $timezone = $_POST['timezone'];
  if ($timezone == 1) {
    echo json_encode(['error' => true, 'message' => '<div class="ch-mes-fixed-warning-animation"><div class="ch-mes-warning">error</div></div>']);
  } else {
    echo json_encode(['error' => false, 'message' => '<div class="ch-mes-fixed-success"><div class="ch-mes-success">success</div></div>');
  }

?>

还有你的ajax:

$(document).ready(function() {

  $("#timezone_form").submit(function() {

    $.ajax({
      type: "POST",
      url: "timezone.php",
      dataType: 'json', //IMPORTANT! PROCESS AS JSON
      data: $(this).serialize()
    })

    .done(function(data) {
       if (data.error) {
         console.log(data);
         //Do something with the error
         $("#settings_ajax_message").html(data.message);
       } else {
         //No error. data.message will be the message
       }
    })

    .fail(function() {
      $("#settings_ajax_message").html(data);
    });

    return false;
  });

答案 1 :(得分:0)

首先,您的字段名称为 timezone_select ,请看此处:<select name="timezone_select">。在PHP中,您检查另一个名称:$timezone = $_POST['timezone'];