将表单提交到服务器后,只需将用户重定向回表单页面即可。使用会话或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模式解决。每当您通过帖子获取数据时,就可以在处理完数据后进行重定向。
这也导致了编程约定,在使用过程样式脚本时非常有用。遵循此约定将帮助您避免技术债务和意大利面条代码:
- 执行初始化并加载所有配置文件
- 使用任何用户输入。重定向回同一页或下一页。
- 执行数据库调用和/或业务逻辑。
- 在完成所有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