简体   繁体   English

查询无法通过JavaScript工作,并出现500个内部服务器错误

[英]Query not working through JavaScript with 500 internal server error

I'm new to AJAX and I'm trying to create two dropdownlists of options from a database depending on what the user selects in two previous dropdowns. 我是AJAX的新手,我试图根据用户在前两个下拉列表中选择的内容从数据库中创建两个选项下拉列表。 I've checked every way shape and form I've looked up but one dropdownlist doesn't return any values and the other says internal server error 500. 我检查了所查找的各种形状,但一个下拉列表未返回任何值,另一个显示内部服务器错误500。

This is where the onChange event is, triggering the AJAX functions: 这是onChange事件所在的位置,触发AJAX函数:

<select required="true" id="oficinaLoc" name="oficinaLoc" onchange="getAgent(this.value); selClub(this.value)">
    <option value="">Seleccione Unidad</option>
    <option value="680 - Centro de Tecnología de Información">680 - Centro de Tecnología de Información</option>
    <option value="681 - Educación Agrícola">681 - Educación Agr&iacute;cola</option> 
    <option value="682 - Planificación y Evaluación">682 - Planificaci&oacute;n y Evaluaci&oacute;n</option>
    <option value="683 - Medios Educativos e Información">683 - Medios Educativos e Informaci&oacute;n</option>
    <option value="684 - Ciencias de la Familia y el Consumidor">684 - Ciencias de la Familia y el Consumidor</option>

    etc...

These are my AJAX functions: 这些是我的AJAX函数:

function getAgent(str) {
    if (str == "") {
        document.getElementById("dispAgente").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 (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("dispAgente").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getagent.php?q="+str,true);
        xmlhttp.send();
    }
}

function selClub(unidad) {
    if (unidad == "") {
        document.getElementById("dispNombre").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 (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("dispNombre").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getnombre.php?j="+unidad,true);
        xmlhttp.send();
    }
}

And these are the PHP pages it calls to through the XMLHttpRequest respectively: 这些是它分别通过XMLHttpRequest调用的PHP页面:

getagent.php getagent.php

<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

$q = ($_GET['q']);

$con = mysqli_connect('intrasise.uprm.edu','jchristian','registro4h','4h');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"4h");
$sql="SELECT nombre FROM personal4h WHERE unidadProg LIKE '%".$q."%'";
$result = mysqli_query($con,$sql);

echo '<select name="agenteExt"';
while($row = mysqli_fetch_array($result)) {
    echo "<option value = " . $row['nombre'] . ">" . $row['nombre'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>

getnombre.php getnombre.php

<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

$j = ($_GET['j']);

$con = mysqli_connect('intrasise.uprm.edu','jchristian','registro4h','4h');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"4h");
$sql="SELECT nombreClub FROM club4h WHERE oficinaLoc LIKE '%".$j."%'";
$result = mysqli_query($con,$sql);

echo '<select name="nombreClub"';
while($row = mysqli_fetch_array($result)) {
    echo "<option value = " . $row['nombreClub'] . ">" . $row['nombreClub'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>

The getAgent function doesn't return any options in the dropdownlist even though it creates the empty select. getAgent函数创建了空选择,它也不会在下拉列表中返回任何选项。 The selClub function gives me a 500 internal server error on the xmlhttp.open("GET","getagent.php?q="+str,true); selClub函数在xmlhttp.open("GET","getagent.php?q="+str,true);上给我500个内部服务器错误xmlhttp.open("GET","getagent.php?q="+str,true); line. 线。 I really don't know what else to do, I've followed every online article I've found about this to the dot. 我真的不知道该怎么办,我将发现的与此有关的每篇在线文章都放在后面。

A 500 error is a server error, so that means the problem will be in PHP and I'm seeing alot of issues there. 500错误是服务器错误,因此这意味着问题将在PHP中出现,而我看到了很多问题。 I'm not sure that this is a complete list,...you'll need to debug it yourself, but here's a couple that aren't helping. 我不确定这是否是完整列表,...您需要自己对其进行调试,但这对夫妇没有帮助。

$j = ($_GET['j']);
$j = $_GET['j'];
//dump the ()

echo '<select name="nombreClub"';
echo '<select name="nombreClub">';
//closing > is missing

echo "<option value = " . $row['nombre'] . ">" . $row['nombre'] . "</option>";
echo "<option value=\"".$row['nombre']."\">".$row['nombre']."</option>";
//quotes around the value are missing

If you view the errors thrown by the PHP files, then you'll be on your way. 如果您查看PHP文件引发的错误,那么您会发现自己的问题。

Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM