tags:

views:

46

answers:

2
+1  Q: 

jquery and post

Hi,

why i don't get data from jQuery? Variables POST always empty

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
<script>
$(document).ready(function(){
    $("form#submit").submit(function() {

    var fname = $('#fname').attr('value');
    var lname = $('#lname').attr('value');
        $.ajax({
            type: "POST",
            url: "#",
            data: "fname="+ fname +"& lname="+ lname,
            success: function(){
                $('form#submit').hide(function(){$('div.success').fadeIn();});

            }
        });
    return false;
    });
});
</script>
</head>
<body>
<?php
echo 'fname:' . $_POST['fname'] . '<br/>';
echo 'lname:' . $_POST['lname'];
?>
<div class="container">
<form id="submit" method="post">
        <fieldset>
            <legend>Enter Information</legend>
            <label for="fname">Client First Name:</label>
<input id="fname" class="text" name="fname" size="20" type="text">

            <label for="lname">Client Last Name:</label>
<input id="lname" class="text" name="lname" size="20" type="text">

            <button class="button positive"> <img src="../images/icons/tick.png" alt=""> Add Client </button>
        </fieldset>
    </form>
<div class="success" style="display: none;">Client has been added.</div>
</div>
</body>
</html>   
+1  A: 

Don't attach the handler to the submit event. Attach to the button click event instead.

Pierreten
Can you clarify a little bit, because now I did not understand where the problem is. Thanks
A: 

Testing your answer on jsFiddle shows that the correct information is sent, however a couple of minor suggestions: Set the type of your script

<script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js'&gt;&lt;/script&gt;

Use .val() instead of `.attr('value')``

Instead of $(document).ready(function(){})simply use $(function(){})

Kristoffer S Hansen