user2373405
user2373405

Reputation: 91

Send Request to PHP file only when input reaches X characters

I am using this java script code to get data from my sql. i want to send request to php file only when the input field has 15 characters. so i used this onkeyup="if(this.value.length>14)" but it didn't work. can some one please modify my java script code.. please

Upvotes: 2

Views: 94

Answers (1)

Siamak Motlagh
Siamak Motlagh

Reputation: 5136

Change your html to this:

 <input type="text" name="name" maxlength="15" id="name" onkeyup="check_length(this)"  />

And add this function into your javascript:

function check_length(thisObject){
     if(thisObject.value.length>14){ajaxFunction(thisObject.value)}
}

Edit:

This happens when you define the functions in the head part and call them, when the document is not yet initialized. Move the script part, where the initialization happens and try it out.

I place you the sample code (save it in a blank html file and test it):

<input type="text" name="name" maxlength="15" id="name" onkeyup="check_length(this)"  />
<script>
function check_length(thisObject){
     if(thisObject.value.length>14){ajaxFunction(thisObject.value)}
}
function ajaxFunction(thisObjectValue)
{
    console.log(thisObjectValue);
    /// Do things ....
}
</script>

Upvotes: 1

Related Questions