【问题标题】:Jquery form $_POST php empty? - My serialize seems to be failingJquery 表单 $_POST php 为空? - 我的序列化似乎失败了
【发布时间】:2014-02-20 19:00:15
【问题描述】:

这周我一直在为这个问题苦苦挣扎。

我正在使用 Jquery 将表单数据提交到同一页面并拉入一个 php 文件。但是 $_POST 似乎总是空的。经过一番搜索,我发现我需要先通过 Jquery 序列化表单的数据,但仍然没有运气。谁能向我解释一下我在这里做错了什么? - 我已经包含了表单、jquery 和 php

表格

<form action="" method="POST" id="searcher" name="searcher">

        <div class="customsearchul">
        <ul>
        <li><h4>Area:</h4></li>
        <li>All of London<input type="radio" name="area" value="london" checked="checked" /></li>
        <li>North<input type="radio" name="area" value="north" /></li>
        <li>East<input type="radio" name="area" value="east" /> </li>   
        <li>South<input type="radio" name="area" value="south" /></li>
        <li>West<input type="radio" name="area" value="west" /></li>
        <li>Central<input type="radio" name="area" value="central" /></li>
        </ul>  
        </div>

        <div class="customsearchul">
        <ul>
        <li><h4>Music:</h4></li>
        <li>House<input type="checkbox" name="music[]" value="house" /></li>
        <li>Techno<input type="checkbox" name="music[]" value="techno" /></li>
        <li>Trance<input type="checkbox" name="music[]" value="trance" /></li>
        <li>Electronica<input type="checkbox" name="music[]" value="electronica" /></li>  
        <li>Drum and Bass<input type="checkbox" name="music[]" value="drum-and-bass" /></li>
        <li>Garage<input type="checkbox" name="music[]" value="garage" /></li>
        <li>Dubstep<input type="checkbox" name="music[]" value="dubstep" /></li>
        <li>Trap<input type="checkbox" name="music[]" value="trap" /></li>
         </div> 
        </ul> 

        <div class="customsearchul" style="margin-top: 34px;">
        <ul>
        <li>Hip-Hop<input type="checkbox" name="music[]" value="hip-hop" /></li>
        <li>R'n'B<input type="checkbox" name="music[]" value="rnb" /></li>
        <li>Rock<input type="checkbox" name="music[]" value="rock" /></li>
        <li>Indie<input type="checkbox" name="music[]" value="indie" /></li>
        <li>Reggae<input type="checkbox" name="music[]" value="reggae" /></li>
        <li>Retro 80's/90's<input type="checkbox" name="music[]" value="retro" /></li>
        <li>Party Bangers<input type="checkbox" name="music[]" value="party-bangers" /></li>
        <li>Chart Hits<input type="checkbox" name="music[]" value="chart-hits" /></li>
        </ul>  
        </div>

        <div class="customsearchul">
        <ul>
        <li><h4>Search Type:</h4></li>
        <!--<li>Everything<input type="checkbox" name="occasion[]" value="debauchery" /></li> -->
        <li>Nightclubs<input type="checkbox" name="occasion[]" value="nightclub" checked="checked" /></li>
        <li>Events & Tickets<input type="checkbox" name="occasion[]" value="event" checked="checked" /> </li>   
        <li>Live Venues<input type="checkbox" name="occasion[]" value="live-venue" checked="checked" /></li>
        <li>Artist Interviews<input type="checkbox" name="occasion[]" value="artist" checked="checked" /></li>
        <li>Gay & Lesbian<input type="checkbox" name="occasiongay[]" value="gay-lesbian"/></li>
        <li>£££ Big Spender<input type="checkbox" name="occasionmoney[]" value="big-spender"/></li>
        <li>££ Keep it Average<input type="checkbox" name="occasionmoney[]" value="keep-it-average"/></li>
        <li>£ Cheap As Chips<input type="checkbox" name="occasionmoney[]" value="cheap-as-chips"/></li>
        </ul>
        </div>
        <center><input type="image" id="performsearch" src="http://www.*******.com/wp-content/themes/bigformat/images/calltoactionindexbutton.jpg">
        </center>

jQuery

<script type="text/javascript">
jQuery(document).ready(function($){

    $("#performsearch").click(function(){
        $("#displayresults").empty();
        $( "#displayresults" ).html('<center><img src="/wp-content/themes/bigformat/images/ajax-loader.gif"></center>').load( '/wp-content/themes/bigformat/template-home-search.php');

        $("searcher").submit(function(){
    var querystring = $(this).serialize();});
        return false;

    });

});
</script>

PHP

// Get Form Data For search
$locationarea = $_POST['area'];
$music = $_POST['music'];
$occasion = $_POST['occasion'];
$occasiongay = $_POST['occasiongay'];
$occasionmoney = $_POST['occasionmoney'];

echo 'User has selected';
print_r($_POST);
var_dump($_POST);
echo '<br><br>';

【问题讨论】:

  • 在您的 HTML 示例中包含
    标签。
  • 什么是$("searcher")
  • 我的错。代码引号没有覆盖表单。

标签: php jquery ajax forms post


【解决方案1】:

$("searcher") 在您的情况下不会做任何事情...您正在使用元素选择器,它正在寻找 &lt;searcher&gt; 元素。您应该使用 ID 选择器。

$("searcher") 更改为$("#searcher")

要实际发布您的表单,请使用以下内容:

$("searcher").submit(function(event) {
    // Prevent form from submitting the normal way
    event.preventDefault();

    $("#displayresults").html('<center><img src="/wp-content/themes/xxx/images/ajax-loader.gif"></center>').load( '/wp-content/themes/xxx/template-home-search.php');

    // Get URL from Form action attribute instead of window.location
    $.post($(this).attr("action"), $(this).serialize());
}

【讨论】:

  • 感谢@Werner,但不幸的是 $_POST 仍然是空的。 print_r 显示 Array (),var_dump 显示 array(0) {}。 - 我看不出为什么 $_POST 是空的?!
  • 您实际上并没有在代码中提交表单...您只是将内容放入var,然后向浏览器返回false。这就是为什么什么都没发生...查看api.jquery.com/serializeapi.jquery.com/submit
  • 我刚刚添加了一个实际将表单提交给server.php的示例。
  • 感谢@Werner,如果我要发布到之前加载的同一页面(使用 template-home-search.php)。我有点迷路了,到目前为止,这就是我想出的。代码中没有错误,但是从 php 中回显的数组仍然是空的。我在这里做错了什么?
  • jQuery(document).ready(function($) { $("#performsearch").click(function() { $("#displayresults").empty(); $( "#displayresults" ).html('&lt;center&gt;&lt;img src="/wp-content/themes/xxx/images/ajax-loader.gif"&gt;&lt;/center&gt;').load( '/wp-content/themes/xxx/template-home-search.php'); $("searcher").submit(function(event) { $.post(window.location, $(this).serialize()); }); event.preventDefault(); }); });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
相关资源
最近更新 更多