How to pass a variable with ajax to another php file

0

Good I have an inconvenience with my code, I want to send you a data through ajax, that allows me to make a query according to what this variable brings, the problem is that you do not want to recognize me when making the $ _POST variable, in it I store the code of a Faculty and I want that according to the selected faculty I load the data, if someone can do me the favor of helping, it would be very helpful, thanks

Here is the code of my select

<div class="form-group">
                            <label for="selectFacultad" class="col-sm-1 control-label">Facultad:</label>
                                    <div class="col-sm-4"><select name="codigoFacultad" id="selectFacultad" class="form-control"  >
                                        <option>Seleccione</option>
                                    </select></div>
                             <button class="btn btn-primary fa fa-search" id="buscar" onclick="Cargar()"></button>
                        </div>

Now follow the escript where I have the function load ()

<script type="text/javascript">

    function Cargar(){
         var codigoFacultad = $('#selectFacultad').val();
        alert(codigoFacultad);
        $.ajax({
            type: "POST",
            dataType: "html",
            url: "llenarPrograma.php",
            data: codigoFacultad,
            success: function(data){
                 $('#contenido').load('llenarPrograma.php');
            }
        });

        }
</script>

Next the other file that should receive the variable

<?php
include_once("conexion.php");

ob_start();
 mysqli_set_charset($conexion,"utf8");

$cod_facultad = $_POST['codigoFacultad']; //Esta es la variable que no me quiere recibir me sale indice indefinido

$consulta = mysqli_query($conexion, "SELECT id_programa,nombre_programa,duracion_semestral,horario,metodologia FROM programa WHERE id_facultad= '".$cod_facultad."'");

if (mysqli_num_rows($consulta) > 0)
{
   echo "<table class='table table-bordered nowrap'> 
            <thead> 
                <tr>
                    <th>Codigo</th>
                    <th>Nombre</th> 
                    <th>Duración semestral</th>
                    <th>Horario</th> 
                    <th>Metodologia</th>
                </tr> 
            </thead> "; 
    while($row = mysqli_fetch_array($consulta, MYSQL_ASSOC)) { 

                echo "<tr>";
                echo "<td>".$row['id_programa']."</td>";
                echo "<td>".$row['nombre_programa']."</td>";
                echo "<td>".$row['duracion_semestral']."</td>";
                echo "<td>".$row['horario']."</td>"; 
                echo "<td>".$row['metodologia']."</td>"; 
                echo "</tr>"; 


} 
  "</table>";  
} else { 
echo " <p>Aún no hay registros en la base de datos</p>"; 
}
?>
    
asked by Carlos Alberto Guerrero Navarr 07.11.2018 в 05:09
source

4 answers

1

Solved Here I leave the solution in case you come to present this same difficulty, believe me I suffered a lot until I have to analyze it in detail and use different ways to reach your solution.

Here's the select code

<div class="form-group" >
                            <label for="selectFacultad" class="col-sm-1 control-label">Facultad:</label>
                                    <div class="col-sm-4"><select name="selectFacultad" id="selectFacultad" class="form-control"  >
                                        <option value="0">Seleccione</option>
                                    </select></div>
                             <button class="btn btn-primary fa fa-search" type="submit" id="buscar" onclick="Cargar();"></button>
                        </div>

Both the name of the select and the id must be called equal only to avoid generating confusion, likewise the script goes like this:

<script>
        function Cargar(){
            var url="llenarPrograma.php";
            var selectFacultad= document.getElementById("selectFacultad").value;

            $.ajax({
                type: "POST",
                url:url,
                data:{selectFacultad:selectFacultad},
                success: function(datos){
                    $('#contenido').html(datos);
                }
            });
        }
     </script>

And finally the file completaPrograma.php

<?php
 include_once('conexion.php');

$codigo=$_POST["selectFacultad"];

$resultado=$conexion->query("SELECT id_programa,nombre_programa,duracion_semestral,horario,metodologia FROM programa WHERE id_facultad='$codigo'");

$total=$resultado->num_rows;

if($total>0){
$tabla="<table class='table table-bordered table-success nowrap'>";
$tabla.="<tr><td>Codigo</td><td>Nombre</td><td>Duración</td><td>Horario</td><td>Metodologia</td></tr>";
while($row=$resultado->fetch_assoc()){
    $tabla.="<tr>" ;
    $tabla.="<td>".$row['id_programa']."</td>";
    $tabla.="<td>".$row['nombre_programa']."</td>";
    $tabla.="<td>".$row['duracion_semestral']."</td>";
    $tabla.="<td>".$row['horario']."</td>";
    $tabla.="<td>".$row['metodologia']."</td>";
    $tabla.="</tr>" ;
}
    $tabla.="</table>" ;
    echo $tabla;
}
else{
     echo "<p>Aún no hay registros en la base de datos</p>";
}
?>

I hope and serve you all, thanks to the people who responded to me, it was very helpful

    
answered by 08.11.2018 в 03:14
0

When you perform the ajax in the "data" only the value of the variable codigoFacultad is sent, a key " codigoFacultad " is not sent that in php you try to access $_POST['codigoFacultad']

try placing the following in the data: {"codigoFacultad" :codigoFacultad }

    
answered by 07.11.2018 в 05:20
0

I hope this code will help you:

to your variable codeFaculty modify it by the following:

var codigoFacultad = { 
   "codigoFacultad":$('#selectFacultad').val();
};

you will be sending the name that identifies the variable (which the POST recognizes) and the value of that variable.

    
answered by 07.11.2018 в 05:26
0

You have to declare where you are going to send the post

<script type="text/javascript">

    function Cargar(){
         var codigoFacultad = $('#selectFacultad').val();
        alert(codigoFacultad);

        /*Le indicas el nombre del del post*/
        var form_data = new FormData();
        form_data.append('codigoFacultad', codigoFacultad);

        $.ajax({
            type: "POST",
            dataType: "html",
            url: "llenarPrograma.php",
            /*Envias los datos*/
            data: form_data,
            success: function(data){
                 $('#contenido').load('llenarPrograma.php');
            }
        });

        }
</script>

In php you receive it like this.

$cod_facultad = $_POST['codigoFacultad'];

And in your HTML code you lack the value in the select option, they should be like this.

<select name="codigoFacultad" id="selectFacultad" class="form-control"  >
   <option value="0">Seleccione</option>
   <option value="1">Facultad 1</option>
   <option value="2">Facultad 2</option>
</select>

I recommend that your name and id be the same, since it can lead to confusion. And also your SQL statement is very susceptible to sql injection I recommend that you review how to go through parameters or PDO.

    
answered by 07.11.2018 в 17:18