如何获取文本框的值并在查询中使用

时间:2017-09-30 07:11:21

标签: javascript php jquery

我使用$ _POST获取order_id的值,但它并没有立即安静下来。我需要先点击Button" Show Food"或任何按钮(如下图所示)弹出我想输出的表格。

如果我将WHERE order_id = $ order_id更改为WHERE order_id = 724,它将立即弹出该数字的值(724)但是它是食物表中所有订单的输出。所以我的问题是如何为特定的order_id输出正确的输出?

示例我有2个订单order_id 1和order_id 2,值1是汉堡和披萨,2的值是沙拉和三明治,所以当我点击1时,食物表的价值只是汉堡和披萨而且然后当我点击2时,价值应该是沙拉和三明治而不是汉堡和披萨。

This is the error I get

This is after I click Show Food or any button

我希望你理解我的解释,我希望你的帮助,谢谢!

更新:我忘了提到这是在我的订单中。只是要清楚

<div class="form-group">
  <label for="order_id" class="col-sm-2 control-label">Order ID</label>
  <div class="col-lg-3"> 
    <input type="text" input style="width:500px" class="form-control" name="ORDER_ID" id="ORDER_ID" placeholder="" value="" required="required" readonly> 
  </div>
</div>

<?php 
  $order_id = trim(addslashes($_POST['ORDER_ID']));
  $sql = "SELECT food_name, special_request, quantity, amount 
          FROM cart_tbl
          WHERE order_id=$order_id";
  $result = mysqli_query(connection2(), $sql);
?>
<table class="table table-hover table-bordered">
  <thead>
    <tr> 
      <th>Food</th>
      <th>Special Request</th>
      <th>Quantity</th>
      <th>Amount</th> 
    </tr>
 </thead> 
 <?php
   if(mysqli_num_rows($result)>0) {
     while($row = mysqli_fetch_array($result)) {
       ?>
       <tr>
         <td><?php echo $row["food_name"];?></td>
         <td><?php echo $row["special_request"];?></td>
         <td><?php echo $row["quantity"];?></td>
         <td><?php echo $row["amount"];?></td>
       </tr>
       <?php
     }
   }
?>

</table>
</div>
<div class="modal-footer">
  <button type="submit" input 
     style="background-color: #FF0000; color:white; float:left" 
     name="showFood" id="showFood" class="btn btn-primary" 
     onclick="if(!confirm('Are you sure you want to see food order?')){return false;}" > Show Food
  </button>
  <button type="submit" input 
     style="background-color: #4CAF50; color:white" 
     name="submitDelivered" id="submitDelivered" 
     class="btn btn-primary" 
     onclick="if(!confirm('Are you sure you want to deliver order?')){return false;}" > Delivered 
  </button>
  <button type="submit" input 
     style="background-color: #0000FF; color: white" 
     name="submitAccept" id="submitAccept" class="btn btn-primary" 
     onclick="if(!confirm('Are you sure you want to accept order?')){return false;}"  <?php if($_POST['order_status']="Accepted"): ?>.disabled <?php endif ?>> Accept 
  </button>
  <button type="button" style="background-color: #FFFF00;color: black" 
     class="btn btn-success" data-toggle="modal" 
     data-target="#myDropdown"> Send 
  </button> 
  <button type="submit" input 
     style="background-color: #f44336; color: white" 
     name="submitCancel" class="btn btn-danger" 
     onclick="if(!confirm('Are you sure you want to cancel order?')){return false;}">Cancel
  </button> 
</div>

1 个答案:

答案 0 :(得分:0)

你正在使用ajax吗?如果是,您需要向我们展示您的ajax代码。

即$ .post(网址,数据)&lt; - 有点像这样。我担心问题出在你的ajax代码上。确保您已在数据中包含ORDER_ID。

这里有更多例子:

var order_id = $('input[name="ORDER_ID"]').val();
var data: {ORDER_ID: order_id};
var url = 'http://your-URL';
$.post(url, data).done(function(result){console.log(result))})