我看了很多youtube视频和文章,并给出了这段代码,但不幸的是,它甚至都没有放入脚本中,我试图在脚本中放入警报以进行检查,但没有任何显示 在同一页面中,我有三个输入文本(项目名称和“优惠PC”以及折扣后的价格) 并且我尝试让ajax处理这些输入并进行一些计算,如果用户直接将Offer pc放在第一位将计算折扣后的价格,以及如果用户直接将Offer pc放在第一位将计算折扣后的价格
我已经下载了jquery库并将其放在相同的方向上
这是manageOffer页面中div中的脚本
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var iname=document.getElementById("iname").value;
var pname=$login_session;
alert("Hello");//this for check but its not shows up at all
$(document).ready(function() {
setInterval(function(){
if(iname != "" || iname!=null){
var price;
//=load("SOPC.php",{
//name: iname,
// phname : pname
//});
$.ajax({
url: 'SOPC.php',
method: 'POST',
dataType: 'html',
data: {name: iname , phname : pname},
success: function (data) {
alert(data);
price = data.d.responseText;
},
error: function (ex) {
alert(ex.responseText);
}
});
var pc=document.getElementById("pc").value;
var ap=document.getElementById("price").value;
if(pc!= "" || pc!=null){
ap.value=((pc/100.0)*price);
}
else if(ap!= "" || ap!=null){
pc.value=(ap*price*100);
}
}
},3000);
});
</script>
和此SOPC.php
<?php
include('config.php');
if(isset($_POST['name'])){
$name = $_POST['name'];
$pname=$_POST['phname'];
$sql="SELECT ItemInBranch.Price from ItemInBranch INNER JOIN Item ON ItemInBranch.ItemNo= Item.ItemNo AND Item.ItemName='$name' INNER JOIN Branch ON ItemInBranch.branchNo= Branch.branchNo AND Branch.pharmacyID='$pname' ";
$result = mysqli_query($db,$sql);
if( $result){
$row = mysqli_fetch_assoc ($result);
echo $row['Price'];
}
}
?>
答案 0 :(得分:1)
您添加了错误的条件:
$result = mysqli_query($db,$sql);
if( !$result){ // => This should be if ($result){
$row = mysqli_fetch_assoc ($result);
echo $row['Price'];
}
答案 1 :(得分:0)
$.ajax({
url: 'SOPC.php',
method: 'POST',
data:{name: iname , phname : pname},//You missed ':' here
success: function (data) {
alert(data)//For testing
},
error: function (ex) {
alert(ex.responseText);
}
});
答案 2 :(得分:0)
您在SOPC.php中检查了错误的条件 尝试以下一个
<?php
include('config.php');
if(isset($_POST['name'])){
$name = $_POST['name'];
$pname=$_POST['phname'];
$sql="SELECT ItemInBranch.Price from ItemInBranch INNER JOIN Item ON ItemInBranch.ItemNo= Item.ItemNo AND Item.ItemName='$name' INNER JOIN Branch ON ItemInBranch.branchNo= Branch.branchNo AND Branch.pharmacyID='$pname' ";
$result = mysqli_query($db,$sql);
if($result){
$row = mysqli_fetch_assoc ($result);
echo $row['Price'];
} else {
//something here
}
}
?>
答案 3 :(得分:0)
在ajax脚本中,人参响应=> dataType:'html'
$.ajax({
url: 'SOPC.php',
method: 'POST',
dataType: 'html', //here
data:{name: iname , phname : pname},
success: function (data) {
alert(data)//For testing
},
error: function (ex) {
alert(ex.responseText);
}
});
还要更改SOPC.php中的条件
if($result){ //change here
$row = mysqli_fetch_assoc ($result);
echo $row['Price'];
} else {
}