Ajax does not send the data to another php page

Asked

Viewed 334 times

1

I am unable to send the value of an input to another php page.

On my products.php page I do a database search and upload the page show_itemswithin a div. When loading the data into this div, I mount an input. This "input: deflarg" that I am unable to send to the.php cart

part of the code...show_items.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_item I am 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 page.php, I’m doing so:

<?php 

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

?>

Where am I going wrong ?

  • See if Ajax is reading the data before sending. Make a console.log() of this $('enviar_para_carrinho').serialize() to see if the data is there.

  • That one $('.enviar_para_carrinho') is the tag class <form>?

1 answer

1

The problem is, through the dial:

$('enviar_para_carrinho')

You are selecting all the elements that have the tag <enviar_para_carrinho>. So, since there probably aren’t any of these elements, you were wrong to choose this selector.

If you want to select the element through a class, you must prefix the selector through a point (.), indicating that you want to select the element through the attribute class. Thus:

$('.enviar_para_carrinho').serialize()

If, however, you want to use the ID of the element to select, you can do so:

$('#enviar_para_carrinho').serialize()

To learn more, please consult selector page jQuery.

  • Friend explain what you changed in the code, did not need to have reset everything if you only changed the selection of the form # (id) to class data: $('.enviar_para_carrinho').serialize(),. It took me a while to realize that.

  • Really, thanks for the remark, Bruno! : ) When I wrote the original answer in 2017, I was still not very used to how this site works. I edited the answer. ^-^

  • Caraca, what this question was doing on the homepage kkkk, I didn’t even see that it was 2017 bro!!!

Browser other questions tagged

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