Simple Newsletter Signup form PHP -
i have simple "newsletter signup" form .php script trying edit.
i want script work @ moment without "name" input box.
i want "email" input box, message if email not valid , response if submission successful.
the .php doc writing .txt file on 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 show , style error message $("myresponse").style.color="red"; // there more ways using css classes example. $("myresponse").innerhtml="please enter name"; // make error function. $("name").clear(); $("name").focus(); // clear field , put cursor inside return false; /* can repeat above else if block if had other fields in form, last name, address etc. example see how name handled , repeat */ } else if (email=="" || !goodemail || bademail) { $("myresponse").style.display="inline"; //3 lines show , style error message $("myresponse").style.color="red"; $("myresponse").innerhtml="please enter valid email"; $("email").focus(); return false; } else { //you may want change url in 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 buttom , message $("loading").show(); // show loading image. return false; } } function showresponse(req) { $("loading").hide(); $("myresponse").innerhtml=req.responsetext; //writes "thank you" message comes optin.php , 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 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 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: shoul use own function here purify requested parameters , protect against injections. example: $email = clean_this($_request["email"]); */ $email = trim($_request["email"]); $name = trim($_request["name"]); /* saving name , email txt file create myemails.txt file , put in same directory. */ $email = trim($_request["email"]); $name = trim($_request["name"]); $pfilename = "myemails.txt"; $myfile = fopen($pfilename, "a"); $nline=$email.','.$name."\r\n"; // use save email: $nline=$email."\r\n"; fwrite($myfile, $nline); fclose($myfile); echo 'thanks subscribing'; // change message if want. die; ?>
i've tried deleting parts of file $name form loses ability give feedback on bad email or successful submission.
i appreciate help, in advance,
sam
delete this:
<tr> <td><font size=2 face=arial>name:</font></td> <td><input type="text" name="name" id="name" value=""></td> </tr>
and this:
var name = trim($f("name")); if (name=="") { // stuff }
you should able remove other 'name' references in javascript well.
change things this:
$("name", "email").invoke('clear');
to:
$("email").invoke('clear');
the php file this:
<?php header ("expires: mon, 26 jul 1997 05:00:00 gmt"); header("cache-control: no-store, no-cache, must-revalidate"); header("pragma: no-cache"); // fetch email , add file $email = trim($_request["email"]); $pfilename = "myemails.txt"; $myfile = fopen($pfilename, "a"); $nline=$email."\r\n"; fwrite($myfile, $nline); fclose($myfile); echo 'thanks subscribing'; // change message if want. die; ?>
Comments
Post a Comment