简单的通讯注册表单PHP

时间:2013-08-28 20:49:07

标签: php forms newsletter

我正在尝试编辑一个简单的“简报注册”表单.php脚本。

我希望脚本能够像现在一样工作,但没有“名称”输入框。

我只想要“电子邮件”输入框,如果电子邮件无效则会显示一条消息,如果提交成功则会显示回复。

.php doc正在写入我服务器上的.txt文件。

我的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">&nbsp;
    <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>

我的php文件

    <?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;
    ?>

我已经尝试删除文件的部分名称$ name但是该表单无法提供有关错误电子邮件或成功提交的反馈。

我将不胜感激, 提前谢谢,

萨姆

1 个答案:

答案 0 :(得分:1)

删除此:

<tr>
<td><font size=2 face=Arial>Name:</font></td>
<td><input type="text" name="name" id="name"  value=""></td>
</tr>

和此:

var name    = trim($F("name"));

if (name=="") {
  // stuff
}

您也应该能够删除JavaScript中的其他“名称”引用。

改变这样的事情:

$("name", "email").invoke('clear');    

为:

$("email").invoke('clear');    

php文件将是:

<?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 the email and add it to the file
$email  = trim($_REQUEST["email"]);
$pfileName  = "myemails.txt";
$MyFile     = fopen($pfileName, "a");   
$nline=$email."\r\n";
fwrite($MyFile, $nline);
fclose($MyFile);
echo 'Thanks for Subscribing';  // Change the message if you want.
die;
?>