Pete
Pete

Reputation: 784

JQuery Ajax serialize method not working?

I am trying to submit a form using jquery and I'm not sure why the form is always empty. I am simply running a while loop inside a form and have a set of inputs with different names and I want to print its value out in a div tag "karma".

EDIT: I want to print out the entire form everytime I click the buttons.

Here is index.php file

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <script type="text/javascript">

    function get() {
        var data = $('forma').serialize();
        console.log(data);
        $.post('data.php', data,
            function(output) {
                $('#karma').html(output).show();
            });
    }

    </script>

</head>


<body>
<div id="karma"></div>

    <form name="forma">
    <?php 
        $x = 5;
        while ($x) {
            ?>
            <?php
            echo "<input type='hidden' name='listid_". $x ." ' value=" . $x . ">";
            $x = $x - 1;
            ?>
            <input type="button" name='butid_<?php echo $x; ?>' value="Get" onClick="get();">

            <?php
        }
    ?>
    </form>
    <!-- <input type="hidden" name="listid" value="HELO"> -->

</body>
</html>

and here is the data.php file

<?php 
echo "geki";
var_dump($_POST);
?>

I appreciate the help. Thank you.

Upvotes: 0

Views: 256

Answers (2)

Leithon English
Leithon English

Reputation: 223

This is your problem:

$('forma') 

That is not a an element or an id of an element and if your trying to get the element by its name that not the way either so that's why you cant find it with the jQuery selector

But if you change that to:

var data = $('form[name='forma']).serialize();

You will be able to reference the element by it name

Or if add an id field to the form then get the element by id using jQuery like so:

<form name="forma">
var data = $('#forma').serialize();

That should work

Upvotes: 0

BReal14
BReal14

Reputation: 1584

forma is not a default html element. You must use a proper ID set up.

Change

  var data = $('forma').serialize();

to

  var data = $('#forma').serialize();

and

 <form name="forma">

to

   <form id="forma">

Upvotes: 1

Related Questions