Created
April 24, 2018 10:36
-
-
Save rahuls360/82d6f0881e2d3da4f04543061d795234 to your computer and use it in GitHub Desktop.
AJAX PHP form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HTML | |
<form action="form.php" method="POST"> | |
<input type="text" placeholder="Name" name="name"> | |
<input type="text" placeholder="Email" name="email"> | |
<input type="text" placeholder="Mobile" name="mobile"> | |
<input type="submit"> | |
</form> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script> | |
<script src="main.js"></script> | |
JavaScript | |
$('form').on('submit', function(){ | |
var that = $(this); | |
var action = that.attr('action'); | |
var method = that.attr('method'); | |
var data = {}; | |
that.find('[name]').each(function(index, value){ | |
var that = $(this); | |
var name= that.attr('name'); | |
var value= that.val(); | |
data[name] = value; | |
}); | |
$.ajax({ | |
url: action, | |
type: method, | |
data: data, | |
success: function(response){ | |
console.log(response); | |
} | |
}); | |
return false; | |
}); | |
PHP | |
<?php | |
if($_POST['name'] != null && $_POST['email'] !=null && $_POST['mobile'] != null){ | |
echo "Nice" ; | |
}else{ | |
echo "Uh uh"; | |
} | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add form submit message
.contact-message1 {
color: orange;
font-weight: 700;
font-size: 20px;
}