Return php to Jquery

Asked

Viewed 383 times

0

I need a JS variable that picks up the result in a . load Jquery from a php file.

Jquery

$.ajax({
 type: "POST",
 url: "_required/sessaoCarrinho.php",
 data: {idProduto:idCampo, novaQuantidade: novaQuantidade},
 success: function(data){
    var subTotal = data;
    return false;
 }
});

alert(subTotal);              

$(".subTotal").html(subTotal.toFixed(2));
$(".totalCarrinho").html(subTotal.toFixed(2));

php

<?php
  session_start();

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

  require_once "../../_controlls/_conexao/Conexao.php";  
  require_once "../../_controlls/_models/Produtos.php";
  require_once "../../_controlls/_daos/ProdutosDao.php";
  require_once "../../_controlls/_util/PhpUtil.php";
  require_once "../../_controlls/_util/Carrinho.php";

  $connection = new Conexao();
  $conexao = $connection->abreConexao();  

  $produtosDao = new ProdutosDao($conexao);  
  $phpUtil = new PhpUtil();
  $carrinho = new Carrinho($produtosDao, $phpUtil);


  $novoProduto = $produtosDao->pesquisaProdutoId($_POST["idProduto"]);

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

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

   $subTotal = $carrinho->subTotal();

   echo $subTotal;

?>

What I’m doing wrong that variable subtotal in the JQuery just enough not defined

I’ve tried that too in php but it didn’t work

   $subTotal = $carrinho->subTotal();

   echo "<script>var subTotal=".$subTotal."</script>";
  • I may be ignorant, but try => die(json_encode(['subtotal' => $subtotal])

  • Try the alert(subTotal); inside the ajax Success

  • Cart.js:28 Uncaught Referenceerror: subtotal is not defined

  • json_encode(['subtotal' => $subtotal] is an array. It gives error in the form of array placement

  • @Carlosrocha left an answer to your question.

  • yes, I already tested and left a comment: almost there!

Show 1 more comment

1 answer

1


In Javascript:

function produto(data, callback) {
        $.ajax({
            type: "POST",
            url: "_required/sessaoCarrinho.php",
            data: {idProduto: idCampo, novaQuantidade: novaQuantidade},
            dataType: 'json'
        }).done(function (response) {
            callback(response);
        });
    }
    function getProduto(response) {
        $(".subTotal").html(response.subTotal.toFixed(2));
        $(".totalCarrinho").html(response.subTotal.toFixed(2));
    }
    produto(data,getProduto);

OR

$.ajax({
     type: "POST",
     url: "_required/sessaoCarrinho.php",
    data: {idProduto: idCampo, novaQuantidade: novaQuantidade},
    dataType: 'json'
}).done(function (response) {
    $(".subTotal").html(response.subTotal.toFixed(2));
    $(".totalCarrinho").html(response.subTotal.toFixed(2));
});

In PHP:

echo json_encode(array('subtotal' => $subtotal));

  • cart.js:29 Uncaught Referenceerror: subtotal is not definedsub @carrinho.js:29onclick @ carrinho.php:149 carrinho.js:25 Object {subtotal: 509.93}.

  • Then I did below the console.log(Sponse); subtotal = Sponse and Gave Undefined

  • found: subtotal = Sponse['subtotal']; Oibrigado.

  • @Carlosrocha on the error subtotal is not definedsub, it will never return because the ajax is asynchronous, ie it will not wait to finish the ajax to finish reading the code. Another thing, if the answer was helpful, from a vote to strengthen me. vlw

  • $(".subtotal"). html(subtotal.toFixed(2)); because it is, how to solve this problem then? I need this information to display on the label

  • @Carlosrocha edited the answer, from a look. I hope it helps you. Something else, google about callback() in ajax.

  • it took me a while to answer because I was trying. and I did just that. But thanks for the answer.

Show 2 more comments

Browser other questions tagged

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