Reputation: 29
Current code: index.html
<script type="text/javascript">
$('#username').blur(function(){
$.ajax({
type: "POST",
url: "search.php",
data: {text:$(this).val()}
});
});
</script>
<form id="search" action="search.php" method="post">
<input type="text" id="username" name="username" size="30" placeholder="username ..." />
<input type="submit" id="submit-button" name="sa" value="search" />
</form>
search.php
<?php
echo $_POST['username'];
?>
If I press the submit button it works well but I want to send the value from the field once I focuse out of the field, without pressing the submit button.
Upvotes: 0
Views: 2918
Reputation: 357
You can try focusout.
$('#usernamecopy').focusout(function(){
$.ajax({
type: "POST",
url: "search.php",
data: {username :$('#username').val()}
});
});
In php file you can use:
$user= $_REQUEST['username'];
Upvotes: 3
Reputation: 70
**Use following code in JavaScript :**
<script type="text/javascript">
$('#username').blur(function(){
var value = $("#username").val();
$.ajax({
type: "POST",
url: "search.php",
data: "username="+value,
success: function(data){
}
});
});
</script>
Upvotes: 0
Reputation: 1000
Not tested but I think if you change data: {text:$(this).val()}
to data: {text:$("#usernamecopy").val()}
; it will work because when you put this
inside the data
; the this
refers to the data
instead of #usernamecopy
. By the way, I hope you are not mistaking #usernamecopy
with #username
Upvotes: 1