简单的时事通讯注册形式PHP



我有一个简单的"时事通讯注册"表单.php脚本,我正在尝试编辑它。

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

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

php文档正在写入我的服务器上的.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."rn";     
    // USE THIS TO SAVE ONLY THE EMAIL: $nline=$email."rn";
    fwrite($MyFile, $nline);
    fclose($MyFile);
    echo 'Thanks for Subscribing';  // Change the message if you want.
    die;
    ?>

我曾尝试删除文件中写有$name的部分,但该表单无法对糟糕的电子邮件或成功提交进行反馈。

如果有任何帮助,我将不胜感激,提前感谢

Sam

删除此:

<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."rn";
fwrite($MyFile, $nline);
fclose($MyFile);
echo 'Thanks for Subscribing';  // Change the message if you want.
die;
?>

最新更新