[英]Javascript send array to PHP using ajax
我正在嘗試使用ajax將數組從javascript發送到PHP腳本。 這是我到目前為止的代碼。
<?php
$i = 1;
while (++$i <= $_SESSION['totalcolumns']) {
$range = $_SESSION["min-column-$i"] . ',' . $_SESSION["max-column-$i"];?>
<br><?php echo "Keyword" ?>
<?php echo $i -1 ?>
<br><input type="text" data-slider="true" data-slider-range="<?php echo $range ?>" data-slider-step="1">
<?php } ?>
<button type="button" >Update</button>
<script>
$("[data-slider]")
.each(function () {
var range;
var input = $(this);
$("<span>").addClass("output")
.insertAfter(input);
range = input.data("slider-range").split(",");
$("<span>").addClass("range")
.html(range[0])
.insertBefore(input);
$("<span>").addClass("range")
.html(range[1])
.insertAfter(input);
})
.bind("slider:ready slider:changed", function (event, data) {
$(this).nextAll(".output:first")
.html(data.value.toFixed(2));
});
$(".output")
.each(function() {
var parms = [];
parms.push($(this).text());
});
</script>
<script>
function loadXMLDoc()
{
$.ajax({
type: "POST",
url: "update.php",
data: { value : $(parms).serializeArray() },
success: function(data)
{
console.log (data);
}
});
}
$("button").on('click',function(){ loadXMLDoc(); });
</script>
在$ .output函數中,我使用parms []數組存儲所有UI滑塊值,這些值將在loadXMLDoc()函數中定義的按鈕單擊事件上傳遞給下一個PHP腳本頁面。 在我的PHP頁面中,按如下方式訪問它們。
<?php
$uid = $_POST['value'];
echo "Am I getting printed";
echo $uid;
// Do whatever you want with the $uid
?>
但是,我無法在我的update.php腳本中查看數據。 有人可以讓我知道做錯了什么嗎?
這是到目前為止我的工作的鏈接 。
serializeArray返回json對象,也許您可以在php腳本中嘗試json_decode,就像這樣:
$uid_arr = json_decode($uid,true);
print_r($uid_arr);
只需使用
data: $(parms).serializeArray()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.