0

This is my form from which i am expecting post results in my div id="response"

<!DOCTYPE html>
<html>
<head>
<title>AJAX POST</title>
<script src="jquery.js"></script>
<script>
    $(document).ready(function(){
        $('input[name="submit"]').click(function(){
            var username=$('input[name="username"]').attr("value");
            var password=$('input[name="password"]').attr("value");
            var data1=encodeURIComponent(username);
            var data2=encodeURIComponent(password);
            $.post("response.php",{username:data1,password:data2},function(data){
            $("#form").hide();
            $("#response").text(data);
            });
        });
    });
</script>
</head>
<body>
<div id="form">
<form action="response.php" method="post">
<input type="text" name="username" value=""/><br/>
<input type="password" name="password" value=""/><br/>
<input type="button" name="submit" value="submit"/>
</form>
</div>
<div id="response"></div>
</form>
</body>
</html>

This is my server side script

<?php
echo $_POST["username"]."<br/>";
echo $_POST["password"];
?>

After filling the form and clicking the submit button the response that i get is "<br/>" I don't understand this.Please explain.

1
  • 1
    .attr("value") gets/sets the default value of the input, not the current value. Commented Sep 12, 2013 at 15:06

1 Answer 1

4

To get the input value you need to use .val() not .attr('value'). .attr('value') will return the value of the attribute which is ''(empty string) here

var username=$('input[name="username"]').val();
var password=$('input[name="password"]').val();
Sign up to request clarification or add additional context in comments.

2 Comments

still why is tit that i get "<br/>" in my result instead of line break in response?
because you have used .text() instead of .html()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.