Problem with .get with php using ajax

0

I have the following form on my website:

I am trying to make sure that, in the selection of subjects, the subjects are loaded according to the chosen career. I read that you can do this type of operation, without reloading the page, through AJAX.

This is the race select code:

<div class="input-field col s12">
                <form name="formulario" action="" >
                    <select id="carrera" name="carrera">
                    <option value="" disabled selected>Elige tu carrera</option>
                    <?php
                        if ($pdo){
                            $sql = "select * from carreras order by clave;";
                            $ps = $pdo->prepare($sql);
                            $ps->execute();
                            while($arr = $ps->fetch(PDO::FETCH_ASSOC)){
                                echo "<option value='".$arr['clave']."'>".$arr['nombre']."</option>";
                            }
                    } else {
                        echo "Hubo un problema con la conexión :( ";
                    }

                    ?>
                </select>
                <label>Carrera que estás cursando</label>
                </form>
            </div>

And this the code of the select of the matters:

<div class="input-field col s12">
                <select id="recargado">
                <option value="" disabled selected>Elige la materia a consultar</option>
                <?php include("materias.php");?>
                </select>
                <label>Materia que deseas consultar</label>
            </div>
            <div class="center-align">
                <a aria-label="show diagram" id="boton"class="btn-consultar waves-effect waves-light btn teal lighten-3 grey-text text-darken-4" href="#diagram">¡Consultar!</a>
            </div>

I thought I'd put the PHP code in another file and invoke it on the page when the form worked.

The point is, the JavaScript code I have for this, is as follows:

$("#carrera").change(function () {
let carrera = document.getElementById("carrera").value;
console.log(carrera);
$.get("materias.php", { idcarrera: carrera })
  .done(function (data) {
    $("#recargado").html(data);
  });
 });

I understand that this code uses jQuery. I'm just starting to handle PHP and AJAX.

And this is the PHP code I'm sending to call:

<?php
 include("/inc/config.php"); 
 $pdo = conexionPDO();

$carrera = $_GET['idcarrera'];
echo "<option>Tu carrera, amigo, es la No: $carrera </option>";

if ($pdo){

    $sql = "SELECT  materias.clave as clave, materias.nombre FROM carreras_plan
            inner join carreras_plan_materia on carreras_plan.clave = carreras_plan_materia.clave_carrera
            inner join materias on carreras_plan_materia.clave_materia = clave
            where carreras_plan_materia.clave_carrera like '$carrera';";

    $ps = $pdo->prepare($sql);
    $ps->execute();

    while($arr = $ps->fetch(PDO::FETCH_ASSOC)){
        echo "<option value''>".$arr[carrera]."</option>";
    }

} else {
    echo "Connection status-> :(";
}
?>

I have a file config.php where I have the connection to the database through PDO. On the page where I have the form, I have the include to this file and the variable $pdo . My problem is the following. As I have the PHP code, I get an error 500. However, if I delete the lines:

include("/inc/config.php"); 
$pdo = conexionPDO();

I do not get the 500 error. What it does works for me:

echo "<option>Tu carrera, amigo, es la No: $carrera </option>";

This line was set to check that my JavaScript code returned the value of the variable to PHP. And indeed, it does. If I delete the lines that I mentioned before, this echo works and updates me the idcarrera according to select the fields of the select of the races.

However, I can not get the script query executed. If I delete echo "<option>Tu carrera, amigo, es la No: $carrera </option>"; , it does not do anything else, this message appears as option of the select:

 echo "Connection status-> :(";

I do not know how I have to connect to the database so that this select is updated with the values of the query. I do not know if in this script I should also put the include and the variable $pdo or if, since they are already in index.php , you should not put them.

Thank you very much for your attention. I hope you have explained to me.

    
asked by Nohemi 08.09.2018 в 03:00
source

0 answers