为什么我的联系方式会引起浏览器警报

问题描述 投票:-1回答:2

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

确认表格重新提交

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

继续按钮和取消按钮

问题编号2:提交表格后,我在我的邮箱中收到邮件,但是我是通过[email protected]收到的,所以有什么问题吗?

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

<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 = "[email protected]"; // 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>```
php html forms gmail contact-form
2个回答
0
投票

只要将表单提交到服务器,只需将用户重定向回表单页面即可。使用会话或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 = "[email protected]"; // 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>

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


0
投票

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

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

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

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

<?php 
session_start();

if(isset($_POST['submit'])){
    $to = "[email protected]"; // 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,$subject,$message,$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>
© www.soinside.com 2019 - 2024. All rights reserved.