lundi 2 mars 2015

Multiple Ajax/PHP email forms

I have a newsletter signup form on my website.


I am using the contact.php file



<?php
/*

Author: Andrew Walsh
Date: 30/05/2006
Codewalkers_Username: Andrew


This script is a basic contact form which uses AJAX to pass the information to php, thus making the page appear to work without any refreshing or page loading time.

*/

$to = "hello@interzonestudio.com"; //This is the email address you want to send the email to
$subject_prefix = ""; //Use this if you want to have a prefix before the subject

if(!isset($_GET['action']))




{
die("You must not access this page directly!"); //Just to stop people from visiting contact.php normally
}

/* Now lets trim up the input before sending it */


$subject = "Newsletter Sign Up"; //The senders subject
$message = trim($_GET['email']); //The senders subject
$email = trim($_GET['email']); //The senders email address

mail($to,$subject,$message,"From: ".$email.""); //a very simple send

echo 'contactarea|Thank you. We promise you won’t regret it.'; //now lets update the "contactarea" div on the contact.html page. The contactarea| tell's the javascript which div to update.
?>


This is my Javascript



<script language="javascript">
function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}
var http = createRequestObject();
function sendemail() {
var email = document.contactform.email.value;
document.contactform.send.disabled=true;
http.open('get', '/build/tca/contact.php?email='+email+'&action=send');
http.onreadystatechange = handleResponse;
http.send(null);

setTimeout(function(){
$(document).find("#thanks").fadeOut();
},3000);

}
function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();
if(response.indexOf('|' != -1)) {
update = response.split('|');
document.getElementById(update[0]).innerHTML = update[1];
}
}
}
</script>

<script>
jQuery(document).ready(function(){
setTimeout(function(){
$("div#thanks").fadeOut("slow", function () {
$("div#thanks").remove();
});

}, 2000);
});
</script>


and this is my HTML



<div id="contactarea">
Newsletter<br/>
<form name="contactform" id="contactform">
<input class ="email" type="text" name="email" id="inputbox" value="E-Mail"
onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
<input type="submit" value="Sign Up" name="send" onclick="sendemail(); return false; " class="signup" >
</form>
</div>


I would like to add another contact form on the same page but I am having conflicts.


I have tried duplicating the javascript and ameending it but I can't get it to work. Or would I be better off amending it?


If anyone can offer some insight it would be greatly appreciated.


Aucun commentaire:

Enregistrer un commentaire