Jquery is only appearing value of an input with . serialize()

Asked

Viewed 278 times

0

I started studying Javascript and Jquery. I was doing a test to display on the screen what was sent via Form, but it is sending value only from a field, someone would be able to help me?!

Here’s the HTML with the script:

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<form name="frm" id="frm" method="POST" action="">
   <input type="text" name="nome" id="nome" value="" />
   <input type="text" name="sobrenome" id="sobrenome" value="" />
   <input type="submit" name="enviar" id="enviar" value="Enviar" />
</form>
<div id="exibir_valor"></div>
<script>
   $("#enviar").click(function(e) {
     e.preventDefault();
     var valor = $("#frm").serialize();
     $.ajax({
       action: $(this),
       type:'POST',
       data: valor,
       url:'insert.php',
       success:function(data) {
         $( "#exibir_valor" ).html(data);
       }
     });
   });
</script>

and here php just for testing (Insert.php):

<?php
  var_dump($name = $_POST['nome']);
  $last_name = $_POST['sobrenome'];

Thanks in advance!

  • 1

    Your code looks ok. You can do Alert, or console.log from valor to see what’s there? and in PHP you’re just doing var_dump of one of the fields. It would be better to do var_dump($_POST);. What gives you that?

2 answers

1


Have you tried in the file Insert.php to do so, for me the js file is correct

<?php
  var_dump($name = $_POST['nome']);
  var_dump($last_name = $_POST['sobrenome']);
  • Puts that lack of attention! Poxa Thanks Marcos!

1

Jquery is working correctly (see jsfiddle), but in PHP you’re only giving var_dump on $_POST['nome'], to see all do so:

PHP:

var_dump($_POST);

or so:

print_r($_POST);
  • Ended up not really seeing, thanks for the quick response!

Browser other questions tagged

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