gpt4 book ai didi

php - 使用 2 个查询的 Ajax PHP 更新

转载 作者:行者123 更新时间:2023-11-29 18:26:12 25 4
gpt4 key购买 nike

我正在尝试额外更新保留表列我将在哪里获得表价格乘以帖子数量的值,但我的问题是我无法更新它这是我的代码

对于 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 标签,我只是用它来放置费用表中列名的所有值

最佳答案

如果你想使用Ajax,你就不能使用form。

只需删除form标签

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

关于php - 使用 2 个查询的 Ajax PHP 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46173484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com