使用下面的代码,我在提交时显示表单响应时遇到了麻烦。我尝试了_GET和_POST的混合使用,但我不知道该用什么,什么时候用,因为我对PHP还比较陌生。代码如何在提交时显示表单响应呢?
<?php {
$fDogErr = $lDogErr = "";
// only show the information if the button named "subButton" has been pressed
if (!isset($_POST['submit'])) {
// set the variable with the submitted value
if (empty($fDog = $_POST['favourite dog'])) {
$fDogErr = "Need favourite dog";
} else {
$fDog = $_POST['favourite dog'];
}
if (empty ($lDog = $_POST['least favourite dog'])) {
$lDogErr = "Need least favourite dog";
} else {
$lDog = $_POST['least favourite dog'];
}
if (empty($password = $_POST['pawsword'])) {
$password = "";
} else {
$password = $_POST['password'];
}
if (empty($dogcac = $_POST['dogcac'])) {
$dogcac = "";
} else {
$dogcac = $_POST['dogcac'];
}
$secretdoggo = $_POST['secretdoggo'];
}
// display the user inputs to the screen
echo "<p>Your favourite dog is <b>" . $fDog . "</b>.</p>";
echo "<p>Your least favourite dog is <b>" . $lDog . "</b>.</p>";
echo "<p>Your pawsword is <b>" . $password . "</b>.</p>";
echo "<p> Did you know? <b>" . $secretdoggo . "</b>.</p>";
}
?>
1条答案
按热度按时间lmyy7pcs1#
你需要有一个HTML表单,首先定义所有的input标签。表单标签有一个action属性,说明数据将被发送到哪里(你链接的PHP),一个方法(POST,GET等)和一个带有typesubmit的按钮,触发表单数据的发送。当提交按钮被点击时,PHP文件可以显示你的表单响应。
参见以下示例:
表单HTML示例:
显示响应的PHP示例:
response.php
希望这个有用。