jQuery AJAX POST提供未定义的索引

时间:2012-09-16 09:35:26

标签: php javascript jquery post

我的eventinfo.php提供以下输出:

<br />
<b>Notice</b>:  Undefined index:  club in <b>/homepages/19/d361310357/htdocs/guestvibe/wp-content/themes/yasmin/guestvibe/eventinfo.php</b> on line <b>11</b><br />
[]

HTML(index.php):

<select name="club" class="dropdown" id="club">
<?php getClubs(); ?>
</select>

jQuery(index.php):

<script type="text/javascript">

    $(document).ready(function() {
        $.ajax({
            type: "POST",
            url: "http://www.guestvibe.com/wp-content/themes/yasmin/guestvibe/eventinfo.php",
            data:  $('#club').serialize(),
            success: function(data) {
                $('#rightbox_inside').html('<h2>' + $('#club').val() + '<span style="font-size: 14px"> (' + data[0].day + ')</h2><hr><p><b>Entry:</b> ' + data[0].entry + '</p><p><b>Queue jump:</b> ' + data[0].queuejump + '</p><br><p><i>Guestlist closes at ' + data[0].closing + '</i></p>');
                },
            dataType: "json"
        });
    });

    $('#club').change(function(event) {
        $.ajax({
            type: "POST",
            url: "http://www.guestvibe.com/wp-content/themes/yasmin/guestvibe/eventinfo.php",
            data:  $(this).serialize(),
            success: function(data) {
                $('#rightbox_inside').hide().html('<h2>' + $('#club').val() + '<span style="font-size: 14px"> (' + data[0].day + ')</h2><hr><p><b>Entry:</b> ' + data[0].entry + '</p><p><b>Queue jump:</b> ' + data[0].queuejump + '</p><br><p><i>Guestlist closes at ' + data[0].closing + '</i></p>').fadeIn('500');
                },
            dataType: "json"
        });

    });

</script>

我可以从jQuery运行警报,因此它处于活动状态。

我已经从旧版本的网站上复制了这个,但是我已经改变了文件结构(通过移动到WordPress),所以我怀疑这些变量甚至可能都没有到达eventinfo.php ...

index.php在wp-content / themes / cambridge中,eventinfo.php在wp-content / themes / yasmin / guestvibe中,但我试图通过完全引用URL来避免构造问题。

有什么想法吗?

修改

抱歉,忘记了eventinfo.php。我怀疑只有第3行才有用,但我可能错了。

include('functions.php');
connect();
$night = $_POST['club'];
$night = mysql_real_escape_string($night);

$query = "SELECT * FROM nights WHERE name = '" .$night. "'";

    $result = mysql_query($query);
    $items = array();

    if($result && mysql_num_rows($result) > 0) { 
        while ($row = mysql_fetch_array($result)) { 
        $items[] = array("entry"=>$row['entry'], "day"=>getLongDateString($row['day']), "queuejump"=>$row['queue jump'], "closing"=>$row['closing']);
        }
    } 

    mysql_close(); 
    // convert into JSON format and print

    echo json_encode($items);
?>

vardump [$ _ POST]给出:

array(0) {
}

1 个答案:

答案 0 :(得分:2)

在index.php文件中,您有:

<select name="club" class="dropdown" id="club">
<?php getClubs(); ?>
</select>

将此更改为(已更新)

<form name="myform" id="myform" action="submit.php" method="POST">
<select name="club" class="dropdown">
<?php getClubs(); ?>
</select>
</form>

然后将您的代码更改为$('#myform').serialize()而不是$('#club').serialize()

$ _ POST ['club']为空,这就是你收到通知的原因。它是null,因为它没有正确提交。必须提交表单,而不是select元素。表单本身必须是serialize(),而不是select元素。