Ajax调用返回空

时间:2017-06-26 09:24:14

标签: php ajax wordpress

我的表格如下:

<div class="any_reg">

            <div class="mail_area">
                <form name="any_reg" id="any_reg" method="POST" action="" class="mail_area">
                <table>
                <tr>
                <td><input type="text" name="email" id="mail" placeholder="Enter Your Email"></td>
                <input type="hidden" name="id" value="<?php echo $auction ?>">
                <td><input type="submit" name="submitmail" id="submitmail" value="Submit"></td>
                </tr>
                </table>
                </form>
            </div>
        </div>  

我有一个如下的ajax调用: 我已将网址设为$ajaxUrl= $dir."/watchemailajax.php";

  if(tre == true){
            $.ajax({url: '<?php echo $ajaxUrl; ?>',method:'POST',
                    data:$('#any_reg').serialize()
                    ,success: function(result){
                        alert(result);
                        if (result== "Success"){

                            alert("SUCCESS");   
                        }else
                        {
                            alert("Failed");
                        }

使用以下代码进入页面:

 <?php
    if (strstr($_SERVER['PHP_SELF'],WPA_PLUGIN_NAME) && isset($_GET['submitmail'])){
    check_ajax_referer( "WPA-nonce" );

      $auction_id = $_POST['auction_id'];
      $watch_email = strip_tags(stripslashes($_POST['email']));
      $tablename = $wpdb->prefix . "wp_wpa_watchlist";

      $sql = "INSERT INTO ".$tablename." (auction_id, watch_email) VALUES (".$auction_id.", '".$watch_email."' );";
      $result = $wpdb->query($sql);

      if ($result){
        echo "Success";
          // _e("You will be notified of any changes to this auction",'wpauctions');
      }
      else {
        echo "Failed";
      }
    }

调用返回空数据,没有别的。对于Ajax以及与之相关的任何事情,我都是新手。请帮我。这是我的错误

1 个答案:

答案 0 :(得分:1)

您可能无法满足此if语句if (strstr($_SERVER['PHP_SELF'],WPA_PLUGIN_NAME) && isset($_GET['submitmail'])){的要求,从而导致它跳过脚本的执行。将else语句添加到第一个if语句并添加一条消息,以便您可以看到它何时失败:

    if (strstr($_SERVER['PHP_SELF'],WPA_PLUGIN_NAME) && isset($_GET['submitmail'])){
        check_ajax_referer( "WPA-nonce" );

          $auction_id = $_POST['auction_id'];
          $watch_email = strip_tags(stripslashes($_POST['email']));
          $tablename = $wpdb->prefix . "wp_wpa_watchlist";

          $sql = "INSERT INTO ".$tablename." (auction_id, watch_email) VALUES (".$auction_id.", '".$watch_email."' );";
          $result = $wpdb->query($sql);

          if ($result){
            echo "Success";
              // _e("You will be notified of any changes to this auction",'wpauctions');
          }
          else {
            echo "Failed";
          }
        }
       else {

           echo "Failed if statement";

       }