如何在查询中使用文本框的值?

时间:2017-09-16 18:45:51

标签: javascript php jquery

如何在查询中正确使用文本框(ORDER_ID)的值?现在发生的事情是,它需要先在ordermodal.php中单击一个按钮以获取它的值,它没有;当我单击View Order时得到该值。输出应该是这样的,当我单击View Order时它已经获得了它的值。我希望你能解决我的问题,我坚持这一点。谢谢!

这是我的order.php代码。这是我点击查看订单的地方。

<tr> 
 <td><?= $order['order_id'] ?></td> 
 <td><?= $order['user_id'] ?></td> 
 <td><?= $date ?></td>
 <td><?= $time ?></td>
 <td><?= $order['order_deliveryCharge'] ?></td>
 <td><?= $order['order_totalAmount'] ?></td>
 <td><?= $order['address'] ?></td>
 <td><?= $order['coordinates'] ?></td>
 <td><?= $order['driver_number'] ?></td>
 <td><?= $order['order_status'] ?></td>
 <td><button type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal" onclick="viewOrder( '<?= $order['order_id'] ?>', '<?= $order['order_id'] ?>', '<?= $order['user_id'] ?>', '<?= $date ?>', '<?= $time ?>', '<?= $order['order_deliveryCharge'] ?>', '<?= $order['order_totalAmount'] ?>', '<?= $order['address'] ?>', '<?= $order['coordinates'] ?>', '<?= $order['driver_number'] ?>', '<?= $order['order_status'] ?>')"> View Order </button>
 </td> 
 </tr>
 <?php endwhile; ?>
 <?php endif; ?> 
 </tbody> 
 </table>
 </div> 
 <div class="panel-footer"> 
 </div>
 </div>
 </div>
 <script>
 function viewOrder(order_id, order_id, user_id, order_date, order_time, order_deliveryCharge, order_totalAmount, address, coordinates, driver_number, order_status) { 
 document.getElementById("titleModal").innerHTML = "Order Information";
 document.getElementsByName("ORDER_ID")[0].setAttribute("value", order_id); 
 document.getElementsByName("ORDER_ID_MODAL_2")[0].setAttribute("value", order_id);
 document.getElementsByName("user_id")[0].setAttribute("value", user_id);
 document.getElementsByName("order_date")[0].setAttribute("value", order_date); 
 document.getElementsByName("order_time")[0].setAttribute("value", order_time); 
 document.getElementsByName("order_deliveryCharge")[0].setAttribute("value", order_deliveryCharge); 
 document.getElementsByName("order_totalAmount")[0].setAttribute("value", order_totalAmount); 
 document.getElementsByName("address")[0].setAttribute("value", address);
 document.getElementsByName("coordinates")[0].setAttribute("value", coordinates); 
 document.getElementsByName("drivers_number")[0].setAttribute("value", driver_number); 
 document.getElementsByName("order_status")[0].setAttribute("value", order_status);  
 document.getElementsByName("viewOrder")[0].setAttribute("name", "viewOrder");


 }
 </script>

这是我的ordermodal.php代码。

<?php 

include_once 'ordermodal2.php';

/** *ordermodal.php **/

 $id = "";
 $order_date = "";
 $order_time = "";
 $order_id = "";
 $order_deliverCharge = "";
 $order_status = "";
 $order_totalAmount= ""; 
 $coordinates = "";
 $driver_number = "";
 $address = "";


 $food_name="";
 $special_request="";
 $quantity="";
 $amount="";
 $orders="";

         ?>

         <!-- MODALS --> <!-- DETAILS --> 
         <div id="myModal" class="modal fade" role="dialog" style="z-index: 1400;">
         <div class="modal-dialog modal-lg">
         <div class="modal-content">
        <form action="" method="post" class="form-horizontal">
         <div class="modal-header">
         <button type="button" class="close" data-dismiss="modal"><center>&times;</button>
     <h4 class="modal-title" id="titleModal">Order Information</h4>
     </div>
         <div class="modal-body"> 

  <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>

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

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

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

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

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

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

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

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

 <div class="form-group">
 <label for="order_status" class="col-sm-2 control-label">Order Status</label> 
 <div class="col-lg-3">
 <input type="text" input style="width:500px" class="form-control" name="order_status" id="order_status" 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:#00000;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;}" > Accept </button>
 <button type="button" style="background-color:#FFFF00;color:black" class="btn btn-success" data-toggle="modal" data-target="#myDropdown" onclick="send('<?= $_POST['order_id'] ?>')"> 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> 
  <?php
