CodeCharge Studio
search Register Login  

Web Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> Tips & Solutions

 Javascript / PHP Mail Contact Form without form submit

Print topic Send  topic

Author Message
MichaelMcDonald

Posts: 594
Posted: 07/11/2014, 2:37 AM

This is a seamless no refresh/no submit/no reload contact form that uses AJAX to validate your email, grab the variables, put them into URL parameters and POST them to your .php file to get them and runs the sendmail script in the background.


The Form your .html file....

<form role="form" id="contactform" method="post">
<div class="someclass">
<input name="name" type="text" class="form-control" id="name" placeholder="Your Name" maxlength="40" autocomplete="off">
</div>
<div class="someclass">
<input name="email" type="text" class="form-control" id="email" placeholder="Your Email" maxlength="80" autocomplete="off">
</div>
<div class="someclass">
<textarea name="message" class="form-control" id="message" placeholder="Your Message..." autocomplete="off"></textarea>
</div>
<div class="someclass"><button type="button" class="btn btn-primary" id="contactformButton_Send" onclick="sendemail();">Send</button></div>
<div class="someclass" type="text" class="form-control" id="error"></div>
</form>



Javascript

<script>

function sendemail(){



var w = document.forms["contactform"]["message"].value;
if (w == null || w == "") {
document.getElementById('error').innerHTML = "Your Message is required";
}




var x = document.forms["contactform"]["email"].value;
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if (atpos< 1 || dotpos<atpos+2 || dotpos+2>=x.length) {
var email = 1;
document.getElementById('error').innerHTML = "A valid email address is required";
}


var y = document.forms["contactform"]["name"].value;
if (y == null || y == "") {
document.getElementById('error').innerHTML = "Your Name is required";
}




if((y != null && y != "") && (w != null && w != "") && (email != 1)){
document.getElementById('error').innerHTML = "";


var a = new XMLHttpRequest();
a.open("POST","sendemail.php?name=" + y + "&email=" + x + "&message=" + w);
a.onreadystatechange = function() {
if( a.readyState == 4) {
if( a.status == 200) {


document.getElementById('error').innerHTML = "Thank you. Your email was sent successfully.";
}
else alert("HTTP error "+a.status+" "+a.statusText);
}
}
a.send();
}
}
</script>



sendemail.php - separate file.

<?php

$name = $_GET ['name'];

$email = $_GET['email'];

$message = $_GET ['message'];

$message = "From: " . $name . " Message: ". $message;


$address = "destination@yourserver.com";


mail( $address, "Subject Title",$message, "From: $email" );

?>
_________________
Central Coast, NSW, Australia.

Use the EXETA Online Business Valuation Tool to
value your business.

www.exeta.com.au
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright 2003-2004 by UltraApps.com  and YesSoftware, Inc.