php表单的问题

时间:2011-04-23 16:39:16

标签: php email forms

我被朋友要求修改他的联系表格的代码。我不知道php所以这证明非常困难。我知道流利的HTML和CSS。

表单需要将姓名和电子邮件详细信息发送到他的电子邮件并重定向到感谢页面。重定向工作正常,所有错误等,但它不会发送任何邮件!这是代码:

<?php 
if(isset($_POST['txtName'])){

$sql = "SELECT * FROM tbl_ezine WHERE email = '$_POST[txtEmail]'";
$result = dbQuery($sql);
$numRows = dbNumRows($result);
if($numRows > 0){
echo "<script>window.location = 'http://www.thankyoupageishere.com';</script>";
}else{

if($_POST['txtEmail'] != "" && $_POST['txtEmail'] != "Email Address"){

$searchfor = "@";
$searchfor2 = ".";

$find1 = strpos($_POST['txtEmail'],$searchfor); // CHECK IF EMAIL CONTAINS @ SYMBOL
$find2 = strpos($_POST['txtEmail'],$searchfor2); // CHECK IF EMAIL CONTAINS . SYMBOL


if($find1 === false || $find2 === false) {
 // string needle NOT found in haystack
$errorStr = "Invalid email format.";
}
else {
 // string needle found in haystack
$sql = "INSERT INTO tbl_ezine (username, email, event) VALUES ('$_POST[txtName]', '$_POST[txtEmail]', 'hop')";
$result = dbQuery($sql);    
echo "<script>window.location = 'http://www.thankyoupageishere.com';</script>";
}


}else{// FIELDS ARE BLANK OR UNCHANGED
    $errorStr = "Please enter your name and email to continue.";
}
}
}

?>

<div id="content" class="hop" style="min-height:220px;">

<div id="body" style="min-height:200px;padding-right:340px;">
    <h2>Sign up to download your photos</h2>
    <p>Complete the registration form</p>
<?php 
if($errorStr != ""){echo "<p style=\"color:#F00;\">" . $errorStr . "</p>";}
?>
<form name="hop" class="competition" method="post" action="">
        <input type="text" value="Name" class="contact-name" name="txtName" /> <input type="text" value="Email Address" class="contact-email" name="txtEmail" /> <input type="submit" value="SUBMIT&gt;" class="submit" /></form>
</div>

有人能告诉我在哪里输入电子邮件详情!

感谢。

4 个答案:

答案 0 :(得分:2)

你的邮件功能在哪里?

您应该使用 php's mail function 发送电子邮件。

htmlspecialchars()会使输入安全,因为您要避免 xss sql injections 。 不要像你发布的代码那样开发一些不安全的东西。

$name=htmlspecialchars($_POST['txtName'],ENT_QUOTES); 
$email=htmlspecialchars($_POST['txtEame'],ENT_QUOTES);
mail("yourfriendsemail@gmail.com","(subject) A new mail","name: $name , email: $email");

答案 1 :(得分:0)

它不会发送任何电子邮件,因为你没有在任何地方使用过php mail()命令。

您可以在http://php.net/manual/en/function.mail.php

找到对该功能和示例的完整参考

答案 2 :(得分:0)

$sql = "INSERT INTO tbl_ezine (username, email, event) VALUES ('$_POST[txtName]', '$_POST[txtEmail]', 'hop')";
$result = dbQuery($sql);    
/*send mail here*/
mail($_POST[txtEmail], 'Your email subject', 'your email content here', "From: youremail@yourdomain.com\r\n");
echo "<script>window.location = 'http://www.thankyoupageishere.com';</script>";

但是,在您开始使用此代码之前,我建议您使用一些现成的脚本。例如,$_POST[txtEmail]应为$_POST['txtEmail']。您没有过滤用户输入。不安全的用户输入是一个安全漏洞。恕我直言,如果你没有兴趣学习PHP或编程聘请一些自由职业者10美元来做脚本而不是自己动手并陷入困境。

答案 3 :(得分:0)

您可以尝试:(将2个变量替换为您想要的内容)。我还修复了$ _POST数组的一些错误用法(你忘记了txtEmail中的单引号)

    <?php 
if(isset($_POST['txtName'])){

$sql = "SELECT * FROM tbl_ezine WHERE email = '". $_POST['txtEmail'] . "'";
$result = dbQuery($sql);
$numRows = dbNumRows($result);
if($numRows > 0){
//************ EDIT THESE ****************
$subject = "Your email subject";
$message = "Your email message";
//****************************************
mail($_POST['txtEmail'], $subject, $message);


echo "<script>window.location = 'http://www.thankyoupageishere.com';</script>";
}else{

if($_POST['txtEmail'] != "" && $_POST['txtEmail'] != "Email Address"){

$searchfor = "@";
$searchfor2 = ".";

$find1 = strpos($_POST['txtEmail'],$searchfor); // CHECK IF EMAIL CONTAINS @ SYMBOL
$find2 = strpos($_POST['txtEmail'],$searchfor2); // CHECK IF EMAIL CONTAINS . SYMBOL


if($find1 === false || $find2 === false) {
 // string needle NOT found in haystack
$errorStr = "Invalid email format.";
}
else {
 // string needle found in haystack
$sql = "INSERT INTO tbl_ezine (username, email, event) VALUES ('". $_POST['txtName']."', '".$_POST['txtEmail'].'", 'hop')";
$result = dbQuery($sql);    
echo "<script>window.location = 'http://www.thankyoupageishere.com';</script>";
}


}else{// FIELDS ARE BLANK OR UNCHANGED
    $errorStr = "Please enter your name and email to continue.";
}
}
}

?>

<div id="content" class="hop" style="min-height:220px;">

<div id="body" style="min-height:200px;padding-right:340px;">
    <h2>Sign up to download your photos</h2>
    <p>Complete the registration form</p>
<?php 
if($errorStr != ""){echo "<p style=\"color:#F00;\">" . $errorStr . "</p>";}
?>
<form name="hop" class="competition" method="post" action="">
        <input type="text" value="Name" class="contact-name" name="txtName" /> <input type="text" value="Email Address" class="contact-email" name="txtEmail" /> <input type="submit" value="SUBMIT&gt;" class="submit" /></form>
</div>