javascript - How to send the name and value off the textbox to ajax which is used in php for validation -


so trying send name , value of textbox ajax , want use name of text box in php validate field. trying use this.name apparently not correct way!. ajax code.

<script type="text/javascript">     function frmvalidation(str)      {         if (str=="")          {             document.getelementbyid("txterror").innerhtml="";             return;         }          if (window.xmlhttprequest)          {             // code ie7+, firefox, chrome, opera, safari             xmlhttp=new xmlhttprequest();         }          else          {               // code ie6, ie5             xmlhttp=new activexobject("microsoft.xmlhttp");         }         xmlhttp.onreadystatechange=function() {         if (xmlhttp.readystate==4 && xmlhttp.status==200)          {             document.getelementbyid("txterror").innerhtml=xmlhttp.responsetext;         }         xmlhttp.open("get","validation.php?value="+str,true);         xmlhttp.send();     }       } 

and thats html

<div class="col-sm-6">             <div class="form-group">                 <label for="first name">first name</label>                 <input type="text" class="form-control" onblur="frmvalidation(this.value)" tabindex="1" id="first name" placeholder="first name" name="fname"><span id="txterror" style="color: red"></span>             </div> 

thanks in advance!

change pass in this can access this.name , this.value inside function

<input onblur="frmvalidation(this)" ....> 

js

function frmvalidation(elem){     var str = elem.value, name =elem.name;      //.....       xmlhttp.open("get","validation.php?value="+str + '&name='+name,true);       //....  } 

since question tagged jquery can replace onblur with:

$('input').blur(function(){     $.get('validation.php', {name: this.name, value:this.value))         .done(function(resp){            $('#txterror').html(resp);         });     }); 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -