gpt4 book ai didi

javascript - 如何从数据库中获取值并在单击提交按钮之前显示计算值

转载 作者:太空宇宙 更新时间:2023-11-04 06:18:35 24 4
gpt4 key购买 nike

分支(数据库)源目的地距离类加罗尔金奈 500

    <?php
$db=mysqli_connect("localhost","root","","courier");
if (isset($_POST['weight'])) {
$sour=$_POST['source'];
$dest=$_POST['dest'];
$weight=$_POST['weight'];
$sql="SELECT distance FROM branch WHERE source='$sour' AND destination='$dest'";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result);
$amt=$row['distance']*$weight*10;
echo $amt;
}
?>
 <!DOCTYPE html>
<html lang="en">
<head>
<title>BOOKING</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"

href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<style>
<div class="form-group">
<label for="source">SOURCE</label>
<select class="form-control" name="source" id="src">
<option>SELECT</option>
<option>BANGALORE</option>
<option>CHENNAI</option>
<option>BOMBAY</option>
<option>KOLKATA</option>
<option>HYDERABAD</option>
<option>THIRUVANANTHAPURAM</option>
</select>
</div>
<div class="form-group">
<label for="dest">DESTINATION</label>
<select class="form-control" name="dest" id="dst">
<option>SELECT</option>
<option>BANGALORE</option>
<option>CHENNAI</option>
<option>BOMBAY</option>
<option>KOLKATA</option>
<option>HYDERABAD</option>
<option>THIRUVANANTHAPURAM</option>
</select>
</div>
<div class="form-group">
<label for="weight">WEIGHT(in kgs):</label>
<input type="textarea" class="form-control" id="weight " placeholder="Enter Weight" name="weight">
</div>
<div class="form-group">
<label for="stadd">AMOUNT:</label>
<input type="text" class="form-control" name="amt" id="amt">
</div>
<button type="submit" class="btn btn-success" >Submit</button>
</form>
  <script type="text/javascript">
$(document).on('change','#dst',function(){
var source=$('#src').val();
var destination=$('#dst').val();
var weight=$('#weight').val();
$.ajax({
method:"post",
url:"amount.php",
data:{source:'source',dest:'destination',weight:weight}

})
.done(function(data){
$("#amt").val(data);
});

});

</script>

我需要从分支说距离中检索值,然后计算等于重量*距离*10 的数量我试过这样做,但它没有显示金额字段中的值我已经尝试为此使用 ajax、php 和 jquery,但我是新手,所以我无法弄清楚代码有什么问题

最佳答案

Onchange destination 你可以发送ajax请求来计算值。

$(document).on('change', '.destination', function(){

$.get('/url', function(res)
{
console.log(res);
});
});

在资源中你可以获得值(value)。

关于javascript - 如何从数据库中获取值并在单击提交按钮之前显示计算值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55720004/

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