PHP 联系表格和发布


PHP Contact Forms and Posting

我正在使用PHP表单作为我网站的一部分,以便人们取得联系。该表单会将结果发布到另一个网页。目前,对于测试,它只是普通的,但它将受到密码保护。然而,它不一定是安全的,这不是一个商业的东西,只是一个项目。表单代码:

<form action="contactform.php" method="post">
<p><label>Name: <input name="name" placeholder="eg: John Doe"></label></p>
<p><label>Subject: <input name="subject" required placeholder="eg: App Updates?"></label></p>
<p><label>Content: <input name="content" required placeholder="eg: When will the next update to your app be released?"></label></p>
<p><label>Email Address: <input name="email" required placeholder="eg: withheld@xxxx.com" type="email"></label></p>
<p><button input type="submit">Submit form</button></p>
</form>

PHP代码:

    <?php
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$subject  = htmlspecialchars($_POST['subject']);
$content = htmlspecialchars($_POST['content']);
echo  $name, $email, $subject, $content;
$text = "NAME: $name <br>
 EMAIL: $email<br>
 SUBJECT: $subject<br>  
 CONTENT: $content<br><br><br>";
$file = fopen("formresults.html","a+");
fwrite($file, $text);
fclose($file);

?>

我不知道为什么它不只是发布,每当我在浏览器中测试它时,它只会下载 PHP 页面

你的代码应该可以工作了。听起来您正在尝试使用配置不正确(或不存在)的服务器在浏览器中运行PHP文件。您是否正在运行Apache或NGINX等服务器软件?

PHP

是一种解释型语言,要在浏览器中查看用PHP编写的页面,您需要设置服务器软件,该软件将拦截对PHP文件的请求并通过PHP解释器运行它们。如果你在Windows机器上开发,你可以使用WAMP或XAMPP等软件来简化这个过程,并安装PHP,Apache和常用的工具,如数据库引擎。