[英]AJAX - Issue calling two functions inside one onchange event
我试图通过 AJAX 从数据库中检索两个东西,一个图像和一个产品价格,一个函数在一个onchange
事件中包含两个函数。
经过多次尝试和错误,我终于停止在控制台中接收错误,但现在即使我没有收到错误,我通过 AJAX 需要的两件事也没有出现。
我的想法用完了,没有收到任何错误消息让我无所适从。
<head>
的 AJAX 函数:
<script type="text/javascript">
function seleccionProd(str) {
function precioProd(str) {
if (str=="") {
document.getElementById("precio").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("precio").innerHTML = this.responseText;
document.getElementById("precioOculto").value = this.responseText;
}
};
xmlhttp.open("GET", "precio.php?q="+str, true);
xmlhttp.send();
}
function imagenProd(str) {
if (str=="") {
document.getElementById("imagen").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("imagen").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "seleccion_prod_compra_imagen.php?q="+str, true);
xmlhttp.send();
}
}
</script>
<select>
元素:
<select class="custom-select my-1 mr-sm-2" id="producto" name="producto" onchange="seleccionProd(this.value);" required>
在 AJAX 函数中调用的两个 PHP 文件:
珍贵的.php
<?php
require $_SERVER['DOCUMENT_ROOT'].'/php/db_key.php';
$con = mysqli_connect($servername, $username, $password, $dbname);
$q = $_GET["q"];
$sql = "SELECT precio FROM productos WHERE titulo = '".$q."'";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
echo $row['precio'];
}
?>
seleccion_prod_compra_imagen.php.php
<?php
require $_SERVER['DOCUMENT_ROOT'].'/php/db_key.php';
$con = mysqli_connect($servername, $username, $password, $dbname);
$q = $_GET["q"];
$sql = "SELECT imagen FROM productos WHERE titulo = '".$q."'";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
echo '<img src="/img/productos/'.$row['imagen'].'" class="card-img-top fluid">';
}
?>
与往常一样,我们非常感谢您的帮助。
您定义了precioProd
和imagenProd
但从不调用它们。
function seleccionProd(str) {
precioProd(str);
imagenProd(str);
function precioProd(str) {
if (str=="") {
document.getElementById("precio").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("precio").innerHTML = this.responseText;
document.getElementById("precioOculto").value = this.responseText;
}
};
xmlhttp.open("GET", "precio.php?q="+str, true);
xmlhttp.send();
}
function imagenProd(str) {
if (str=="") {
document.getElementById("imagen").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("imagen").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "seleccion_prod_compra_imagen.php?q="+str, true);
xmlhttp.send();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.