Im trying to submit a HTML form using AJAX using this example.
My HTML code:
<form id="formoid" action="studentFormInsert.php" title="" method="post">
<div>
<label class="title">First Name</label>
<input type="text" id="name" name="name" >
</div>
<div>
<label class="title">Name</label>
<input type="text" id="name2" name="name2" >
</div>
<div>
<input type="submit" id="submitButton" name="submitButton" value="Submit">
</div>
</form>
My script:
<script type="text/javascript">
$(document).ready(function() {
$('#formoid').ajaxForm(function() {
alert("Thank you for your comment!");
});
});
</script>
This is not working, I'm not even getting the alert message and when I submit I don't want to redirect to another page, I just want to show the alert message.
Is there a simple way of doing it?
PS: I have several fields, I have just put two as an example.
Quick Description of AJAX
AJAX is simply Asyncronous Javascript or XML (in most newer situations JSON). Because we are doing an ASYNC task we will likely be providing our users with a more enjoyable UI experience. In this specific case we are doing a FORM submission using AJAX.
Really quickly there are 4 general web actions
GET
,POST
,PUT
, andDELETE
; these directly correspond withSELECT/Retreiving DATA
,INSERTING DATA
,UPDATING/UPSERTING DATA
, andDELETING DATA
. A default HTML/ASP.Net webform/PHP/Python or any otherform
action is to "submit" which is a POST action. Because of this the below will all describe doing a POST. Sometimes however with http you might want a different action and would likely want to utilitize.ajax
.My code specifically for you (described in code comments):
Documentation
From jQuery website
$.post
documentation.Example: Send form data using ajax requests
Example: Post a form using ajax and put results in a div
Important Note
Without using OAuth or at minimum HTTPS (TLS/SSL) please don't use this method for secure data (credit card numbers, SSN, anything that is PCI, HIPAA, or login related)
If you add:
You can simply do this:
Note: You could use simple $('FORM').serialize() as suggested in post above, but that will not work for FILE INPUTS... ajaxForm() will.