PHP with input form with text - do a MySQL query when form focus changes -
i looking how make input form handling in php.
what need when user writes data text form (table1), , moves text form (like pressing tab, or selecting mouse), should start , mysql query see if such data written @ table1 existing in matching mysql table.
my goal without pressing submit button. when google checks if username want register exists.
i thinking this:
$duplicate_data_test =""; if (focus has moved form field - how check ?) { $query = "select table1 testdatabase table1 = "' . (table1 form below - how data form field without post?) .'"; $result = mysqli_query($con,$query); if(mysqli_num_rows($result)>0) { $duplicate_data_test = "this data found in database. choose else"; } } echo '<form action="'.htmlspecialchars($_server["php_self"]).'" method="post">'; echo '<input type="text" maxlength="30" name="table1">'; echo '<span class="duplicaterror">'. $duplicate_data_test.' </span>'; echo '<input type="text" maxlength="30" name="table2">'; echo '<input type="submit" value="ok">'; echo '</form>';
thank help!
you cannot "interface" check php. "focus has moved form field" has done javascript.
first, build form html
<form name="testform" action="postform.php" method="post" id="myform"> <label>input 1 : </label> <input type="text" id="in1" value="" name="input1" /> <label>input 2 : </label> <input type="text" id="in2" value="" name="input2" /> <div style="color:red;font-weight:bold;" id="error"></div> <button id="submitbutton">submit</button> </form>
then make checks when user clicks on submit button javascript/jquery & ajax (prevent event form posting) :
$(document).on('click','#submitbutton',function(event){ event.preventdefault(); if($.trim($('#in1').val()) == ''){ //input 1 empty $("#error").html('input 1 empty'); }//....continue checks
finally, if checks good, post form
$("#myform").submit();
and if checks not display user message!
$("#error").html("message!");
i made little example on how (it's not best way it's example) on jsfiddle, check link : http://jsfiddle.net/9ayo89jt/2/
hope helps!
Comments
Post a Comment