I read everywhere about ajax and in all of them the form is in an index.html not an index.php. I can do ajax like them but I want to do it when my form is in a php page without need to action attribute or another php page for sending data. is it possible? how should I do it?
home.php :
<form method="POST" id="form1">
<input type="text" name="username" placeholder="username"><hr>
<input type="submit" name="submit" value="enter" class="submit"><br><br>
<div id="al"></div>
</form>
home.php again :
<?php if(isset($_POST['submit']))
{if(isset($_POST['username']){
if(preg_match("/^[a-zA-Z0-9]*$/",$_POST['username'])){
code...
echo "yes";
}else{echo "error!";}
}} ?>
js.js :
$(document).ready(function(){
$('#form1').submit(function(e){
e.preventDefault();
$.ajax({
type: 'post',
url: 'home.php',
data:$('#form1').serialize(),
success:function(response){
$('#al').html(response);
}
})
})
})
when i use code above my home.php page loads into div with id="al"!!! with the error.