我发布时变量没有显示在字符串中?

时间:2015-02-05 07:03:25

标签: php html html5

这是我疯狂的lib表单的HTML页面,我有一个表单来填写疯狂的lib。一旦用户填写表格,他将提交,并应返回完成的疯狂lib。

<head>

    <title>This page sends the data</title>

</head>

    <body>
        <em><h1>Hip-Hop Mad Libs</h1></em>

        <form action="page2.php" method="post">
            <!--1-->
            <label for="clothing">Item of clothing:</label><br>

            <input type="text" name="clothing" id="clothing">


            <br><br>
            <!--2-->
            <label for="adj1">Adjective:</label><br>

            <input type="text" name="adj1" id="adj1">


            <br><br>
            <!--3-->
            <label for="verb1">-ing Verb</label><br>

            <input type="text" name="verb1" id="verb1">


            <br><br>
            <!--4-->
            <label for="verb2">-ed Verb</label><br>

            <input type="text" name="verb2" id="verb2">


            <br><br>
            <!--5-->
            <label for="nickname">Nickname:</label><br>

            <input type="text" name="nickname" id="nickname">

            <br><br>

            <!--6-->
            <label for="race">Race of people:</label><br>

            <input type="text" name="race" id="race">


            <br><br>
            <!--7-->
            <label for="valued">Thing OF Value:</label><br>

            <input type="text" name="valued" id="valued">


            <br><br>
            <!--8-->
            <label for="body">Body Function:</label><br>

            <input type="text" name="body" id="body">


            <br><br>
            <!--9-->
            <label for="verb3">-ed Verb:</label><br>

            <input type="text" name="verb3" id="verb3">


            <br><br>
            <!--10-->
            <label for="woman">Famous Woman:</label><br>

            <input type="text" name="woman" id="woman">


            <br><br>

            <!--11-->
            <label for="occupation">Odd Occupation:</label><br>

            <input type="text" name="occupation" id="occupation">


            <br><br>

            <!--12-->
            <label for="occupation2">Same Occupation:</label><br>

            <input type="text" name="occupation2" id="occupation2">


            <br><br>
            <!--13-->
            <label for="body2">Body Part:</label><br>

            <input type="text" name="body2" id="body2">


            <br><br>
            <!--14-->
            <label for="behavior">Behavior:</label><br>

            <input type="text" name="behavior" id="behavior">


            <br><br>
            <!--15-->
            <label for="nickname2">Mean Nickname:</label><br>

            <input type="text" name="nickname2" id="nickname2">


            <br><br>
            <!--16-->
            <label for="verb4">-ed Verb:</label><br>

            <input type="text" name="verb4" id="verb4">


            <br><br>
            <!--17-->
            <label for="body3">Body Part:</label><br>

            <input type="text" name="body3" id="body3">


            <br><br>



            <button type="submit">Submit</button>

        </form>
        <img src="madlib.jpg" style= "width: 600px;heigth:600px" >

</body>

这是发布变量以创建疯狂lib的PHP,但是当我单击表单上的提交时,它只显示没有变量的段落

<?php
            $clothing = $_GET["clothing"];
            $adj1 = $_GET["adj1"];
            $verb1 = $_GET["verb1"];
            $verb2 = $_GET["verb2"];
            $nickname = $_GET["nickname"];
            $race = $_GET["race"];
            $valued = $_GET["valued"];
            $body = $_GET["body"];
            $verb3 = $_GET["verb3"];
            $woman = $_GET["woman"];
            $occupation = $_GET["occupation"];
            $occupation2 = $_GET["occupation2"];
            $body2 = $_GET["body2"];
            $behavior = $_GET["behavior"];
            $nickname2 = $_GET["nickname2"];
            $verb4 = $_GET["verb4"];
            $body3 = $_GET["body3"];


            $message= "Once upon a time not long ago, where people wore $clothing and
       lived life slow, when were $adj1 and justice stood, and people were $verb1 like they ought ta good. There lived a little boy who was $verb2 by another little boy and this is what he said: Me and you,$nickname we're gonna make  some cash, robbing $race and making a dash.They did the job, $value came with ease. But one couldn't $body. It's like he had a disease. He $verb3 another and another and $woman and her brother;tried to rob a man who a/am $occupation. The $occupation2 grabbed his $body2 he started acting $behavior.He said, Keep still, $nickname2, no need for static, $verb4 him in his $body3 and he gave him a slap.";


        echo $message;              
        ?>

6 个答案:

答案 0 :(得分:0)

您使用POST作为表单的方法并尝试使用$_GET进行访问,您需要使用$_POST访问这些值,因此请更改为

$clothing = $_POST["clothing"];
...

答案 1 :(得分:0)

为了让你编码工作

<form action="page2.php" method="post">

必须更改为:

<form action="page2.php" method="get">

或在 PHP 中使用 $ _ POST 变量:

 $clothing = $_POST["clothing"];
    ....

答案 2 :(得分:0)

在PHP代码中使用$_POST代替$_GET变量。

$clothing = $_POST["clothing"];
$adj1 = $_POST["adj1"];
$verb1 = $_POST["verb1"];
$verb2 = $_POST["verb2"];
...

仅当您的表单未设置方法或设置为获取$_GET时,才使用<form method="get">变量。如果要处理查询字符串,通常使用$_GET变量。这是您在域后的URL中看到的文本。例如http://google.com/search?query_sting=value

如果您不希望在提交后在网址中公开数据,请使用<form method="post">并使用$name=$_POST['name']

访问PHP中的数据

答案 3 :(得分:0)

您正在使用post方法,因此您应该使用代码。

<?php
            $clothing = $_POST["clothing"];
            $adj1 = $_POST["adj1"];
            $verb1 = $_POST["verb1"];
            $verb2 = $_POST["verb2"];
            $nickname = $_POST["nickname"];
            $race = $_POST["race"];
            $valued = $_POST["valued"];
            $body = $_POST["body"];
            $verb3 = $_POST["verb3"];
            $woman = $_POST["woman"];
            $occupation = $_POST["occupation"];
            $occupation2 = $_POST["occupation2"];
            $body2 = $_POST["body2"];
            $behavior = $_POST["behavior"];
            $nickname2 =$_POST["nickname2"];
            $verb4 = $_POST["verb4"];
            $body3 = $_POST["body3"];


            $message= "Once upon a time not long ago, where people wore $clothing and
       lived life slow, when were $adj1 and justice stood, and people were $verb1 like they ought ta good. There lived a little boy who was $verb2 by another little boy and this is what he said: Me and you,$nickname we're gonna make  some cash, robbing $race and making a dash.They did the job, $value came with ease. But one couldn't $body. It's like he had a disease. He $verb3 another and another and $woman and her brother;tried to rob a man who a/am $occupation. The $occupation2 grabbed his $body2 he started acting $behavior.He said, Keep still, $nickname2, no need for static, $verb4 him in his $body3 and he gave him a slap.";


        echo $message;              
        ?>

答案 4 :(得分:0)

如果我必须选择,我可能不会使用$_REQUEST,我会选择$_GET$_POST - 这取决于我的应用应该做什么(i.e. one or the other, but not both):一般来说:

  • 当有人从您的应用程序请求数据时,您应该使用$_GET
  • 当有人向您的应用程序推送(插入或更新;或删除)数据时,您应该使用$_POST

所以,只需更改代码:

<form action="page2.php" method="get">

答案 5 :(得分:0)

  

您正在使用POST方法提交表单并获取值   使用GET方法。你应该为两者获取或两者都是POST。