0

我有一个简单的“时事通讯注册”表单 .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."\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 的部分,但表单无法就错误的电子邮件或成功提交提供反馈。

我会很感激任何帮助,在此先感谢,

山姆

4

1 回答 1

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;
?>
于 2013-08-28T20:58:15.090 回答