使用2个查询进行Ajax PHP更新

时间:2017-09-12 10:01:32

标签: php jquery mysql ajax

我试图更新保留表列 生病从表价格中获取价值乘以邮政数量,但我的问题是我无法更新 继承我的代码

表单php

  <form method="post">

              <div class="col-md-12">
                <label>ID</label>
                <input type="text" class="form-control" id="id" name="id">
              </div>

              <div class="col-md-12" >
                <label>Charge</label>
                <select id="name" name="name" class="form-control">
                  <?php
                  while ($reserve=mysqli_fetch_array($charge)) { ?>
                    <option value=" <?php echo $reserve['name']?>">
                      <?php echo $reserve['name']; ?>
                    </option><?php } ?>
                </select>
              </div>

              <div class="col-md-12">
                <br>
                <label>Quantity</label>
                <input type="number" class="form-control" id="quantity" name="quantity">
              </div>

              <div class="col-md-12">
                <br>
                <button class="Huge ui teal button" id="charge" name="charge">Add Charge</button>
              </div>

            </form>

为ajax

  $(document).ready(function(e){
  $('#charge').click(function(){
  var id = $('#id').val();
  var name = $('#name').val();
  var quantity = $('#quantity').val();

         $.ajax({
           type   : 'POST',
           data   :{
              id:id,
              name:name,
              quantity:quantity,
                  },
            url     :"charge.php",
            success : function(result){
              if(result)
              {
                 $('#error').html("<span class='text-success' >Success Man</span>");
             }else{
               $('#error').html("<span class='text-danger'>Check mo information Man</span>");
           }
          }
         })
    });
  });

和charge.php

<?php

$connect = mysqli_connect("localhost", "root", "", "tobedetermined");

   $id = $_POST['id'];
   $name = $_POST['name'];
   $quantity = $_POST['quantity'];

   $sql = "SELECT price FROM charge where name ='$name'";
   $result = $connect->query($sql);
   $additional = $result * $quantity;

 mysqli_query($connect,"update reserve set additional= '$additional' where id = $id");


 mysqli_close($connect);


?>

请不要介意我刚刚使用的Select标签,以列出所有列名称frm费用表

1 个答案:

答案 0 :(得分:0)

如果您想使用Ajax,则无法使用表单。

只需删除表单标记

即可
<div class="col-md-12">
                <label>ID</label>
                <input type="text" class="form-control" id="id" name="id">
              </div>

              <div class="col-md-12" >
                <label>Charge</label>
                <select id="name" name="name" class="form-control">
                  <?php
                  while ($reserve=mysqli_fetch_array($charge)) { ?>
                    <option value=" <?php echo $reserve['name']?>">
                      <?php echo $reserve['name']; ?>
                    </option><?php } ?>
                </select>
              </div>

              <div class="col-md-12">
                <br>
                <label>Quantity</label>
                <input type="number" class="form-control" id="quantity" name="quantity">
              </div>

              <div class="col-md-12">
                <br>
                <button class="Huge ui teal button" id="charge" name="charge">Add Charge</button>
              </div>