0

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. 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.

This is where the onChange event is, triggering the AJAX functions:

<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:

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:

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

<?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. The selClub function gives me a 500 internal server error on the 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.

jChris
  • 37
  • 7
  • Please, do not mix presentation logic (```echo```) with logic. Also escape your variables, your code is prone to [SQL injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). In order to check your error on agent, just point your browser to ```http://whatever/you/have/getagent.php```. – mTorres Oct 16 '15 at 15:26
  • I used error reporting for php and this is the error I get, Notice: Undefined variable: mysqli in /var/www/html/registro4h/login/clubes/club4h/getnombre.php on line 10 Fatal error: Call to a member function real_escape_string() on a non-object in /var/www/html/registro4h/login/clubes/club4h/getnombre.php on line 10 I was escaping the variable but i deleted that code for the sake to find out if the error would go away, it still keeps showing the real_escape_string error even though i deleted that. – jChris Oct 16 '15 at 15:40

1 Answers1

0

A 500 error is a server error, so that means the problem will be in PHP and I'm seeing alot of issues there. 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.

Good luck

MichaelClark
  • 1,192
  • 8
  • 7
  • I got one to work (function selClub with getnombre.php) but even replicating it to the other function and php it still doesn't give me the query values. Sometimes the function that works gets duplicated into both fields. I still need help understanding why. When I've done each one with different variables because it's a different query. – jChris Oct 23 '15 at 19:43