我的 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) {
}
最佳答案
在你的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 元素。
关于php - jQuery AJAX POST 给出 undefined index ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12445829/