[英]Ajax Posted data not being read by $_POST in php
我試圖通過Ajax將表中字段的值發布到將處理數據的process.php中。
桌子:
<form name="items">
<table width="90%" border="1">
<tbody>
<tr>
<td width="26%">Item Name</td>
<td width="22%">If other then give name</td>
<td width="22%">Quantity</td>
<td width="16%">$/Unit</td>
<td width="14%">Total</td>
</tr>
<?php for ($i = 1; $i <= 10; $i++) {
?>
<tr>
<td>
<select name='itemname<?php echo $i;?>' id='itemname<?php echo $i;?>'>
<option value="other">other</option>
<?php
$qry_item_name = "SELECT DISTINCT item_name FROM bus_name_details";
$result_item_name = mysql_query($qry_item_name);
while($row_item_name = mysql_fetch_array($result_item_name)) {
$option .="<option>" . $row_item_name['item_name'] . "</option>";
echo $option;
}
?>
</select>
</td>
<td>
<input type="text" name="other<?php echo $i;?>" id="other<?php echo $i;?>"></td>
<td>
<input type="text" name="quan<?php echo $i;?>" id="quan<?php echo $i;?>" value="0"></td>
<td><input type="text" name="unit<?php echo $i;?>" id="unit<?php echo $i;?>" onkeyup="calculateTotal('<?php echo $i;?>')"></td>
<td><span name="total<?php echo $i;?>" id="total<?php echo $i;?>"></span></td>
</tr>
<?php }?>
</tbody>
</table>
</form>
<span id="subm3" class="subm3" >Submit </span>
Ajax:
<script>
$(document).on('click', '.subm3', function() {
var datas = {};
for ($i = 1; $i < 2; $i++) {
// drop down list of items
datas["item_name"+$i]= $('#itemname'+$i+' option:selected').attr('value')
// other name
datas["other"+$i]= $('input[name=other'+$i+']').val()
// quantity
datas["quan"+$i]= $('input[name=quan'+$i+']').val()
// price per unit
datas["unit"+$i]= $('input[name=unit'+$i+']').val()
}
$i = $i_limit-1;
$.ajax({
url: 'parts/process.php?order=3&items='+$i,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(datas)
});
});
</script>
然后的過程:
<?php
if ($order==3){
$loop = $_GET[items];
$other1= $_POST["other1"];
echo "other1 = ".$other1;
}
?>
在chrome inspector上,我得到這樣發送的數據作為源:{“ item_name1”:“ other”,“ other1”:“ Water Valve”,“ quan1”:“ 2”,“ unit1”:“ 2”}
像這樣解析:item_name1:“ other” other1:“ Water Valve” quan1:“ 2” unit1:“ 2”
但是process.php(獲得值的響應)的響應似乎只讀取了get值,而不是post值:other1 =
我對ajax相當陌生,希望有人可以幫助我指出我在這里出了問題。 這甚至可能不是我嘗試做的最簡單或最好的方法,但是目前我的大腦對此很有意義,因此我選擇了這條路線。 任何幫助表示贊賞
你可以嘗試data:datas
data: JSON.stringify(datas)
ajax請求中的data: JSON.stringify(datas)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.