Fill in PHP session via ajax

3

I'm having trouble solving a problem.

I have a PHP application that needs to fill in 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 populated and does not return any errors.

Edit

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

I put the complete code of the two pages

    
asked by anonymous 15.12.2016 / 16:05

1 answer

0

If your file has only this code:

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

It's missing a start in the session, before you can use it. Here's how:

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

I hope I have helped !!

    
15.12.2016 / 17:01