Ajax does not send the data to another php page

0

I'm not able to send the value of an input to another php page.

In my products.php page I search the database and load the show_itens page inside a div. When loading data into this div, I mount an input. This "input: deflarg" that I am not able to send to the cart.php

part of the code ... show_itens.php

echo '<tr>';
echo '<td height="45">'."Definir a Largura da Porta: ".'<input type="text" name="deflarg" id="deflarg" size="5" maxlength="5"/>'. " cm".'</td>';
echo '</tr>';

echo '<tr>';
//echo '<td height="45">' . '<input type="submit" name="comprar" value="COMPRAR"/>'.'</td>';
//echo '<td height="45">' . '<a href="carrinho.php?acao=add&id='.$linha["id"].'">Comprar</a>'.'</td>';

echo'<td height="45">' . '<a href="carrinho.php?acao=add&id='.$id.'">
    <img src="img/comprar-1.png" id="add_car" height="70" width="200" align="center" title="Adicionar ao Carrinho"></a>'.'</td>';
echo '</tr>';

Inside the page show_item I'm using this jquery + Ajax, but without success.

<script type="text/javascript">
$(document).ready(function(){
$("#add_car").click(function(e){ 
     if($("#deflarg").val()==""){ 
       alert("Ôpa!!!  " + "Você esqueceu de digitar a largura!");
       e.preventDefault();
     }
       $.ajax({
            url: 'carrinho.php',
            type: 'POST',
            data: $('enviar_para_carrinho').serialize(),
            success: function(data){
                  $('#deflarg').html(data);

            }
       });
   });
});          
</script>

On the cart.php page, I'm doing this:

<?php 

$largura = $_POST["deflarg"];
echo "Largura: " . $largura;

?>

Where am I going wrong?

    
asked by anonymous 21.10.2017 / 19:27

1 answer

0

You should probably switch to:

<script type="text/javascript">
$(document).ready(function(){
$("#add_car").click(function(e){ 
     if($("#deflarg").val()==""){ 
       alert("Ôpa!!!  " + "Você esqueceu de digitar a largura!");
       e.preventDefault();
     }
       $.ajax({
            url: 'carrinho.php',
            type: 'POST',
            data: $('#enviar_para_carrinho').serialize(),
            success: function(data){
                  $('#deflarg').html(data);

            }
       });
   });
});          
</script>

Or, if the above does not work:

<script type="text/javascript">
$(document).ready(function(){
$("#add_car").click(function(e){ 
     if($("#deflarg").val()==""){ 
       alert("Ôpa!!!  " + "Você esqueceu de digitar a largura!");
       e.preventDefault();
     }
       $.ajax({
            url: 'carrinho.php',
            type: 'POST',
            data: $('.enviar_para_carrinho').serialize(),
            success: function(data){
                  $('#deflarg').html(data);

            }
       });
   });
});          
</script>
    
21.10.2017 / 20:33