【问题标题】:redirect to html with custom messagenafter php form form submission在 php 表单提交后使用自定义消息重定向到 html
【发布时间】:2020-10-29 02:53:50
【问题描述】:

我是第一次使用 Php 并尝试与我们联系表格。我已经在 html 代码中使用内联 php 制作了表单,并将文件保存为 .php 并且它可以工作。现在看起来真的很难看,而且我的 nodejs 服务器不提供我的 php 文件,所以我试图将 php 代码取出到一个单独的文件中。

所以现在我有两个文件 -

  1. index.html 格式
  2. mail.php

index.html

 <h2>PHP FORM </h2>
 <form action="mail.php" method="POST">
    <label for="user">Name</label>
     <input type="text" id="user" name="users-name"><br>
     <label for="email">Email</label>
      <input type="email" id="email" name="users-email"><br>
      <label for="cars">Category</label>
       <select name="users-cat" id="cars">
           <option value="volvo">Volvo</option>
           <option value="saab">Saab</option>
           <option value="mercedes">Mercedes</option>
           <option value="audi">Audi</option>
       </select><br>
     <label for="subject">Subject</label>
     <input type="text" id="subject" name="users-subject"><br>
     <label for="msg">Message</label>
      <input type="text" id="msg" name="users-message"><br>
      <input type="submit">
 </form>

ma​​il.php是这样的-

    <?php 
        if (isset($_POST['users-name']) ||
                isset($_POST['users-email']) ||
                isset($_POST['users-subject'])) { 
            $admin_email = "username@gmail.com"; 
            $name = stripslashes($_POST['users-name']);
            $subject = stripslashes($_POST['users-subject']);
            $email = stripslashes($_POST['users-email']);
            $category = stripslashes($_POST['users-cat']);
            $message = stripslashes($_POST['users-message']); 
            //send email
            $maiL_status = mail($admin_email, "$subject", "Contact Email: " . $email . "\n" . "Name: " . $name . "\n" . "Category: " . $category . "\n" . "Message: " . $message);
     
            //Email response
            echo '<h4 class="text-center">THANK YOU.</h4>';
            echo '<h6 class="text-center">Your message is now being processed.</h6>';
            echo '<h6 class="text-center">We will get back to you promptly.</h6>';
            header("Location: index.html?message=ThankYou");
        }else{
            echo '<h4 class="text-center">Problem</h4>';
        }
    ?>



所以我的问题是 - 我如何使用 mail.php 提交我的表单并重定向回带有消息的 html 页面。我开放使用 Jquery 或 javascript。我想将回显响应发送回 html 页面并在 html 页面中显示消息。

我浏览了很多 stackvoerflow 帖子,但我无法弄清楚

  1. How do I make a redirect in PHP?
  2. PHP Pass Data with Redirect

编辑:

我已经使用 javascript 解决了这个问题 -

let msg = window.location.href;
        msg = msg.split('=')[1];
        console.log(msg);
        if(msg!=null){
            $('#form_id').hide();
            $('#repos').show();
            $('#repos').text(msg);
        } 

【问题讨论】:

  • 您应该查看“AJAX”——您在后台使用 javascript 将表单发布到 PHP 脚本,并在 javascript 中获取结果,然后使用 javascript 显示结果,以及在他们看到您的结果消息后,可能会将用户导航到新页面。这是一种更现代的方式。另一种选择是使用index.php?showWelcomeMessage=1 之类的查询参数重定向用户,并在存在该查询参数时有条件地显示您的消息。

标签: javascript php html forms


【解决方案1】:

我写了一个你可以参考的程序结构。部分代码省略。

<?php
// Process anything about sending mail.
if (isset($_POST['users-name'])) {
    // Some codes are omitted
    $msg = "";
    $hasError = true;
    if ($hasError) {
        $msg = "xxx";
    }
    $redirectUrl = "/xxx/contact.php?msg={msg}";
    header("Location: {$redirectUrl}");
}
?>
<html>
<script type="text/javascript">
// Process anything about show msg.
<?php
if (isset($_GET['msg']) && $_GET['msg']) {
    $msg = $_GET['msg'];
    // output html for init js var msg
    echo "var msg = \"{$msg}\";";
}
?>

// js code: alert the msg if msg is not empty.
// Some codes are omitted
// other way: show the msg by html element.

</script>

<form action="/xxx/contact.php" method="POST">
<!-- form content -->
</form>
</html>

【讨论】:

  • 所以如果我从你的代码中理解正确 - (1) php 代码在 contact.php 文件中,html 代码在 index.html 文件中?对? (2) 我试图这样做,但在 html 文件中出现错误 - “Uncaught SyntaxError: Unexpected token '
  • 这可行,但结构上不是最干净的。这就像 PHP 101 类型的代码,它会工作,但如果你的项目很大或者你通过编写这段代码获得报酬,这绝对不是你想要的方式。如果这对您来说在概念上是新的,我强烈建议您寻找一些 PHP 教程。
  • @ChrisBaker 我同意你的看法。谢谢。
  • @Greyfrog 如果你不使用php框架,建议你阅读文章,code.tutsplus.com/tutorials/…stackoverflow.com/questions/5004233/…
  • @Xingchao 链接已损坏,但确实如此。我试图在我的 html 文件中获取消息,而不是在 php 文件中。因此,我尝试了可以​​使用窗口位置从 url 获取响应的技巧。无论如何,这是一个小项目,只需要这个表格。我通常使用 nodej、javascript 和 firebase 做一些事情,但只是为了这个小任务,我不想编写任何 firebase 函数。基本上我现在试图避免任何服务器端编码。
【解决方案2】:

你已经解决了。但是,我认为,可以通过将此 javascript 放在 php 文件中的结束 php 标记之外,即最后

<script language="javascript">location.href="index.html?msg="<?php echo $msg ?>";</script>

【讨论】:

  • 我想在 html 文件中接收消息,而不是在 php 文件中。我放在那里的代码在 html 文件中。如上所述,将 php 代码放入 .html 文件中不会被执行。所以简而言之,我试图在我的 html 文件中获取消息,而不是在 php 文件中,但感谢您的输入。
猜你喜欢
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 2016-09-22
  • 2013-09-10
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多