gpt4 book ai didi

php - 简单的ajax表单提交报错

转载 作者:搜寻专家 更新时间:2023-10-31 22:11:22 25 4
gpt4 key购买 nike

我正在尝试实现 Ajax jQuery 表单提交。我在下面粘贴的代码不起作用。 day monthyear

出现 undefined index 错误

jQuery Ajax

<script>
$(document).ready(function() {
$('#cal_submit').click(function(event) {
if ($('select[name="day"]').val() == '') {
event.preventDefault();
alert('Please enter a day');
}
else {
event.preventDefault();
new_day = $('select[name="day"]').val();
new_month = $('select[name="month"]').val();
new_year = $('select[name="year"]').val();
$.get('function.php', { day: new_day, month: new_month, year:new_year} );
$('select[name="day"]').val('');
$('select[name="month"]').val('');
$('select[name="year"]').val('');
}
});
});
</script>

处理php所在的函数。

function calendar()
{


$j = mysql_real_escape_string($_GET['day']);
$F = ucwords(mysql_real_escape_string($_GET['month']));
$Y = mysql_real_escape_string($_GET['year']);
$date =" ".$F." ".$j.", ".$Y." ";

$query = "SELECT *
FROM calendar
WHERE event_day = '".$j."'
AND event_month = '".$F."'
AND event_year = '".$Y."'" ;
$run = mysql_query($query);
$norows = mysql_numrows($run);

if ($norows < 1){
echo "<div class=\"indexnoresult\">No TAP Event(s) for $date</div>" ;
} else {
while($row = mysql_fetch_array($run)) {

echo "<div class=\"indexnoresult\">Event(s) for $date<br>
Name : $row[event_name] <br>
Time : $row[event_time] <br>
Venue : $row[event_venue] <br>
</div>
" ;



}

} ?>

HTML 表单

     <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" >

Pick Date To View Events

<select name="day">
<option value =""> </option>
<option value ="1">1</option>
<option value ="2">2</option>
</select>

Month
<select name="month">
<option value =""> </option>
<option value ="january">January</option>
<option value ="february"> february </option>
<option value ="march">March</option>
</select>

Year:
<select name="year">
<option value =""> </option>
<option value ="2005">2005</option>
<option value ="2006">2006</option>
</select>

<input type='submit' name='cal_submit' id='cal_submit'>
</form>
</div>

请问我错在哪里。

最佳答案

您的 ajax 对象/数据中需要有相同的变量。

        day = $('select[name="day"]').val();
month = $('select[name="month"]').val();
year = $('select[name="year"]').val();
$.get('function.php', { day: day, month: month, year:year} );

第一个选择选项(调用 ajax 时的默认值,没有从选择列表中进行选择)是空白,这就是错误的原因。

此外,表单显示的是 POST 方法,而您的 ajax 是 GET。

关于php - 简单的ajax表单提交报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12520721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com