Error in ajax .JavaScript

2
Hello, The following code will present a table with values. I am retrieving the table cell value that the user clicks through this $(this).text() function in javaScript. I have to send this information get through this function, for an ajax, to a page that is called ( Tela_Escolha_Perguntas_correcao.php ) that is present in the same folder as the code below. In this ( Tela_Escolha_Perguntas_correcao.php ) screen I want to appear the value sent to it. Note that in constructing the table I am creating a link to ( Tela_Escolha_Perguntas_correcao.php ) for when the user clicks on it to be directed to it.

I tried to use $_GET['nomeGrupo'] and did not succeed.

 <head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><scripttype="text/javascript">
     $(function () {
         var valor;

         var nomeGrupo;

     $('.classGrupo').click(

     function () {

      nomeGrupo=$(this).text();


      valor ={'nomeGrupo':nomeGrupo};

     $.ajax(
         {
             type: 'GET',
             url:'Tela_Escolha_Perguntas_correcao.php',
             data: valor,
             async: false         
         }
     ).done(function (retorno) {
         alert(retorno);
     });

     }
     );
     });

  </script>
 </head>
   <body>
      <header>
         <figure>
        <img  <img src="../imagens/logotipo.png">
     </figure>
     </div>
     <div id="divBusca">
        <input type="text" id="txtBusca" placeholder="Buscar..."/>
          <button id="btnBusca">Buscar</button>
     </div>
     <div id="IDsair">
        <a href="Sair_Sessao.php">SAIR</a>
     </div>
  </header>
   <div id="id_tabela">
      <table >
         <tr>
            <th>Grupos</th>
         </tr>
         <?php
            for($i=0;$i<4;$i++){

               echo " <tr>
              <td class='classGrupo'><a href='Tela_Escolha_Perguntas_correcao.php'>valor,$i</a></td>         
           </tr>";
           }
           ?>
     </table>
  </div>

Second Screen ( Tela_Escolha_Perguntas_correcao.php ) The first code sends the text of the table cell to this second screen, and the user tmb is directed to this screen and can see the contents sent.

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><body><divclass="principal">

    <header>

    <figure id="logo">
        <img src="../imagens/logotipo.png">
    </figure>

    <span><h1><?php echo'',$_GET['nomeGrupo']; ?></h1></span>

    <div id="logoUserADM">

    <figure id="logoADM">
       <img src="../imagens/administrador.png">
    </figure>

    <figure id="logoUser">
        <img src="../imagens/imagemPerfilA.png">
    </figure>

    </div>
</header>

<section>
    <label>Criar Perguntas</label>
    <label>Corrigir Perguntas</label>

    <div id="imagensBotoes">

    <a href="jk"><img src="../imagens/CostrucaoAtividade.jpg"> </a>

    <a href="jk"><img src="../imagens/Correcao.png"> </a>
  <div>
</section>
 </header>
   </div>
    
asked by anonymous 23.05.2018 / 01:57

1 answer

2
  

According to what you intend "Second Screen (Screen_Choice_Parts_Correct.php) The first code sends the table cell text to this second screen, and the user tmb is directed

You do not need to use ajax or libraries , just go via GET the parameters directly on the link to be clicked:

<?php
     for($i=0;$i<4;$i++){
        echo " <tr>
        <td class='classGrupo'><a href='Tela_Escolha_Perguntas_correcao.php?nomeGrupo=valor,".$i."'>valor,$i</a></td>        
       </tr>";
     }
?>
  

If you have to pass a vector, transform the contents of this vector into a string, separating the elements by some character that will never be used in the content of the items. Generally, developers use a comma in order to separate the elements. I prefer the pipe character "|", due to the fact that it does not appear in the Portuguese language rules for any function.

//array com a lista de produtos
$produtos = array(0 =>"relogio digital",
1 =>"mouse",
2 =>"arame para cerca",
3 =>"bateria de celular",
4 =>"doce de abobora",
5 =>"tv de plasma",
6 =>"prato de porcelana");

creating the string with the php implode function

  $string_array = implode("|", $produtos);

  echo "<tr>
        <td class='classGrupo'><a href='Tela_Escolha_Perguntas_correcao.php?nomeGrupo=".$string_array."'>enviar</a></td>        
  </tr>";
  

A good example of using AJAX is applications without loading a new page.

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><scripttype="text/javascript">
$(document).ready(function () {

    $('td').click(function(){

       var nomeGrupo=$(this).text();
       var dataString = {"nomeGrupo":nomeGrupo};

              $.ajax({
                url: 'Tela_Escolha_Perguntas_correcao.php',
                type: 'POST',
                data: dataString,
                beforeSend: function(){
                    $("#resultados").html("Carregando...");
                },
                success: function(data){
                    $("#resultados").html(data);
                },
                error: function(){
                    $("#resultados").html("Ouve um erro ao enviar sua URL");
                }
             });//ajax 

    });

}); 

</script>

</head>
<body>
   <header>
      <div>
         <figure>
           <img  <img src="../imagens/logotipo.png">
         </figure>
     </div>
     <div id="divBusca">
        <input type="text" id="txtBusca" placeholder="Buscar..."/>
          <button id="btnBusca">Buscar</button>
     </div>
     <div id="IDsair">
        <a href="Sair_Sessao.php">SAIR</a>
     </div>
  </header>
   <div id="id_tabela">
      <table >
         <tr>
            <th>Grupos</th>
         </tr>
         <?php
            for($i=0;$i<4;$i++){
               echo " <tr>
              <td class='classGrupo'>valor,$i</td>         
           </tr>";
           }
           ?>
     </table>
  </div>

  <div id="resultados"></div>

</body> 
    
23.05.2018 / 07:36