【问题标题】:Echoing not leaving the page回声不离开页面
【发布时间】:2014-01-08 22:17:45
【问题描述】:

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

这些是我的表单的主要问题。请您指导我一些有用的tuts,或者只是更正我的代码。任何帮助将不胜感激!

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" );

?>

【问题讨论】:

    标签: php email subscription


    【解决方案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>
    

    【讨论】:

    • 感谢 Moax 的努力,但代码似乎不起作用。它呼应了两个空框和提交按钮)
    • 警告:无法修改标头信息 - 标头已由第 58 行 /home/dastiche/public_html/subscribe.php 中的(输出开始于 /home/dastiche/public_html/subscribe.php:53)发送- 这是什么意思?
    【解决方案2】:

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

    【讨论】:

    • 谢谢,Moax,我已经添加了你的建议。但主要问题仍然存在。
    【解决方案3】:

    这应该有效:

    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>
    

    【讨论】:

    • 它与我的订阅 div 相呼应,仅此而已。
    【解决方案4】:

    它可能正在回显,但它发生得如此之快,以至于您在重定向之前可能看不到它。尝试在 mail() 函数和 header() 函数之前放置一个exit;,我想你会看到它。

    您需要做的是在回显和重定向之间以某种方式暂停。您可以通过多种方式使用 PHP 或 javascript 来执行此操作。

    [编辑] 更新如何使用 echo 保持页面加载

           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" );
    

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

    【讨论】:

    • 谢谢史蒂夫,但我会稍微改一下我问的内容。我不希望用户被重定向。只需在“提交”按钮成功/失败下方回显,仅此而已。你能帮我解决这个问题吗?或者你知道一步一步的,这样我就可以自己去做。
    • 然后删除重定向代码并将您的 php 和表单放在同一页面上,这将为您节省很多。/
    • Moax,我可能要求的太多了,但你能在代码中给我看一下吗?我不像程序员那么好,我是平面设计师:-)
    猜你喜欢
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    相关资源
    最近更新 更多