表单提交后是否显示在PHP/HTML中?

b4lqfgs4  于 2023-02-03  发布在  PHP
关注(0)|答案(1)|浏览(136)

使用下面的代码,我在提交时显示表单响应时遇到了麻烦。我尝试了_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>";
    

}

?>
lmyy7pcs

lmyy7pcs1#

你需要有一个HTML表单,首先定义所有的input标签。表单标签有一个action属性,说明数据将被发送到哪里(你链接的PHP),一个方法(POST,GET等)和一个带有typesubmit的按钮,触发表单数据的发送。当提交按钮被点击时,PHP文件可以显示你的表单响应。
参见以下示例:
表单HTML示例:

<form method="post" action="response.php">
    <label>Password:</label> <input type="text" id="favourite_dog" name="favourite_dog" /> 
    <label>Password:</label> <input type="password" id="password" name="password" />
     more input tags . . .
    <button type="submit">Submit</button>
</form>

显示响应的PHP示例:
response.php

<?php
if (!empty($_POST)){
   $favourite_dog = $_POST['favourite_dog'];
   $password= $_POST['password'];

   echo "<p>Your favourite dog is <b>" . $favourite_dog . "</b>.</p>";       
}

希望这个有用。

相关问题