error when passing php variable with ajax

5

My problem is as follows I have a BOOSTRAP menu where the content is extracted from a database mysql , being that when selecting an option I load the content of a page in a field div of central id. When I make the selection I am already in session and I have some variables extracted from the BD that I send them by the method GET and the variable arrives perfectly, the problem is presented in the selection of new variables that are in the menu because do not reach the div central. To explain me better, I place parts of script in code AJAX :

$('#div-btn5').click(function(){
    $.ajax({
       type: "GET",
       url: "clases.php?ci=<?php echo  $assoc_prosesar['ci'];  ?>&idTema=<?php echo $modulo['idTema']; ?>",                  
       success: function(a) {
          $('#central').html(a);
       }
    });
});

Menu code is as follows:

<div class="collapse navbar-collapse" id="myNavbar">
    <ul class="nav navbar-nav navbar-right">
        <li><a href="#"  class="dropdown-toggle" data-toggle="dropdown">Ver Clases<span class="caret"></span></a>
            <ul class="dropdown-menu">
               <li  class="dropdown-submenu">
                  <?php
                     $busqUsuario="SELECT * FROM $tabla4 WHERE idalumno='$ci'"; //matricula
                     $resultUsuario = mysqli_query($db, $busqUsuario) or die(mysql_error());
                  ?>      
                  <?php 
                    if ($cursoUsuario=mysqli_fetch_array($resultUsuario)){ 

                       do { 
                           $idCurso=$cursoUsuario['idCurso'];
                           $busqCurso="SELECT * FROM $tabla3 WHERE id='$idCurso'"; //curso
                           $resultCurso = mysqli_query($db, $busqCurso) or die(mysql_error());
                           $curso=mysqli_fetch_array($resultCurso);
                 ?>
                  <a href="#" class="test" ><?php echo $curso['curso']; ?><span class="caret"></span></a>
                  <ul class="dropdown-menu">
                     <?php
                        $busqModulo="SELECT * FROM $tabla5 WHERE curso='$idCurso'"; //modulo
                        $resultModulo = mysqli_query($db, $busqModulo) or die(mysql_error());

                        if ($modulo=mysqli_fetch_array($resultModulo)){ 
                           do {
                               $idTemaSession=$modulo['idTema'];
                               if($modulo['cargado']=='SI'){
                               $busqCurso="SELECT * FROM $tabla11 WHERE id='$idCurso'"; //curso
                               $resultCurso = mysqli_query($db, $busqCurso) or die(mysql_error());
                               $curso=mysqli_fetch_array($resultCurso);
            ?>
              <li><a  href="#" id="div-btn5" ><?php  $e= addslashes($modulo['tema']); $codificadoe = utf8_encode($e); echo $codificadoe;?></a></li>
            <?php }
        } while ($modulo=mysqli_fetch_array($resultModulo)); 
    }
?>      
          </ul>
           <?php
             } while ($cursoUsuario=mysqli_fetch_array($resultUsuario)); 
              }
           ?>
            </li>
         </ul>
     </li>

In a publication of a problem on this page I saw an example where they modified href="#" by href="?ci=<?php echo $assoc_prosesar['ci']; ?>&idTema=<?php echo $modulo['idTema']; ?>" but it did not work for me. I hope you can help me and in advance I thank you for the time devoted to this problem.

    
asked by Martín José Cartaya 17.09.2018 в 22:52
source

1 answer

0

To pass variables through ajax you must use the value data . In my case I used the POST method

$('#div-btn5').click(function(){
    var ci_v=<?php echo  $assoc_prosesar['ci'];  ?>;
    var id_tema_v=<?php echo $modulo['idTema']; ?>;
    $.ajax({
       type: "POST",
       url: "clases.php",
       data: {ci:ci_v,id_tema_v:id_tema},                  
       success: function(a) {
          $('#central').html(a);
       }
    });
});

And in clases.php process the information

if($_SERVER['REQUEST_METHOD']=="POST"){
    $ci=$_POST['ci'];
    $id_tema=$_POST['id_tema'];
}

Regarding the href:

It worked for me, putting everything in an echo. Example:

$ci=$assoc_prosesar['ci'];
$id_tema=$modulo['idTema'];
$e= addslashes($modulo['tema']);
$codificadoe = utf8_encode($e);
echo('<a  href="clases.php?ci='.$ci.'&id_tema='.$id_tema.'" id="div-btn5" >'.$codificadoe.'</a>');
    
answered by 25.11.2018 в 06:23