Dynamically Update php Session with jQuery

Asked

Viewed 1,469 times

10

Well, I have the function below that, with Jquery, I update the cart values display labels.

Everything works:

  function add(_quant, _preco, _total, _estoque) {
      quantidade = parseInt($("#"+_quant).val());
      estoque = parseInt($("#"+_estoque).val());
      preco = parseFloat($("#"+_preco).val());

      novaQuantidade = quantidade + 1;

      if(novaQuantidade <= estoque) {         
          if(novaQuantidade == 0) {
              alert("Quatidade não por ser 0");
          } else {
              total = novaQuantidade * preco;                 
              $("#"+_quant).val(novaQuantidade) ;
              $("#"+_total).html(total.toFixed(2));
              $(".totalCarrinho").html(total.toFixed(2));
              $(body).load('_required/sessaoCarrinho.php?idProduto=' + _quant + '&novaQuantidade=' + novaQuantidade);
          }
      }  else {
              alert("Quatidade escolhida maior que estoque");
      }
  }

The problem is that I am not able to update also in php Session.

I’m trying like this:

$(body).load('_required/sessaoCarrinho.php?idProduto=' + _quant + '&novaQuantidade=' + novaQuantidade);

And in php like this:

  $idProduto = $_GET["idProduto"];
  $novaQuantidade = $_GET["novaQuantidade"];

  foreach ($_SESSION["carrinho"] as $key=>$produtoC) {
       if($produtoC[$idProduto] == $novoProduto->getIdProdutos()) {
           $achou = true;
           $chave = $key;
           $estoque = $novoProduto->getEstoque();
           break;                
       }
   }

   if($achou == true) {
       if ($estoque > $_SESSION["carrinho"][$chave]["quantidade"]) {
          $_SESSION["carrinho"][$chave]["quantidade"] = $novaQuantidade;
       }       
   }

But Session is not being updated.

What am I doing wrong?

  • To update a session, I recommend killing the previous session... so that it cleans and recreates the session. use unset($_SESSION['carrinho']);, don’t forget to put in the header: session_start();

  • In the php code you quoted is not present the function session_start(), but as the code is apparently not complete it is worth giving a check on the above lines if this function has been called. Without it changes in session will not be persisted.

  • the "$(body). load(...);" command indicates that you want to replace all body content with the load return. Use the "$. get(...);" to make the request without loading the return on the body...

2 answers

0

Try it like this

Javascript:

function add(_quant, _preco, _total, _estoque) {
    quantidade = parseInt($("#"+_quant).val());
    estoque = parseInt($("#"+_estoque).val());
    preco = parseFloat($("#"+_preco).val());

    novaQuantidade = quantidade + 1;

    if(novaQuantidade <= estoque) {         
        if(novaQuantidade == 0) {
            alert("Quatidade não por ser 0");
            }
            else {
                total = novaQuantidade * preco;                 
                $("#"+_quant).val(novaQuantidade) ;
                $("#"+_total).html(total.toFixed(2));
                $(".totalCarrinho").html(total.toFixed(2));
                //
                $("#div_vazia_qualquer").load('_required/sessaoCarrinho.php',{
                    idProduto:_quant, 
                    novaQuantidade:novaQuantidade}, function(resposta){
                        alert(resposta);
                    });
            }
        }
        else {
            alert("Quatidade escolhida maior que estoque");
        }
    }
}

PHP:

<?php
$idProduto = $_POST["idProduto"];
$novaQuantidade = $_POST["novaQuantidade"];

foreach ($_SESSION["carrinho"] as $key=>$produtoC) {
   if($produtoC[$idProduto] == $novoProduto->getIdProdutos()) {
        $achou = true;
        $chave = $key;
        $estoque = $novoProduto->getEstoque();
        break;                
   }
}

if($achou == true) {
   if ($estoque > $_SESSION["carrinho"][$chave]["quantidade"]) {
      $_SESSION["carrinho"][$chave]["quantidade"] = $novaQuantidade;
   }       
}
  • did not give. As I test is finding the php file?

  • In php put a echo any and run, the script will give an alert with what it receives from echo

  • not giving. I think it’s directory. I have the php file in the _required folder. And the file that calls . js is in the folder that this _required folder is attached to. Type> index.php, _required/, top.php. That’s right?

  • That’s right, but . js is in which folder?

  • <src=".. /_global/_js/carrinho.js"></script> and php, direct _required

0

There’s no way to guess. See what the load() request is returning. In your web browser go to inspect element, you will have a tab called network (network) in it Select the ajax requests and see what was sent to the server and what was returned as reset. This varies from browser to browser. Ai you can identify if there is some syntax error happening in php;

  • 1

    I did so, it worked out, $("body"). load('_required/sessaoCarrinho.php',{ idProduct: idCampo, novaQuantidity: novaQuantidity, });, but it’s going to the php page and stopping there.

Browser other questions tagged

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