PHP/Ajax - return Ajax does not bring when if ==

Asked

Viewed 215 times

0

I have the page that returns an Ajax request.

When executing it by passing the parameters: grupoCategoria=1&subCat=302 in the expression: if($dados_Subcategorias[codigo_subcategoria] == $_GET[subCat]) is made the option as slected.

On this page it works, but on the page that makes the request to this page it does not bring the data that enters the if only the data of else.

inserir a descrição da imagem aqui

<?php
$grupoCategoria = $_GET['grupoCategoria'];


$sql = "SELECT codigo_categoria,codigo_subcategoria,nome_subcategoria,descricao_subcategoria
FROM Subcategorias WHERE codigo_categoria=$grupoCategoria and codigo_subcategoria in(302,308,309) order by nome_subcategoria ";
$res_Subcategorias = mysql_query($sql, $con_local);
$num_Subcategorias = mysql_num_rows($res_Subcategorias);
if($num_Subcategorias>0){
    echo "<select name='Subcategorias' id='Subcategorias'  >";
        echo "<option value=''>Selecione...</option>";
        for($j=0;$j<$num_Subcategorias;$j++){
            $dados_Subcategorias = mysql_fetch_assoc($res_Subcategorias);
//          echo "<option value='$dados_Subcategorias[codigo_subcategoria]'>".utf8_encode($dados_Subcategorias[nome_subcategoria])."</option>";
                if($dados_Subcategorias[codigo_subcategoria] == $_GET[subCat]){
                echo "<option selected='selected' value='$dados_Subcategorias[codigo_subcategoria]'>".utf8_encode($dados_Subcategorias[nome_subcategoria])."-".$dados_Subcategorias[codigo_subcategoria]."</option>";
                }
                else{

                    echo "<option value='$dados_Subcategorias[codigo_subcategoria]'>".utf8_encode($dados_Subcategorias[nome_subcategoria])."-".$dados_Subcategorias[codigo_subcategoria]."</option>";
                    }
      }
    echo "</select>";
}






  ?>
  • How is your js code to perform asynchronous call?

  • Hello, give a var_dump($dados_Subcategories) or print_r($dados_Subcategories) and see if this brings the data you expect to come from your query.

  • <script> Function searchSubCategories(){ var groupCategory = $('#groupCategory'). val(); if(groupCategory){ //Alert('getSubCategories.php?groupCategory='+groupCategory); var url = 'getSubCategories.php? groupCategory='+groupCategory; $.get(url, Function(dataReturn) { $('#checkSubcategories').html(dataReturn); }); } } </script>

  • Luis, welcome to [en.so]. I reversed its last edition because here we do not put the title of "closed" to inform that our problem has been solved, and yes we mark as accepted the answer that solved our problem. See more in: How and why to accept an answer?, take advantage and make a [tour] by the site.

2 answers

1

Try quotation marks on the right-angled arrays.

if($dados_Subcategorias['codigo_subcategoria'] == $_GET['subCat']){

  • even putting quotes it does not enter the IF, the funniest thing that on the page that has ajax is that it does it correctly, but on the page that calls it does not list the IF, I tried so and it worked: if($data_Subcategories['codigo_subcategory'] == 302){ tried so and it worked: if($data_Subcategories[code_subcategory] == 302){ does there exist some way to force the value to be of type int?

  • Cast. (int)($variable['value']).

  • $sub = (int)($_GET[Subcat]); $codigo_subcategory = (int)($dados_Subcategories_codigo_subcategory]); if( $codigo_subcategory == $sub), even if it states that the types are gettype integers, it doesn’t work... I tried to use settype functions, I tried to convert to all types the 2 variables, but nothing

  • this combo is very stylish to the City x State, I need to change a record, but I want it to be already selected according to the database data.

  • i realized that the variable that I receive on the ajax page: $_GET[Subcat] it comes null for the page that receives the ajax, variavelGet NULL

0


Code

<?php
$grupoCategoria = ($_GET['grupoCategoria'] ? filter_var($_GET['grupoCategoria'], FILTER_VALIDATE_INT) : NULL); 
$subcat = (!empty($_GET['subCat']) ? filter_var($_GET['subCat'], FILTER_VALIDATE_INT) : NULL);

$sql = "SELECT 
         codigo_categoria,codigo_subcategoria,nome_subcategoria,descricao_subcategoria
      FROM Subcategorias 
      WHERE 
         codigo_categoria = {$grupoCategoria} AND 
         codigo_subcategoria in(302,308,309) 
      ORDER BY nome_subcategoria ";

$res_Subcategorias = mysql_query($sql, $con_local);


$html = '';

if(mysql_num_rows($res_Subcategorias)>0){
   $html .= "<select name='Subcategorias' id='Subcategorias'  >";
   $html .= "<option value=''>Selecione...</option>";
   while ($dados_Subcategorias = mysql_fetch_assoc($res_Subcategorias)){
      //echo "<option value='{$dados_Subcategorias['codigo_subcategoria']}'>".utf8_encode($dados_Subcategorias[nome_subcategoria])."</option>";
      if($dados_Subcategorias['codigo_subcategoria'] == $subcat){
            $html .= "<option selected='selected' value='{$dados_Subcategorias['codigo_subcategoria']}'>".
                        utf8_encode($dados_Subcategorias['nome_subcategoria'])."-".$dados_Subcategorias['codigo_subcategoria'].
                     "</option>";
      } else {

         $html .= "<option value='{$dados_Subcategorias['codigo_subcategoria']}'>".
                     utf8_encode($dados_Subcategorias['nome_subcategoria'])."-".$dados_Subcategorias['codigo_subcategoria'].
                  "</option>";
      }
   }
    $html .= "</select>";
}

echo $html;

Explanation

Avoid possible mistakes:

 $grupoCategoria = ($_GET['grupoCategoria'] ? filter_var($_GET['grupoCategoria'], FILTER_VALIDATE_INT) : NULL); 

Filter the variable that will enter your SELECT, to avoid SQL Injection:

$grupoCategoria = filter_var($_GET['grupoCategoria'], FILTER_VALIDATE_INT);

Use keys in string variables to help PHP identify them:

codigo_categoria = {$grupoCategoria}

NEVER leave the index of the array unquoted, unless it is numerical:

$dados_Subcategorias['codigo_subcategoria']
$dados_Subcategorias[0]

Completion:

Your code has many errors, but the problem may not be in it, maybe your AJAX is not passing the parameters correctly. Also put your javascript that makes the AJAX request in the question.

Browser other questions tagged

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