为什么我的联系表单会引起浏览器警报

实际上我的php联络表单存在一些问题。 问题1:如果用户重新加载了浏览器,则提交联系表格后,他们会收到来自浏览器警报的消息

  

确认表格重新提交

     
    

您要查找的页面使用了您输入的信息。返回该页面可能会导致您重复执行任何操作。您要继续吗?

         
      

继续按钮和取消按钮

    
  

问题2:提交表格后,我在我的邮箱中收到邮件,但是我是通过 hostinguser@webserver.com 收到的,那有什么问题吗?

这是我的所有代码,如果我做错了或类似的事情,请让我纠正。谢谢

<head>
<title>Form submission</title>
</head>
<body>

<form method="post">
First Name: <input type="text" name="client_name"><br>
Last Name: <input type="text" name="client_phone"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php 
if(isset($_POST['submit'])){
    $to = "mailmenow23@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $client_name = $_POST['client_name'];
    $client_phone = $_POST['client_phone'];
    $subject = "Form Submission by"." " $client_name ;
    $message = "Client Name : ". $client_name ."\n\n". "Client Phone : " . $client_phone ."\n\n"." wrote the following:" . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    echo "Mail Sent. Thank you " . $client_name . ",we will contact you shortly.";

    }
?>

</body>
</html>```
jjjjcccchhhh 回答:为什么我的联系表单会引起浏览器警报

将表单提交到服务器后,只需将用户重定向回表单页面即可。使用会话或cookie来存储成功/失败消息,以便将其输出到表单页面上。

<?php session_start(); ?>
<head>
<title>Form submission</title>
</head>
<body>

<?php 
if( isset( $_SESSSION['status' ] ) ){
   echo '<p>'. $_SESSSION['status' ] .'</p>';
   unset( $_SESSION['status'] ); // unsetting the status index
}
?>  

<form method="post">
First Name: <input type="text" name="client_name"><br>
Last Name: <input type="text" name="client_phone"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php 
if(isset($_POST['submit'])){
    $to = "mailmenow23@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $client_name = $_POST['client_name'];
    $client_phone = $_POST['client_phone'];
    $subject = "Form Submission by"." " $client_name ;
    $message = "Client Name : ". $client_name ."\n\n". "Client Phone : " . $client_phone ."\n\n"." wrote the following:" . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);


     // storing the message in the session
     $_SESSION['status'] = "Mail Sent. Thank you " . $client_name . ",we will contact you shortly.";

    // redirecting the user to the form page
    header('Location: form-page-name.php');


    }
?>

</body>
</html>

此外,不要忘记在提交表单时对用户输入内容进行清理和验证。

,

这可以通过Post-Redirect-Get模式解决。每当您通过帖子获取数据时,就可以在处理完数据后进行重定向。

这也导致了编程约定,在使用过程样式脚本时非常有用。遵循此约定将帮助您避免技术债务和意大利面条代码:

  1. 执行初始化并加载所有配置文件
  2. 使用任何用户输入。重定向回同一页或下一页。
  3. 执行数据库调用和/或业务逻辑。
  4. 在完成所有php工作之前,请勿输出任何内容。尽可能将逻辑和表示分开。仅在这一点上,您才准备输出html。此时,仅将php用于循环和变量替换

使用这些原则,您的脚本将如下所示:

<?php 
session_start();

if(isset($_POST['submit'])){
    $to = "mailmenow23@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $client_name = $_POST['client_name'];
    $client_phone = $_POST['client_phone'];
    $subject = "Form Submission by"." " $client_name ;
    $message = "Client Name : ". $client_name ."\n\n". "Client Phone : " . $client_phone ."\n\n"." wrote the following:" . "\n\n" . $_POST['message'];


    // warning! This is open to injection! 
    $headers = "From:" . $from;
    mail($to,$headers);

    $_SESSION['message'] = "Mail Sent. Thank you " . $client_name . ",we will contact you shortly.";

    // cannot have any output before this!
    // see https://www.php.net/manual/en/function.header.php

    header('Location: ' . htmlentities($_SERVER['PHP_SELF '));
    exit; // prevent script from continuing

}
// do any other logic stuff,I.e.,database calls,calculations,formatting functions,etc.

// now that all php stuff is done,it’s time to present the view
?>
<html>
    <head>
      <title>Form submission</title>
    </head>
    <body>
        <?php if(array_key_exists('blah',$_SESSION) && !empty($_SESSION['blah'])):?>
        <div class= "alert-success"><?= $_SESSION["message"] ?></div>
        <?php endforeach;?>
        <form method="post">
            First Name: <input type="text" name="client_name"><br>
            Last Name: <input type="text" name="client_phone"><br>
            Email: <input type="text" name="email"><br>
            Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
            <input type="submit" name="submit" value="Submit">
        </form>
    </body>
</html>
本文链接:https://www.f2er.com/3162405.html

大家都在问