HTML 个需要保存数据的表单 PHP
HTML forms that needs to save data with PHP
我正在尝试制作一个表单,其字段将从 url 自动填充。此外,如果用户单击 "Save me a seat",它应该重定向到一个位置,发送电子邮件并保存 cookie。如果用户点击 "Not interested, thank you",它应该重定向 him/her 到另一个位置,发送电子邮件并保存 cookie。
现在,我已经 2 年没有使用 PHP 了,我忘记了很多东西。
代码如下:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if(isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$title = $_POST['positiontitle'];
$company = $_POST['companyname'];
$all = array($firstname, $lastname, $title, $company);
if($firstname !=''&& $lastname !=''&& $title !=''&& $company !='')
{
// To redirect form on a particular page
header("Location:http://www.example.com");
}
else{
?><span><?php echo "Please fill in all fields!";?></span> <?php
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="noindex, nofollow" />
</head>
<body>
<form class="conf-form" method="POST">
<legend>Please input your information:</legend><br />
First name:<br>
<input class="fst-name" type="text" name="firstname" value="First Name">
<br><br />
Last name:<br>
<input class="lst-name" type="text" name="lastname" value="Last Name">
<br /><br />
Position title:<br />
<input class"pos-title" type="text" name="positiontitle" value="Position Title"/>
<br /> <br />
Company name:<br />
<input class="cmp-name" type="text" name="companyname" value="Company Name"/>
<br /><br />
<input id="save-me" type="submit" value="Save me a seat" name="submit"> <input id="not-int" type="submit" value="Not interested, thank you" name="notsubmit">
<br /><br />
If you have any comments/questions please let us know here:<br />
<textarea rows="4" cols="50"></textarea>
</form>
</body>
</html>
它没有重定向到我想要的位置。首先,我该如何解决这个问题?
发送内容后无法进行重定向。在您的情况下,您在 运行 header()
:
之前已经有 HTML 内容
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
这应该触发错误,但您可能关闭了错误报告。要打开错误报告,您需要将其包含在每次运行的主 PHP 文件的顶部:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
这将节省您的开发时间。
要解决您的确切问题,您需要将 PHP 包含文件移动到 HTML 内容上方。
您的 PHP 代码没有得到 运行。 form
的默认方法是 get
,在您的代码中,您尝试从永远不会发送的 HTTP POST
中获取某些内容。
要解决这个问题,您应该更改表单 method
属性,或者将 PHP 更改为使用 $_GET
。我建议将 method='post'
添加到您的表单标签中。有关更多信息,请查看 PHP.net、$_POST and $_GET.
上的这两页
对于你在问题中描述的内容
I'm trying to make a form whose fields will be automatically populated from the url.
也许 get
对您来说更有趣。
旁边,为什么会有这个代码:
If you have any comments/questions please let us know here:<br />
<textarea rows="4" cols="50"></textarea>
如果你反正不使用它。如果您打算使用它,那么您应该使用 post
作为您的表单方法,因为 url(因此 get
)对其大小有限制。无论哪种方式,您都可能希望向该文本区域添加 name
属性。
回顾一下
- 将您的
form method
更改为 post
,或将您的 PHP 代码更改为 $_GET
- 将
name
属性添加到您的 textarea
以便您的代码可以访问它而不是无用
我正在尝试制作一个表单,其字段将从 url 自动填充。此外,如果用户单击 "Save me a seat",它应该重定向到一个位置,发送电子邮件并保存 cookie。如果用户点击 "Not interested, thank you",它应该重定向 him/her 到另一个位置,发送电子邮件并保存 cookie。
现在,我已经 2 年没有使用 PHP 了,我忘记了很多东西。
代码如下:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if(isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$title = $_POST['positiontitle'];
$company = $_POST['companyname'];
$all = array($firstname, $lastname, $title, $company);
if($firstname !=''&& $lastname !=''&& $title !=''&& $company !='')
{
// To redirect form on a particular page
header("Location:http://www.example.com");
}
else{
?><span><?php echo "Please fill in all fields!";?></span> <?php
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="noindex, nofollow" />
</head>
<body>
<form class="conf-form" method="POST">
<legend>Please input your information:</legend><br />
First name:<br>
<input class="fst-name" type="text" name="firstname" value="First Name">
<br><br />
Last name:<br>
<input class="lst-name" type="text" name="lastname" value="Last Name">
<br /><br />
Position title:<br />
<input class"pos-title" type="text" name="positiontitle" value="Position Title"/>
<br /> <br />
Company name:<br />
<input class="cmp-name" type="text" name="companyname" value="Company Name"/>
<br /><br />
<input id="save-me" type="submit" value="Save me a seat" name="submit"> <input id="not-int" type="submit" value="Not interested, thank you" name="notsubmit">
<br /><br />
If you have any comments/questions please let us know here:<br />
<textarea rows="4" cols="50"></textarea>
</form>
</body>
</html>
它没有重定向到我想要的位置。首先,我该如何解决这个问题?
发送内容后无法进行重定向。在您的情况下,您在 运行 header()
:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
这应该触发错误,但您可能关闭了错误报告。要打开错误报告,您需要将其包含在每次运行的主 PHP 文件的顶部:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
这将节省您的开发时间。
要解决您的确切问题,您需要将 PHP 包含文件移动到 HTML 内容上方。
您的 PHP 代码没有得到 运行。 form
的默认方法是 get
,在您的代码中,您尝试从永远不会发送的 HTTP POST
中获取某些内容。
要解决这个问题,您应该更改表单 method
属性,或者将 PHP 更改为使用 $_GET
。我建议将 method='post'
添加到您的表单标签中。有关更多信息,请查看 PHP.net、$_POST and $_GET.
对于你在问题中描述的内容
I'm trying to make a form whose fields will be automatically populated from the url.
也许 get
对您来说更有趣。
旁边,为什么会有这个代码:
If you have any comments/questions please let us know here:<br />
<textarea rows="4" cols="50"></textarea>
如果你反正不使用它。如果您打算使用它,那么您应该使用 post
作为您的表单方法,因为 url(因此 get
)对其大小有限制。无论哪种方式,您都可能希望向该文本区域添加 name
属性。
回顾一下
- 将您的
form method
更改为post
,或将您的 PHP 代码更改为$_GET
- 将
name
属性添加到您的textarea
以便您的代码可以访问它而不是无用