回应不离开页面

时间:2013-12-20 05:43:11

标签: php email subscription

我在我的网站上有一个简单的订阅时事通讯表格。我希望你帮助我使它更加用户友好。 我的表格不能做什么: - 它不能回应“成功”“不成功”不离开页面(它显示新页面中的状态,这是令人沮丧的) - 无法验证正确和不正确的电子邮件(如果你放入一些垃圾,我会收到它)

这些是我表格的主要问题。请你指点一些有用的东西,或者只是纠正我的代码。任何帮助将不胜感激!

HTML:

<div class="subscribe">
          <form method="POST" action="subscribe.php">
<p><input type="text" name="Name" size="24" maxlength="40" style="font-family:'Times New Roman', Times, serif" value="Ваше имя"  onfocus="if (this.value == 'Ваше имя') {this.value = '';}" onblur="if (this.value == '') {this.value ='Ваше имя';}"></p>
<p><input type="text" name="Email" size="24" maxlength="40" style="font-family:'Times New Roman', Times, serif" value="Ваш email"  onfocus="if (this.value == 'Ваш email') {this.value = '';}" onblur="if (this.value == '') {this.value ='Ваш email';}"></p>
<p><input type="image" value="submit" name="Submit" src="images/subscribe.png"></p>
</form>

        </div>

PHP:

<?php

$recipient = "order@dastiche.kz";
$subject = "Subscriber";
$name = $_POST['Name'];
$email = $_POST['Email'];
$location = "index.html";
$sender = $recipient;
$body .= "Name: ".$_REQUEST['Name']." \n";
$body .= "Email: ".$_REQUEST['Email']." \n";


if (($name != "") or ($email != ""))
// Если существуют проверяем... 
{
   if ((strlen($name) >= 2) and (strlen($name) <= 25))
   {
   $name = stripslashes($name);
   $name = html_entity_decode($name);
   $name = strip_tags($name);
   }
   else
   {
   echo " something is wrong with name field ";
   echo "<center><input name='back' type='button' value='get back'
   onclick= 'javascript:history.back()'></center>";
   }

   if (eregi("^[._a-zA-Z0-9-]+@[.a-zA-Z0-9-]+.[a-z]{2,6}$", $email))
   {
   $email = stripslashes($email);
   $email = htmlspecialchars($email);
   $email = strip_tags($email);
   }
   else
   {
   echo "There are some mistakes in the \"E-mail\" field";
   echo "<center><input name='back' type='button' value='try again'
   onclick= 'javascript:history.back()'></center>";
   }

}
// Если не существуют выводим сообщение... 
else
{
echo "Please, fill in blanks";
echo "<center><input name='back' type='button' value='try again'
onclick= 'javascript:history.back()'></center>";
}

   if (($name) and ($email))
{
   echo "thanks for subscription!";
}


mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
header( "Location: $location" );

?>

4 个答案:

答案 0 :(得分:1)

这里有验证的代码你可以自己添加电子邮件功能。此外,我没有测试这个,但希望它会工作..

<?php
$errors = array();
if(isset($_POST['submit'])){
    $uname = $_POST['uname'];
    $email = $_POST['email'];
    if($uname == ''){
        $errors['uname'] = "Username is required.";
    }
    if($email == ''){
        $errors['email'] = "Email is required!";
    }
    if(empty($errors)){
        echo "Subscription Successfull";
    }else{
        echo "Subscription failed";
    }
}
  ?>
  <div id="error">
<ul>
<?php
    foreach($errors as $error){
        echo "<li>".$error."</li>";
    }
?>
  </ul>
  </div>
  <form name="subscribe" action="" method="post">
  <input type="text" name="uname" value="<?php echo isset($_POST['uname'])? $_POST['uname']:'';?>"/>
<input type="email" name="email" value="<?php echo isset($_POST['email'])?$_POST['email']:'';?>"/>
  <input type="submit" name="submit" value="Subscribe"/>
  </form>

答案 1 :(得分:0)

它可能正在回声,但它发生得太快,你可能在重定向之前没有看到它。尝试在mail()函数之后和header()函数之前放置exit;,我想你会看到它。

你需要做的是在回声和重定向之间以某种方式暂停。使用PHP或javascript可以通过多种方式实现此目的。

[编辑]关于如何保持页面加载回声的更新

       if (($name) and ($email))
{
   echo "thanks for subscription!";
}


mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
// comment out the redirection and you should send the email, 
// reload the page and have the "thanks" echoed.
// header( "Location: $location" );

但仍有很大的改进空间。如果我们为你做这件事,你就不会学到太多东西,但这应该让你开始

答案 2 :(得分:0)

有一些改进代码的建议.. 1.使用输入字段的类型,它将以电子邮件而不是文本形式发送电子邮件。 2.由于你只想在用户提供姓名和电子邮件时才想执行表单子代码,因此你必须使用AND语句而不是OR。

答案 3 :(得分:0)

这应该有效:

subscribe.php

<?php
if(isset($_POST))
{
$recipient = "order@dastiche.kz";
$subject = "Subscriber";
$name = $_POST['Name'];
$email = $_POST['Email'];
$location = "index.html";
$sender = $recipient;
$body .= "Name: ".$_RE``QUEST['Name']." \n";
$body .= "Email: ".$_REQUEST['Email']." \n";


if (($name != "") or ($email != ""))
// Если существуют проверяем... 
{
if ((strlen($name) >= 2) and (strlen($name) <= 25))
{
$name = stripslashes($name);
$name = html_entity_decode($name);
$name = strip_tags($name);
}
else
{
echo " something is wrong with name field ";
echo "<center><input name='back' type='button' value='get back'
onclick= 'javascript:history.back()'></center>";
}

if (eregi("^[._a-zA-Z0-9-]+@[.a-zA-Z0-9-]+.[a-z]{2,6}$", $email))
{
$email = stripslashes($email);
$email = htmlspecialchars($email);
$email = strip_tags($email);
}
else
{
echo "There are some mistakes in the \"E-mail\" field";
echo "<center><input name='back' type='button' value='try again'
onclick= 'javascript:history.back()'></center>";
}

}
// Если не существуют выводим сообщение... 
else
{
echo "Please, fill in blanks";
echo "<center><input name='back' type='button' value='try again'
onclick= 'javascript:history.back()'></center>";
}

if (($name) and ($email))
{
echo "thanks for subscription!";
}


mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
header( "Location: $location" );
}
?>

 <div class="subscribe">
      <form method="POST" action="">
 <p><input type="text" name="Name" size="24" maxlength="40" style="font-family:'Times New Roman', Times, serif" value="Ваше имя"  onfocus="if (this.value == 'Ваше имя') {this.value = '';}" onblur="if (this.value == '') {this.value ='Ваше имя';}"></p>
 <p><input type="text" name="Email" size="24" maxlength="40" style="font-family:'Times New Roman', Times, serif" value="Ваш email"  onfocus="if (this.value == 'Ваш email') {this.value = '';}" onblur="if (this.value == '') {this.value ='Ваш email';}"></p>
 <p><input type="image" value="submit" name="Submit" src="images/subscribe.png"> </p>
 </form>

    </div>