Submit form without page reloading

It  is often required to have  Newsletter subscription form on the home page of your website.

For that i have written a small piece of code to demonstrate Newsletter subscription without page reload

The HTML part

[code]

<form id="signup" name="signup">
<input name="email" id="email" type="text" value="Email ID"  onblur="clearText(this)" onfocus="clearText(this)"  />
<input name="form_submit" id="form_submit" type="submit" value="Subscribe" />
</form>

[/code]

The Javascript part

[code]

<script language="javascript" type="text/javascript">

$(function() {
$("#form_submit").click(function()
{

var email = $("#email").val();

var dataString = ’email=’+ email;

if(email==”)
{
alert(‘Please provide email address’);
}
else
{
$.ajax({
type: "POST",
url: "newsletter-subscribe.php",
data: dataString,
cache: false,
success: function(html){

$("#email").val(”);

$("#container").append(html);
}
});
}return false;
});
});

</script>

[/code]

The PHP part

[code]

include(‘config/settings.php’);

$link = mysql_connect(_DB_SERVER_, _DB_USER_, _DB_PASSWD_);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}

$db_selected = mysql_select_db(_DB_NAME_, $link);
if (!$db_selected) {
die (‘Can’t use : ‘ . mysql_error());
}

$query = sprintf("INSERT into ps_newsletter (email, newsletter_date_add, ip_registration_newsletter) VALUES (‘%s’, ‘%s’, ‘%s’)",
mysql_real_escape_string($_REQUEST[’email’]),
date(‘y-m-d h:i:s’),
$_SERVER[‘REMOTE_ADDR’]);

// Perform Query
$result = mysql_query($query);

mysql_close($link);

echo ‘Thank you for subscribing to our Newsletter’;

[/code]

Get Free Email Updates!

Signup now and receive free offers, discounts & coupon codes

I will never give away, trade or sell your email address. You can unsubscribe at any time.

Leave a Reply

Your email address will not be published. Required fields are marked *

CommentLuv badge