Javascript ajax上的未识别索引提交时获取php文件
我使用AJax调用php文件并获取值,但是,在提交时,一切正常,但从被调用的php文件返回未识别的变量 这是我的剧本Javascript ajax上的未识别索引提交时获取php文件,javascript,php,ajax,Javascript,Php,Ajax,我使用AJax调用php文件并获取值,但是,在提交时,一切正常,但从被调用的php文件返回未识别的变量 这是我的剧本 <script> function showPrice(str) { if (str == "") { document.getElementById("txtHint").innerHTML = ""; return; } else { if (window.XMLHttpRequest) {
<script>
function showPrice(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET",'getcurrentpumpprice.php?q='+str,true);
xmlhttp.send();
}
var pumpprice1 = document.getElementById('pumpprice').value;
var amount1 = document.getElementById('amount').value;
document.getElementById('litre').value = (amount1) / (pumpprice1);
}
</script>
函数showPrice(str){
如果(str==“”){
document.getElementById(“txtHint”).innerHTML=“”;
返回;
}否则{
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
document.getElementById(“txtHint”).innerHTML=this.responseText;
}
};
open(“GET”,getcurrentpumpprice.php?q='+str,true);
xmlhttp.send();
}
var pumpprice1=document.getElementById('pumpprice').value;
var amount1=document.getElementById(“金额”).value;
document.getElementById('L')。值=(数量1)/(泵许可证1);
}
这是我的php代码
$q = isset($_GET['q']);
$outletid = $session->userinfo['retailoutlet'];
if($q == 'PMS'){
$query = "SELECT pms_price FROM ".TBL_RETAIL_OUTLETS." WHERE outlet_id= '$outletid ' ";
$result = $database->query($query);
$row = $result->fetch_assoc();
$pump_price = $row['pms_price'];
}
echo '<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Current Pump Price <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="pumpprice" name="pumpprice" class="form-control col-md-7 col-xs-12" required="required" type="text" value="'.$pump_price.'" readonly="readonly">
</div>
</div>';
$q=isset($\u GET['q']);
$outletid=$session->userinfo['retailoutlet'];
如果($q=='PMS'){
$query=“从“.TBL\u零售店”中选择pms\u价格。其中店号=“$outletid”;
$result=$database->query($query);
$row=$result->fetch_assoc();
$pump_price=$row['pms_price'];
}
回声'
现行泵价*
';
但是,我使用AJax调用php文件并在
提交,一切正常,但返回未知变量
从被调用的php文件
原因
在您的情况下,变量$q
永远不会是'PMS'
,因为您得到了
$q=intval($\u GET['q'])
soif($q='PMS'){
计算结果为false,变量$pump\u price
将未定义
总是
下面的示例计算了false原因intval()
if($q='PMS'){
然后
在echo
中使用的变量($pump\u price
)将是未定义的变量
您应该在if
语句中使用echo
if($q == 'PMS'){
echo 'your html';
}
或
最好在查询前进行验证,如果使用PDO也很好
if( isset($_GET['q']) && is_int($_GET['q']) ){
$q = intval($_GET['q']);
// your remaining code
}else{
echo 'invalid input';
}
感谢你的分段,我在调试时放入了intval,我已经删除了它,即使这样。它仍然不起作用。我使用if(isset($\u GET['q']){$q=isset($\u GET['q']);使用if($pump\u price){echo///result。如果不起作用,你的编辑就没有意义,
$q=isset($\u GET['q']));
保存布尔状态OK,现在它不会给我任何错误,而是加载到被调用的php文件,并在我提交时返回空白。但是每次提交都是工作文件
if( isset($_GET['q']) && is_int($_GET['q']) ){
$q = intval($_GET['q']);
// your remaining code
}else{
echo 'invalid input';
}