dqlk31541 2018-06-11 09:56
浏览 66

Ajax传递提示输入到php

A form is dynamically created with PHP loop and presents one row from DB. I'm asking a user for input through prompt when he changes qty, but when I send it with AJAX to PHP it's empty. I guess it's because hidden input "new_message" is filled at the same time AJAX is sending values to PHP script so at that moment is blank. For the first row generated I get a message, but not for any other row.

Can you help me how to pass prompt input to PHP script?

//Update qty on article    
$('.update_qty').on('change', function() {

  var message = prompt("Upišite razlog za izmjenu količine:");
  //e.preventDefault();

  //Get message value
  $('#new_message').val(message);

  var data = $('#form1').serializeArray();

  if (message != "" || message != NULL) {
    $.ajax({
      type: 'POST',
      url: 'update_qty.php',
      data: data,
      success: function(data) {
        alert(data);
      }
    });
  } else {
    alert("Empty");
    e.PreventDefault();
    return false;
  }
});
<tbody>
<?php if (!empty($sales_plan_list)) {
      foreach($sales_plan_list as $sales_key => $sales_value) {
 ?>
<tr class="new">
   <td>
   <a href="view_product_sales.php?id=<?php echo $sales_value['article_id']; ?>">
   <?php echo $sales_value['article_no']; ?>
   </a>
   </td>
   <td><?php echo $sales_value['description']; ?></td>
   <td><?php echo myNumberFormat($sales_value['rrp']); ?></td>
   <td>
   <form name="form1" id="form1" method="POST" action="update_qty.php">
   <input type="text" name="update_qty" class="update_qty" id="qty" value="<?php echo $sales_value['qty'] ?>"><?php echo ' Kom'; ?>
   <input type="hidden" name="article_id" id="article_id" value="<?php echo $sales_value['article_id']; ?>">
   <input type="hidden" name="sales_plan_id" id="sales_plan_id" value="<?php echo $sales_plan_id; ?>">
   <input type="hidden" name="product_mix_id" id="product_mix_id" value="<?php echo $product_mix_id; ?>">
   <input type="hidden" name="new_message" id="new_message" value="">
   </form>
   </td>
   <td>
   <a href="view_product_sales.php?id=<?php echo $sales_value['article_id']; ?>" class="ti-eye" title="Pogledaj"></a> &nbsp;&nbsp;
   <a href="edit_product_sales.php?id=<?php echo $sales_value['article_id']; ?>" class="ti-settings" title="Izmjeni"></a> &nbsp;&nbsp;
   <a href="delete_product_sales.php?id=<?php echo $sales_value['article_id']; ?>$sales_plan_id=<?php echo $sales_plan_id; ?>" class="ti-trash" title="Obriši"></a>
  </td>
   </tr>
   <?php
   }
   }
   ?>                                                                    
   </tbody>
  • 写回答

1条回答 默认 最新

  • dqc42632 2018-06-11 10:10
    关注

    The problem is that you have multiple elements with the same id. You should NOT do that. You check the value in jQuery and then send different form data.

    This is all just quessing, I am unable to check it. But try changing id to class and make sure that you send the right form data.

    For example change this

    <form name="form1" id="form1" method="POST" action="update_qty.php">
    

    to this

    <form name="form1" id="form<?= $sales_key; ?>" method="POST" action="update_qty.php">
    
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程