我正在使用 ajax 实现一个带有文本字段的弹出窗口,允许在下拉菜单中创建新选项而无需提交表单。它不太好用。关键问题似乎是我无法将字符串 new_option 传递给 php。为什么下面的 $_POST 数组是空的?
<script type="text/javascript">
$.ajaxSetup ({
cache: false
});
$('#upload_form option[value="addnew"]').click( function() {
// Show modal window
$('#add-new').modal('show');
$('#add-new-submit').on('click', function() {
// Get new option from text field
var new_option = $('#add-new-text').val();
console.log(new_option);
$.ajax({
type: "POST",
url: "<?php echo site_url(); ?>main/upload_page",
data: {new_option:'new_option'}
}).fail(function (jqXHR, textStatus, errorThrown) {
console.error("The following error occured: " + textStatus, errorThrown);
});
$('#add-new').modal('toggle');
});
});
</script>
<?php
$new_option = $_POST['new_option']; # <----- Gives Undefined index error
//var_dump($new_option);
// When I get this working this is how I'll get $new_option
if(isset($new_option))
{
$new_option = $_POST['new_option'];
$species = array($new_option => $new_option) + $species;
//print_r($new_option);
//print_r($species);
var_dump($new_option);
}
?>
在控制台中,我看到输入的字符串(new_option),但 php 在 $new_option = $_POST['new_option'] 处给出了错误(未定义索引:new_option)。我已经尝试了很多东西,但似乎无法将 new_option 转移到 php。我错过了一些明显的东西吗?提前感谢您的帮助!