我正在努力将一个名为$numbers
的数组传递给1,2,4
,就像说$.ajax
一样。它没有被识别,我不知道如何在下一页上获取此信息。这是我的代码:
<?php
// $numbers = 1,2,4;
print
"<div class='col-lg-6 col-md-6 col-sm-6 col-xs-12 col-md-pull-6 col-sm-pull-6'>".
"<div class='form-group'>".
"<div class='col-md-12'><strong>Forename:</strong></div>".
"<div class='col-md-12'><input type='text' class='form-control' id='forename'></div>".
"</div>".
"<div class='form-group'>".
"<div class='col-md-12'><strong>Surname:</strong></div>".
"<div class='col-md-12'><input type='text' class='form-control' id='surname'></div>".
"</div>".
"<div class='form-group'>".
"<div class='col-xs-12 col-md-6'>".
"<button id='button' class='btn btn-success btn_add'>Add</button>".
"</div>".
"</div>".
"</div>".
"</div>";
}
?>
<script type="text/javascript">
$(document).on("click", 'button.btn_add', function(){
var forename = document.getElementById('forename').value;
var surname = document.getElementById('surname').value;
var number = <?php echo $numbers; ?>;
if((forename == null || forename == "") && (surname == null || surname == "")){
alert("Please fill in all fields");
}
else {
$.ajax({
url: adduserinfo.php,
method: 'post',
dataType: 'json',
data: 'forename=' + forename + '&surname=' + surname + '&numbers=' + number
});
</script>
答案 0 :(得分:0)
您需要传递对象而不是查询字符串。
使用
如果$ numbers是数组,则使用
var number = <?php echo json_encode($numbers); ?>; // cant echo array
如果$ numbers是字符串,则使用
var number = "<?php echo $numbers; ?>"; // cant echo array
ajax call
$.ajax({
url: adduserinfo.php,
method: 'POST',
dataType: 'json',
data: { forename : forename, surname : surname, numbers : number }
});