if(isset($_POST['submitDelivered'])){  
  $order_id = trim(addslashes($_POST['ORDER_ID']));
  $query = "UPDATE order_tbl SET `order_status`='Delivered' WHERE `order_id` = $order_id";
  if (mysqli_query(connection2(), $query)) { 
         mysqli_query(connection2(), "COMMIT");
         $_SESSION['message'] = "Order Delivered"; } 
         else { 
         $_SESSION['message'] = mysqli_error(connection2());
         mysqli_query(connection2(), "ROLLBACK");
         }
  }

  if(isset($_POST['submitAccept'])){  
  $order_id = trim(addslashes($_POST['ORDER_ID']));
  $query = "UPDATE order_tbl SET `order_status`='Accepted' WHERE `order_id` = $order_id";
  if (mysqli_query(connection2(), $query)) { 
         mysqli_query(connection2(), "COMMIT");
         $_SESSION['message'] = "Order Accepted"; } 
         else { 
         $_SESSION['message'] = mysqli_error(connection2());
         mysqli_query(connection2(), "ROLLBACK");
         }
  }


  if(isset($_POST['submitCancel'])){  
  $order_id = trim(addslashes($_POST['ORDER_ID']));
  $query = "UPDATE order_tbl SET `order_status`='Cancelled' WHERE `order_id` = $order_id";
  if (mysqli_query(connection2(), $query)) { 
         mysqli_query(connection2(), "COMMIT");
         $_SESSION['message'] = "Order Cancelled"; } 
         else { 
         $_SESSION['message'] = mysqli_error(connection2());
         mysqli_query(connection2(), "ROLLBACK");
         }
  }

  ?>

 </div>
 </form>
 </div>
 </div>
 </div>
<script>
function send(order_id) { 
 document.getElementById("titleModal2").innerHTML = "Choose Driver";
 document.getElementsByName("ORDER_ID_MODAL_2")[0].setAttribute("value", order_id);
 document.getElementsByName("send")[0].setAttribute("name", "send");
 }
</script>

1 个答案:

答案 0 :(得分:0)

如果你使用jquery会很好。

//Change your button 'View Order' with this
<button type="button" class="btn btn-success"  onclick="viewOrder( '<?= $order['order_id'] ?>', '<?= $order['order_id'] ?>', '<?= $order['user_id'] ?>', '<?= $date ?>', '<?= $time ?>', '<?= $order['order_deliveryCharge'] ?>', '<?= $order['order_totalAmount'] ?>', '<?= $order['address'] ?>', '<?= $order['coordinates'] ?>', '<?= $order['driver_number'] ?>', '<?= $order['order_status'] ?>')"> View Order </button>

//place this script below this function

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

function viewOrder(order_id, order_id, user_id, order_date, order_time, order_deliveryCharge, order_totalAmount, address, coordinates, driver_number, order_status) { 
$('#titleModal').text("Order Information")
$("#ORDER_ID").val(order_id); 
$("#ORDER_ID_MODAL_2").val(order_id);
$("#user_id").val(user_id);
$("#order_date").val(order_date); 
$("#order_time").val(order_time); 
$("#order_deliveryCharge").val(order_deliveryCharge); 
$("#order_totalAmount").val(order_totalAmount); 
$("#address").val(address);
$("#coordinates").val(coordinates); 
$("#drivers_number").val(driver_number); 
$("#order_status").val(order_status);  
$("#viewOrder").attr("name", "viewOrder");
$('#myModal').modal('show');
}