Implementing a Submit Form without Refreshing Page Using Jquery
💻 coding

Implementing a Submit Form without Refreshing Page Using Jquery

2 min read 335 words
2 min read
ShareWhatsAppPost on X
  • 1The tutorial demonstrates how to submit a form using jQuery without refreshing the page, utilizing the ajaxForm method.
  • 2It includes form field validation using the jQuery validate plugin to ensure required fields are filled correctly.
  • 3The PHP script processes the form data and inserts it into a database table named 'contact'.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The tutorial demonstrates how to submit a form using jQuery without refreshing the page, utilizing the ajaxForm method."

Implementing a Submit Form without Refreshing Page Using Jquery

This post helps you to submit your form without refreshing page. In this tutorial, I will show you how simple it is to do using jQuery form plugin just five lines of JavaScript code, no need to post data string values via ajax. Explained collaboration with validates plugin for implementing form field validations.

JavaScript Code

$("#form").ajaxForm()- form is the ID name of the FORM tag. While submitting FORM ajaxForm() method posting data to submit.php without refreshing page. Result will show in #preview.

<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
$('document').ready(function()
{
$('#form').ajaxForm( {
target: '#preview', 
success: function() { 
$('#formbox').slideUp('fast'); 
} 
}); 
});
</script>

Contact.html

Simple HTML code. FORM contains three input fields name, email and message.

<div id="preview"> </div>
<div id="formbox"> 
<form id="form" action="submit.php" method="post">
Name
<input type="text" name="name" />
Email
<input type="text" name="email" />
Message
<textarea name="message"></textarea>
<input type="submit" value="Submit">
</form>
</div>

Contacts

Table contains name, email, message and created data etc.

CREATE TABLE `contact` (
`id` int(11) AUTO_INCREMENT PRIMARY KEY,
`name` varchar(255) UNIQUE KEY,
`email` varchar(100),
`message` text UNIQUE KEY,
`created_date` int(11),
)

submit.php

Contails simple PHP code. Inserting values into contacts table.

<?php
include("db.php");
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$name=mysql_real_escape_string($_POST['name']);
$email=mysql_real_escape_string($_POST['email']);
$message=mysql_real_escape_string($_POST['message']);
if(strlen($name)>0 && strlen($email)>0 && strlen($message)>0)
{
$time=time();
mysql_query("INSERT INTO contact (name,email,message,created_date) VALUES('$name','$email','$message','$time')");
echo "<h2>Thank You !</h2>";
}
}
?>

Validate Plugin

Here the collaboration of jQuery validate and form plug-in.

<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
<script type="text/javascript">
$('document').ready(function()
{
$('#form').validate(
{
rules:
{
"name":{
required:true,
maxlength:40
},
"email":{
required:true,
email:true,
maxlength:100
},
"message":{
required:true
}},

messages:
{
"name":{
required:"This field is required"
},
"email":{
required:"This field is required",
email:"Please enter a valid email address"
},
"message":{
required:"This field is required"
}},

submitHandler: function(form){
$(form).ajaxSubmit({
target: '#preview', 
success: function() { 
$('#formbox').slideUp('fast'); 
} 
}); 
}

})
});
</script>

db.php

PHP database configuration file

<?php
$mysql_hostname = "Host name";
$mysql_user = "UserName";
$mysql_password = "Password";
$mysql_database = "Database Name";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 22 August 2019 · 2 min read · 335 words

Part of AskGif Blog · coding

You might also like