[英]How to send a PHP array via AJAX?
在我的注冊頁面上,人們可以通過填寫一些輸入廣告復選框來注冊我的網站。 它基本上是一個將數據發送到帶有ajax的PHP頁面的表單,然后將其放入數據庫中。
例如,javascript從表單中獲取值
var w = _("website").value;
var bio = _("bio").value;
然后像這樣發送ajax.send("w="+w+"&bio="+bio);
這是應該的,但我想添加一些東西。
現在我有一些復選框,我希望在一個數組中通過ajax發布在一個變量中。 所以在我的表單中我有這段PHP:
$licensessql = mysqli_query($db_conx, "SELECT * FROM licenses");
while($licenserecord = mysqli_fetch_assoc($licensessql)) {
echo '<input type="checkbox" value="'.$licenserecord["license"].'" name="licenses[]" id="'.$licenserecord["license"].'"><label for="'.$licenserecord["license"].'">'.$licenserecord["license"].'</label><br>';
}
使用純PHP,這將工作,並將復選框中的所有值放在數組license []中,但我不知道如何使用ajax實現此目的。 如果我有5個復選框,我希望javascript var具有例如值'value1,value2,value3'並將其發布到PHP。
謝謝!
首先,我建議使用其他答案中發布的表單序列化。
要回答你的實際問題: "If I have 5 checkboxes, I want the javascript var to have for example the value 'value1,value2,value3' and have that posted to PHP."
這是一個完整的工作示例(使用3個復選框),它將生成一個javascript變量,您可以使用該變量傳遞給您的AJAX post方法以供PHP處理。
JSFiddle: https ://jsfiddle.net/o5q04vf0/
代碼片段:
$(document).ready(function() { $('#btnTest').click(function() { var checkBoxValues = $("[name=licenses\\\\[\\\\]]").map(function() { if ($(this).is(':checked')) { return this.value; } }).get().join(); alert(checkBoxValues); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" value="0" name="licenses[]" /> <input type="checkbox" value="1" name="licenses[]" /> <input type="checkbox" value="2" name="licenses[]" /> <button id="btnTest">Submit</button>
這將幫助您指導正確的方向,但請考慮通過AJAX傳遞數據,就像這里建議的其他成員一樣。
使用POST並將數組序列化為數據
$data = $('form').serialize(); $.post( "ajax/test.html", function($data) { $( ".result" ).html( data ); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="demo_form.asp"> <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br> <input type="checkbox" name="vehicle" value="Car" checked> I have a car<br> <input type="submit" value="Submit"> </form>
在PHP中,您使用$_POST['attributeName']
來讀取值
您可以使用serializeArray
函數發送所有表單數據。
Serialize
用於GET, serializeArray
對POST很有用。
$('#btn-send').click(function(e) {
$.ajax({
url: 'url-to-data-send',
method: 'post',
data: $(this).closest('form').serializeArray(); <--- send form data
}).done(function(response) {
alert('form sent');
})
});
或者您可以指定要發送的內容
var data = $(this).closest('form').serializeArray();
console.log(data);
**編輯**
這就是你想要的
var arr = [];
$.map( $(this).closest('form').find('input[name="chck[]"]:checked'), function( n, i ) {
arr.push({ name: $(n).attr('name'), value: $(n).val()});
});
console.log(arr);
開始了:
你需要Serialize功能! 以下是您如何使用它的一個問題:
HTML:
<form id="yourOrderFormId" method="post">
<input type="checkbox"/><input type="text"/>
<input type="checkbox"/><input type="text"/>
<button id="yourButtonId">Send</button>
</form>
jQuery Ajax:
<script>
$('#yourButtonId').click(function(e) {
var yourData = $("#yourOrderFormId").serialize();
$.ajax({
method: 'POST',
url: '/yourUrl',
data: yourData,
dataType: 'html',
success:success: function (result) {
alert("Your message sent!");
}
});
或者與serializeArray()一起使用;
console.log($('form').serializeArray());
在這里:在此輸入鏈接描述
希望能幫助到你;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.