我对AJAX有问题。我想发送一个通过表单获取的值,然后在我的PHP脚本中使用该值进行处理。
但是我总是“未定义的索引”,我不明白为什么。
我的表格:
d1={1:1,-1:0}
d2={1:2,1:1,1:0}
print(d1) # {1: 1, -1: 0}
print(d2) # {1: 0} - both of 1:2,1:1 were overwritten by the last key 1:0 spec
这里有我的JS代码:
<form target="../app/DisplayController.php" method="post" class="form-group month-chose col-6 mx-auto" id="form-month">
<select class="form-control" id="month" name="month">
<option value=''>Chose :</option>
<?php foreach($viewVars[2] as $result): ?>
<option value='<?=$result->getId();?>'><?=$result->getMonth();?></option>
<?php endforeach; ?>
</select>
<button class="btn btn-success mt-3">Ok</button>
</form>
我的数据正常,我的consol.log返回“ month = 2”。
这是我的PHP测试脚本(chose-month.php):
$('.month-chose').on('submit', function(){
event.preventDefault();
var answers = $('#form-month').serialize();
console.log(answers);
$.ajax({
method : 'POST',
url : '../app/inc/chose-month.php',
data: answers,
success: $('.month-chose').load('../app/inc/chose-month.php')
});
})
当我加载模板时,我有一个著名的“月:未定义索引”。
感谢您的帮助!