如何在 PHP 中成功发布数据?未定义变量:check.php 中的 name_result

我现在正在制作联系表格,但出现以下错误。未定义变量:check.php 中的 name_result


我想让网站像:欢迎先生(女士)的名字


HTML(更新)↓


<!DOCTYPE html>

<html>

<head>

</head>

<body>

    <h1>Contact form</h1>

    <form method="POST" action="check.php">

        <div>

        <div>

        <label for="" class="font">Title:</label>

        <label for="" class="font">Name:</label>

        </div>

        <div>

            <div>

            <input type="radio" name="gender" value="Mr.">

            <input type="radio" name="gender" value="Ms.">

       <input type="text" name="name" value="" class="text"><br>

            </div>

            <br>

        <div>

        <input type="submit" value="submit" name="submit">

        </div>

    </form>

</body>

</html>

check.php(更新)↓


<?php

    $gender = $_POST['gender'];  

    $name = $_POST['name']; 

    $name_result = '';

        if(isset($_POST['submit'])){ 

                if(isset($_POST['gender'])){

                    $name_result = 'welcome ' . $gender . $name;

                }else{

                    $name_result = 'Oops! No name typed';

                }

            }

?>

<!DOCTYPE html>

<html>

<head>

</head>

<body>

    <h1>confirm</h1>

    <div>

    <p><?php echo $name_result; ?></p>

    </div>

</body>

</html>

先感谢您。


噜噜哒
浏览 122回答 3
3回答

幕布斯6054654

在 check.php 中,您应该将 $name_result 声明为空字符串。并且为了访问 post 变量或获取数据,它们应该有一个 name 属性。所以,给提交按钮一个特定的名称,如您正在使用的提交。此外,您在$name_result = 'welcome ' . $gender . $name];<!DOCTYPE html>&nbsp; &nbsp; <html>&nbsp; &nbsp; <head>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <h1>Contact form</h1>&nbsp; &nbsp; &nbsp; &nbsp; <form method="POST" action="check.php">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="" class="font">Title:</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="" class="font">Name:</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="gender" value="Mr.">Mr.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="gender" value="Ms.">Ms.&nbsp; &nbsp;        <input type="text" name="name" value="" class="text"><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="submit" name="submit">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>检查.php<?php&nbsp; &nbsp; $gender = $_POST['gender'];&nbsp;&nbsp;&nbsp; &nbsp; $name = $_POST['name'];&nbsp;&nbsp; &nbsp; $name_result='';&nbsp; &nbsp; &nbsp; &nbsp; if(isset($_POST['submit'])){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($_POST['gender'])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name_result = 'welcome ' . $gender .' '. $name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name_result = 'Oops! No name typed';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }?><!DOCTYPE html><html><head></head><body>&nbsp; &nbsp; <h1>confirm</h1>&nbsp; &nbsp; <div>&nbsp; &nbsp; <p><?php echo $name_result; ?></p>&nbsp; &nbsp; </div></body></html>

呼唤远方

在 php 脚本的顶部用空白字符串全局声明 name_result 变量。$name_result&nbsp;=&nbsp;'';现在它作为局部变量起作用,范围有限。

函数式编程

为您的提交按钮命名 name="submit"<input type="submit" value="submit" name="submit">将 $name_result 定义为全局变量<?php&nbsp; &nbsp; &nbsp;$gender = $_POST['gender'];&nbsp;&nbsp;&nbsp; &nbsp; $name = $_POST['name'];&nbsp;&nbsp; &nbsp; $name_result="";&nbsp; &nbsp; &nbsp; &nbsp; if(isset($_POST['submit'])){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(isset($_POST['gender'])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$name_result = 'welcome ' . $gender . $name];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name_result = 'Oops! No name typed';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }?>
打开App,查看更多内容
随时随地看视频慕课网APP