【问题标题】:Ajax Posted data not being read by $_POST in phpphp中的$_POST未读取Ajax发布的数据
【发布时间】:2018-11-03 23:16:30
【问题描述】:

我正在尝试通过 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>

阿贾克斯:

    <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 检查器上,我知道数据是这样作为源发送的: {"item_name1":"other","other1":"水阀","quan1":"2","unit1":"2"}

并像这样解析: item_name1:“其他” other1:“水阀” 权1:“2” 单元1:“2”

但是来自 process.php(获取值的那个)的响应似乎只读取了 get 值而不是 post 值: 其他1 =

我对 ajax 非常陌生,希望有人能帮助我指出我在这里出错的地方。这甚至可能不是做我想做的事情的最简单或最好的方法,但目前这对我的大脑来说有点道理,所以我走这条路。任何帮助表示赞赏

【问题讨论】:

    标签: php ajax


    【解决方案1】:

    你能在ajax请求中尝试data:datas insted of data: JSON.stringify(datas)

    参考:how to send multiple data with $.ajax() jquery

    【讨论】:

    • 我试过了,还是不行。在 chrome 检查器中我看到: item_name1=other&other1=water+heater&quan1=2&unit1=2 但 php 没有在 $other1= $_POST["other1"];
    • 试试 print_r($other1); echo "other1 = ".$other1; thn chk
    猜你喜欢
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多