Simple Newsletter Signup form PHP
php script that I am trying to edit.
I want the script to work exactly as it does at the moment but without the
"name" input box.
I just want the "email" input box, with a message if the email is not
valid and a response if the submission was successful.
The .php doc is writing to a .txt file on my server.
my index.html
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="css/main.css" />
<script src="prototype.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
function trim(str){str = str.replace(/^\s*$/, '');return str;}
function signup() {
var email = trim($F("email"));
var name = trim($F("name"));
//EMAIL VALIDATION
var goodEmail =
email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|
(\.info)|(\.sex)|(\.biz)|(\.aero)|(\.coop)|(\.museum)|(\.name)|(\.pro)|(\.arpa)|(\.asia)|(\.cat)|
(\.int)|(\.jobs)|(\.tel)|(\.travel)|(\.xxx)|(\..{2,2}))$)\b/gi);
apos=email.indexOf("@");dotpos =
email.lastIndexOf(".");lastpos=email.length-1;
var badEmail = (apos<1 || dotpos-apos<2 || lastpos-dotpos<2);
if (name=="") {
$("myResponse").style.display="inline"; //3 lines to
show and style the error message
$("myResponse").style.color="red"; // there are
more ways to do it using css classes for example.
$("myResponse").innerHTML="Please enter your name"; // you could
also make an error function.
$("name").clear(); $("name").focus();
// clear the field and put cursor inside
return false;
/* YOU CAN REPEAT THE ABOVE ELSE IF BLOCK IF YOU HAD OTHER FIELDS IN
YOUR FORM,
LIKE LAST NAME, ADDRESS ETC. AS AN EXAMPLE SEE HOW THE NAME IS
HANDLED AND REPEAT
*/
}
else if (email=="" || !goodEmail || badEmail) {
$("myResponse").style.display="inline"; //3 lines to show
and style the error message
$("myResponse").style.color="red";
$("myResponse").innerHTML="Please enter a valid email";
$("email").focus();
return false;
}
else {
//YOU MAY WANT TO CHANGE THE URL IN THE LINE BELOW
var url = "optIn.php";
var params = $("subform").serialize();
new Ajax.Request(url, {onComplete:showResponse,
onException:showException, onFailure:showException,
asynchronous:true, method:'post', evalScripts:false,
postBody:params});
$("submit", "myResponse").invoke('hide'); // Hide the buttom and the
message
$("loading").show(); // show the loading
image.
return false;
}
}
function showResponse(req) {
$("loading").hide();
$("myResponse").innerHTML=req.responseText; //Writes the "Thank you"
message that comes from optIn.php and styles it.
$("myResponse").style.display="inline";
$("myResponse").style.color="blue";
$("submit").show();
$("name", "email").invoke('clear');
}
function showException(req) {
$("myResponse").innerHTML=req.responseText;
alert("An error occured while talking to the server. Please try again.");
$("loading", "myResponse").invoke('hide');
$("submit").show();
$("name", "email").invoke('clear');
}
</script>
</head>
<body>
<h1>Hog</h1>
<form onsubmit="return signup();" method="post" name="subform"
id="subform" action="">
<table border="0" cellpadding="5" cellspacing="0">
<tr>
<td colspan=2><span style="FONT-FAMILY: Arial; FONT-SIZE: 12pt;
font-weight:bold;">Subscribe to our newsletter</span>
</td>
</tr>
<tr>
<td><font size=2 face=Arial>Name:</font></td>
<td><input type="text" name="name" id="name" value=""></td>
</tr>
<tr>
<td><font size=2 face=Arial>Email:</font></td>
<td><input type="text" id="email" name="email" value=""></td>
</tr>
<tr>
<td colspan="2" align=right>
<input type="submit" id="submit" name="submit" value="Sign up">
</td>
</tr>
<tr>
<td colspan="2" align="right" style="height:20px">
<div id="myResponse" style="DISPLAY:none;"></div>
<div id="loading" style="display:none;"><img src="wait.gif" alt=""></div>
</td>
</tr>
</table>
</form>
</body>
</html>
my php file
<?php
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
/* ***************
Attention: you shoul use your own function here to
purify the requested parameters and protect against injections.
Example: $email = clean_this($_REQUEST["email"]);
*/
$email = trim($_REQUEST["email"]);
$name = trim($_REQUEST["name"]);
/*
SAVING NAME AND EMAIL TO A TXT FILE
Create a myemails.txt file and put it in the same directory.
*/
$email = trim($_REQUEST["email"]);
$name = trim($_REQUEST["name"]);
$pfileName = "myemails.txt";
$MyFile = fopen($pfileName, "a");
$nline=$email.','.$name."\r\n";
// USE THIS TO SAVE ONLY THE EMAIL: $nline=$email."\r\n";
fwrite($MyFile, $nline);
fclose($MyFile);
echo 'Thanks for Subscribing'; // Change the message if you want.
die;
?>
I've tried deleting parts of the file that say $name but the form loses
its ability to give feedback on a bad email or successful submission.
I would appreciate any help, Thanks in advance,
Sam
No comments:
Post a Comment