Fill in PHP session via ajax

Asked

Viewed 1,297 times

3

I’m having a hard time solving a problem.

I have an application in PHP 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 also does not return any error.


Edit

I made a change to the file filtering.php and put it to make a Insert in the bank and the same does not. Apparently he is not accessing the page.

I put the complete code of the two pages

  • @Danielomine I check so much for select which loads after that function how much when I have the session contents printed

  • Are you sure you’re calling this page ? Open the browser console, go to the XHR tab. Click on the button that calls this function and see if the page is invoked.

  • @Gumball he only carries his own page and style sheet

  • @Danielomine I put the full code of the two pages but the bank issue is not even relevant I did just to make sure he was not calling the page

  • 1

    In this passage alert("tudo ocorreu bem!");, modify it like this: console.log("tudo ocorreu bem!", data);

  • 1

    And in filtering.php, in the first line, before the session_start();, put this echo 'OK'; exit;. Give an F5 on the form page and send the ajax request. It should show in the browser console everything ocorreu bem! OK. This is just for debugging and debugging. Understand debugging logic and you can find the problem.

  • @Danielomine sorry for the delay in answering, I did the test anyway I could not get any answer, as it was a project that required a certain urgency I ended up remaking it in Asp.net where I had a larger domain but even I will continue testing to see if I can find the problem.

Show 2 more comments

1 answer

1

If in your file you only have this code:

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

You are missing a start to the session before you can use it. Do so:

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

I hope I’ve helped!!

  • did not work apparently this sending to the page I made a change to make an insertion in the bank and even shoot an Alert and did not work

  • Put it there then, as was the file that receives the data...

  • I entered the complete code of the two pages to which you send and which you receive

Browser other questions tagged

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