我有一个位于contact.php上的html表单,这是代码
<?php
header('Content-Type: text/html; Charset=utf-8');
mb_internal_encoding('UTF-8');
date_default_timezone_set('Europe/Warsaw');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Prove</title>
</head>
<body>
<form action="test.php" method="post" name="post">
<label for="email">E-Mail</label>
<input type="email" id="email" name="email" />
<label for="name">Name</label>
<input id="name" name="name" />
<br>
<label> Message: <br><textarea cols="45" rows="6" name="message"></textarea><br></label>
<p>
<button type="submit" name="post" value="POST COMMENT">POST COMMENT</button>
</p>
<br>
</form>
<h2>Comments:</h2>
</body>
</html>
该表单是从test.php处理的
<?php
date_default_timezone_set('Europe/Warsaw');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<?php
include "contact.php";
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message'])) {
echo "<br><b><h3>*** Please enter all required fields ***</h3></b>";
}
else
{
$name = filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);
$datetime = date('m/d/Y h:i:s a', time());
echo "<br>"
. "<b>From: </b>" . htmlspecialchars( $name )
. "<b> at: </b>" . htmlspecialchars( $datetime)
. "<br><br>" . htmlspecialchars( $message )
. "<br><hr>";
}
?>
</body>
</html>
用户填写并提交表单后,它会显示在test.php而不是contact.php上。
为了安全起见,为什么要同时使用两个代码而不是一个代码...
我的问题是,如何将数据保留在contact.php中?
答案 0 :(得分:0)
<?php
header('Content-Type: text/html; Charset=utf-8');
mb_internal_encoding('UTF-8');
date_default_timezone_set('Europe/Warsaw');
if(isset($_REQUEST['post'])){
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message'])) {
echo "<br><b><h3>*** Please enter all required fields ***</h3></b>";
}
else
{
$name = filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);
$datetime = date('m/d/Y h:i:s a', time());
echo "<br>"
. "<b>From: </b>" . htmlspecialchars( $name )
. "<b> at: </b>" . htmlspecialchars( $datetime)
. "<br><br>" . htmlspecialchars( $message )
. "<br><hr>";
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Prove</title>
</head>
<body>
<form action="" method="post" name="post">
<label for="email">E-Mail</label>
<input type="email" id="email" name="email" />
<label for="name">Name</label>
<input id="name" name="name" />
<br>
<label> Message: <br><textarea cols="45" rows="6" name="message"></textarea><br></label>
<p>
<button type="submit" name="post" value="POST COMMENT">POST COMMENT</button>
</p>
<br>
</form>
<h2>Comments:</h2>
</body>
</html>
我希望这会有所帮助。
答案 1 :(得分:0)
所以,“我”找到了解决方案,人们帮助了我,非常感谢他们!
我的 contact.php 文件;
<?php
include 'Classtest.php';
header('Content-Type: text/html; Charset=utf-8');
mb_internal_encoding('UTF-8');
date_default_timezone_set('Europe/Paris');
error_reporting(-1);
$ProveContackt=new Classtest();
?>
<!DOCTYPE html>
<html lang="en_mx">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form name="form" id="form" method="post" action="index4.php" >
<?php
$ProveContackt->form();
$ProveContackt->PostOk();
?>
</form>
</body>
</html>
我的 test.php 代码;
<?php
header('Content-Type: text/html; Charset=utf-8');
mb_internal_encoding('UTF-8');
date_default_timezone_set('Europe/Paris');
error_reporting(-1);
class Classtest {
private $Name;
private $Email;
private $Message;
function __construct() {
$this->Name="";
$this->Email="";
$this->Message="";
}
function Form() {
echo('<table>');
echo('<label for="name">Name </label>');
echo('<input type="text" name="Name" value="'.$this->Name.'">');
echo('<label for="email"> Email </label>');
echo('<input type="email" value="'.$this->Email.'" name="Email" ');
echo('<tr>');
echo('<td>');
echo('<br>');
echo('<label> Message: <br><textarea cols="45" rows="6" name="Message">'.$this->Message.'</textarea></label>');
echo('<br><br>');
echo('<input type="submit" name="post" value="POST COMMENT" id="comment">');
echo('</td>');
echo('</tr>');
echo('</table>');
}
function PostOk() {
$file = "test.txt";
$this->Name=$_POST["Name"];
$this->Email=$_POST["Email"];
$this->Message=$_POST["Message"];
if(empty($_POST['Name']) || empty($_POST['Email']) || empty($_POST['Message'])) {
echo "<br>" . "<b>" . "<h3>*** Please enter all required fields ***</h3>" . "</b>";
}
else
{
$name = filter_input( INPUT_POST, 'Name', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'Email', FILTER_SANITIZE_STRING);
$message = filter_input(INPUT_POST, 'Message', FILTER_SANITIZE_STRING);
$datetime = date('m/d/Y h:i:s a', time());
$data = array("name" => $name, "email" => $email, "message" => $message, "datetime" => $datetime);
$data = serialize($data);
file_put_contents($file, $data . "\n", FILE_APPEND|LOCK_EX);
}
$messages = file($file);
foreach ($messages as $value) {
$data = unserialize($value);
echo "<br>"
. "<b>From: </b>" . htmlspecialchars( $data["name"])
. "<b> at: </b>" . htmlspecialchars( $data["datetime"])
. "<br><br>" . htmlspecialchars( $data["message"])
. "<br><hr>";
}
}
}
?>
我的屏幕快照现在如何显示it