用HTML和PHP制作了一个表单,但无法找出问题所在


Made a form in HTML and PHP but unable to figure out what is wrong

我制作了一个表格,通过电子邮件将数据发送到电子邮件ID。但在填写表格并提交后,浏览器会说:

服务器错误。网站在检索http://localhost/process.php时遇到错误。

这是代码:

form1.html

<html>
<head>
<title>Form</title>
</head>
<body>
<form method = "post" action = "process.php">
Enter your name <input type = "text" name = "namee" id = "namee" value = "enter your name" />
Enter your phone number <input type = "text" name = "phone" id = "phone" />
<br>
<input type  = "submit" value = "post it!" />
</form>

</body>
</html>

process.php

<?php
$person_name = $_POST["namee"];
$person_number = $_POST["phone"];
$to = "example234671_1@gmail.com";
$subject = "form filled up";
$body = $person_name. "<br>" $person_number . "<br>" . $person_name ;

mail($to, $subject, $body);
echo "Thank you!" ;
?>

错误是什么??

$body = $person_name. "<br>" $person_number . "<br>" . $person_name ;

这一行错了,您缺少一个连接符。

$body = $person_name. "<br>" . $person_number . "<br>" . $person_name ;

为了正确格式化电子邮件,必须将使用text/html的标头用作<br>的换行符。

否则,您的电子邮件正文将显示为John Doe<br>213-555-0123<br>etc.

另外,正如其他人已经指出的,中缺少一个连接

$body = $person_name. "<br>" $person_number . "<br>" . $person_name ;

应读作:

$body = $person_name. "<br>" . $person_number . "<br>" . $person_name;

使用添加的标头重写:

<?php
$person_name = $_POST["namee"];
$person_number = $_POST["phone"];
$to = "example@gmail.com";
$subject = "form filled up";
$from="email@example.com";
$body = $person_name. "<br>" . $person_number . "<br>" . $person_name;
$header = 'MIME-Version: 1.0' . "'n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
$header .= "From: ". $from . " $from " . "'r'n";
mail($to, $subject, $body, $header);
echo "Thank you!";
?>

这里有一个语法错误。

$body = $person_name. "<br>" $person_number . "<br>" . $person_name ;

"<br>"后面需要一个连接点,如下所示:

$body = $person_name. "<br>" . $person_number . "<br>" . $person_name ;

在开发过程中,您需要启用显示php错误的功能。看看PHP运行时配置。

就像前面说的那样,你错过了一个连接点,但我也看到了另一个错误。

"<br>" 

是HTML,如果没有正确的内容类型,您将无法在电子邮件中显示HTML。

看看http://php.net/manual/en/function.mail.php了解更多信息和示例。

或使用''n作为换行符。