Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Newsletter Signup form PHP
    primarykey
    data
    text
    <p>I have a simple "newsletter signup" form .php script that I am trying to edit. </p> <p>I want the script to work exactly as it does at the moment but without the "name" input box. </p> <p>I just want the "email" input box, with a message if the email is not valid and a response if the submission was successful. </p> <p>The .php doc is writing to a .txt file on my server.</p> <p>my index.html</p> <pre><code> &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Form&lt;/title&gt; &lt;meta charset="utf-8" /&gt; &lt;link rel="stylesheet" type="text/css" href="css/main.css" /&gt; &lt;script src="prototype.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript" language="javascript"&gt; 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&lt;1 || dotpos-apos&lt;2 || lastpos-dotpos&lt;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'); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Hog&lt;/h1&gt; &lt;form onsubmit="return signup();" method="post" name="subform" id="subform" action=""&gt; &lt;table border="0" cellpadding="5" cellspacing="0"&gt; &lt;tr&gt; &lt;td colspan=2&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 12pt; font-weight:bold;"&gt;Subscribe to our newsletter&lt;/span&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;font size=2 face=Arial&gt;Name:&lt;/font&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="name" id="name" value=""&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;font size=2 face=Arial&gt;Email:&lt;/font&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" id="email" name="email" value=""&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2" align=right&gt; &lt;input type="submit" id="submit" name="submit" value="Sign up"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2" align="right" style="height:20px"&gt;&amp;nbsp; &lt;div id="myResponse" style="DISPLAY:none;"&gt;&lt;/div&gt; &lt;div id="loading" style="display:none;"&gt;&lt;img src="wait.gif" alt=""&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>my php file</p> <pre><code> &lt;?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; ?&gt; </code></pre> <p>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.</p> <p>I would appreciate any help, Thanks in advance,</p> <p>Sam</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload