Populate PHP Session via ajax

Question:

I'm having a hard time solving a problem.

I have a PHP application that needs to fill some sessions

I'm using this method to send.

<script src="localhost:8080/painel_cop_mdu/js/jquery-3.1.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function carregaFiltro(){
    $('#filtrar').load('filtros.php');  
}
 function addFiltro(botao,selecao) {     
             $.ajax({
                    method: "post",
                    url: "filtrando.php", 
                    data: {botao : botao,selecao : selecao},
                    success: function(data){
                        alert("tudo ocorreu bem!");
                    },
                    error: function() {
             alert("Ocorreu um erro ao carregar os dados.");
           }                    
                });
          }
function pegaSelect(nomedoselect){

    var options = document.getElementById(nomedoselect).getElementsByTagName("option");
    var texto = "";
    for(var i=0;i<options.length;i++){
        if(options[i].selected){            
            texto = texto+","+options[i].value;
        }
    }       
    return texto;
}
</script>
<?php
    echo $_SESSION["condicaoRegional"];
?>
</div>
<div id="combos">
    <form action="#" method="post" enctype="multipart/form-data>
        <div id="dCidades">

            <label>Selecione uma regional</label>
            <select id="cidades" name="cidades[]" size="3" multiple>
            <?php
                regionais();
             ?> 
            </select>    
            <button id="filtrarCidade" onClick="addFiltro('filtrarCidade',pegaSelect('cidades'))" >filtrarNatureza</button>
        </div>
        <div id="dGrupos">
            <label for="">Seelecione um grupo</label>
            <td><select id="grupos" name="grupos[]" size="3" multiple>
             <?php
                grupos($_SESSION["condicaoRegional"]);
             ?>
            </select>
            <input type="submit" name="filtrarGrupo" value="filtrarGrupo" onClick="carregaFiltro"/>
        </div>
            <div id="dSub">
            <label for="">Filtrar por sub cluster</label>
            <select id="sub" name="sub[]" size="3" multiple>
                <?php
                sub_cluster($_SESSION["condicaoGrupo"]);
                ?>
            </select>
            <input type="submit" name="filtrarSub" value="filtrarSub" onClick="carregaFiltro" />
        </div>
        <div id="dNatureza">
            <label for="">Filtrar por Natureza</label>
            <select id="natureza" name="natureza[]" size="3" multiple>
                <?php
                cidades($_SESSION["condicaoSubCluster"]);
                ?>
            </select>     
             <button id="filtrarNatureza" onClick="addFiltro('filtrarNatureza',pegaSelect('natureza'))" >filtrarNatureza</button>
         </div>         
        <input type="submit" name="filtrar" value="filtrar" />
        <input type="submit" name="limpar filtros" value="limpar filtros" />
    </form>
</div>

and in my PHP

<?php
  session_start();
  include('conexao.php');
  $id = $_POST['botao'];
  $texto = $_POST['selecao'];
  $sql = "insert into obs_painel values (9999,'".$texto."');";  
  $conn->query($sql);
  $_SESSION["condicaoRegional"] =  $_POST['selecao'];
?>

However, the session is not filled and it does not return any error.


Edit

I made a change in the filtering.php file and put it to make an insert in the database and it doesn't. Apparently he is not accessing the page.

I put the complete code of the two pages

Answer:

If your file only has this code:

$id = $_POST['botao'];
$texto = $_POST['selecao'];
$_SESSION["condicaoRegional"] = $texto;

It is missing to start the session, before being able to use it. Do like this:

session_start();
$id = $_POST['botao'];
$texto = $_POST['selecao'];
$_SESSION["condicaoRegional"] = $texto;

Hope this helps!!

Scroll to Top
AllEscort