【问题标题】:PHP success message on same page / styling同一页面/样式上的 PHP 成功消息
【发布时间】:2020-11-28 21:53:52
【问题描述】:

我是 PHP 新手,但找到了一个 php 联系表单的教程。除了在新页面上显示“您的电子邮件已成功发送”文本外,该表单运行良好。

有没有办法让文本显示在同一页面上?

我也不确定如何设置信息样式?

谢谢

<?php


$to = 'zoeharrisondesign@gmail.com';
$subject = 'New Message Recieved!';
$from = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$name = $_POST['name'];

//check honeypot
    if( !empty( $honeypot ) ) {
        echo 'There was a problem';
    }
 
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 
// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
 
// Compose a simple HTML email message
$message = "$message \r\n |
                From $name \r\n |               
                Tel: $phone";


 
// Sending email

if(mail($to, $subject, $message, $headers)){
    echo 'Your mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}

?>

HTML

<form role="form" action="action.php" method="post" class="contact-form">   
            
<div class="form-row"><input type="text" id="name" name="name" placeholder="name" required="required"></div>
                        
<div class="form-row"><input type="email" id="email" name="email" placeholder="email" required="required">  </div>
                        
<div class="form-row"><input type="tel" id="phone" name="phone" placeholder="phone"></div>
                        
<div class="form-row"><textarea id="message" name="message" placeholder="message" required="required" style="height:200px"></textarea></div>
                        
<div class="form-row" style="display:none;">
<input type="hidden" name="url" placeholder="URL"></div>
                        
<div class="form-row"><input class="submit" type="submit" value="Send"></div>   
</form>

【问题讨论】:

  • 可以添加重定向到其他页面的代码吗?我假设您的表单具有指向另一个页面的操作是吗?
  • 我不这么认为,我的 html 只是一个标准形式,我没有理由知道为什么它会把我带到 PHP 以外的另一个页面
  • 你能添加form标签吗,你的表单标签中有action属性吗? PHP 不会随意将您转到另一个页面,而无需在您的表单标记中使用标头重定向或操作属性进行编码。您的代码中的某些内容正在将您重定向到另一个页面...
  • &lt;form action="NEW_PAGE_LINK"&gt; 更改为&lt;form action=""&gt;
  • $honeypot 定义在哪里?

标签: php html css forms


【解决方案1】:

使用 sessions 的简单方法, 你需要像这样创建一个会话 php 文件:

SESSION.PHP

ini_set('session.use_only_cookies', 1);
session_set_cookie_params(0,'/','localhost',false,false); // IF YOU ARE HOSTING IT IN LOCAL 
session_start();
session_regenerate_id(); // REGENERATE SESSIONE FOR SECURITY ISSUES

邮寄 PHP 文件

// add this on top of your mail.php file 
include 'session.php'; // just insert the path of the file
// AND CHANGE THIS
if(mail($to, $subject, $message, $headers)){ 
   $_SESSION['error'] = 'Your mail has been sent successfully.'; // setting your session and value
   header("Location: index.php"); // path of the file where you want to show the text
   exit();
} else{
   // same thing 
}

INDEX.PHP:(只需将其添加到您要显示文本的位置,例如:)

 if(isset($_SESSION['error'])) {
   echo '<h1>'.$_SESSION['error'].'</h1>';
   unset($_SESSION['error']); // to display it just once and then delete it 
 }

重要提示:您还需要在 index.php 文件的顶部添加 &lt;?php include 's-session.php'; ?&gt;

【讨论】:

    【解决方案2】:
    In top of php code start the session`session_start();`
    

    插入这段代码

    if(mail($to, $subject, $message, $headers)){   
    $_SESSION['message'] = "Submitted Successfully";
    }

    将此代码粘贴到要显示消息的 html 中

    <?php if (isset($_SESSION['message'])): ?>
          <div class="msg text-success" id="success" >
          <?php 
            echo $_SESSION['message']; 
            unset($_SESSION['message']);
          ?>
          </div>
        <?php endif ?>

    【讨论】:

      猜你喜欢
      • 2013-11-30
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      相关资源
      最近更新 更多