Error in ajax . Javascript

Asked

Viewed 42 times

1

Hello, The following code will present a table with values.I am recovering the cell value of the table that the user clicks through this function $(this).text() in Java. I have to send this information by this function,by an ajax,to a page called (Tela_Escolha_Perguntas_correcao.php) which 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 the table construction 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 the $_GET['nomeGrupo'] and did not succeed.

 <head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
   <script type="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 from the table cell to this second screen, and the tmb user is directed to this screen and can see the submitted content.

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
 <body>
  <div class="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>

1 answer

2


According to what you intend "Second Screen (Tela_choose_questions_fix.php) The first code sends the text from the table cell to this second screen, and the tmb user is directed to this screen can see the content sent.", no need to use ajax or libraries, just pass 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 an array, turn the contents of this array into a string, separating the elements by some character that will never be used in the content of the items. Developers usually use comma in order to separate the elements. I prefer the pipe character "|", due to the fact that this is not in the rules of the Portuguese language 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 function php implode

  $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 are applications without loading a new page.

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

<script type="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> 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.