输出HTML用户输入*使用HTML代码*

时间:2016-10-05 01:02:47

标签: php html

我正在尝试输出用户输入网站的信息。这是我的页面第一部分的代码,显示填写的信息(此代码有效):

<form action = "comment_process.php" method = "post">
    Name: 
    <input type = "text" name = "name"/> <br>

    Do you like this page?
    <input type = "radio" name = "answer" value = "Yes"> Yes 
    <input type = "radio" name = "answer" value = "No"> No <br>

    Comment:
    <textarea rows="3" cols="50" name = "comment"/> </textarea> <br>

    Rating 
    <select name = "rating"> <br>
        <option value = "0"> 1 </option>
        <option value = "1"> 2 </option>
        <option value = "2"> 3 </option>
        <option value = "3"> 4 </option>
        <option value = "4"> 5 </option> 

    <input type = "submit" value = "go"/>
</form>

但是,处理输入数据的页面不起作用。我的代码如下:

<?php

    $v = $_POST['firstname'];
    $x = $_POST['comment'];
    $y = $_POST['answer'];
    $z = $_POST['rating'];

    echo "Hello $v! <br>"

    if ($y == "Yes") {

        echo "I am happy that you like the page :)"

    }
    else {

        echo "I am sorry that you do not like the page :("

    }

    echo "Thanks for your comment [$x]";
    echo "You rated our page as Rating $z";

?>

我正在尝试根据用户输入的数据输出屏幕右侧小文本框中的内容:

enter image description here

我想知道是否有人可以帮助我!

2 个答案:

答案 0 :(得分:0)

检查您的输入命名。它是HTML中的name和php中的firstname。 还要确保在php中的每一行后使用分号,否则会出错。 echo "Hello $v! <br>";

不是错误,但可能令人困惑,您在下拉列表中的选项具有与使用期望值<option value = "0"> 1 </option>不同的值。这意味着当他们评分为1时,他们会被告知他们的评分为0.此外,这意味着您将在sorry选项中包含等级3或更低。

编辑:使用您编辑的代码,您的PHP中的第一行echo将导致错误,因为没有分号。

答案 1 :(得分:0)

你有一些错误

这对我有用:

<form action = "comment_process.php" method = "post">
    Name: 
    <input type = "text" name = "name"/> <br>

    Do you like this page?
    <input type = "radio" name = "answer" value = "Yes"> Yes 
    <input type = "radio" name = "answer" value = "No"> No <br>

    Comment:
    <textarea rows="3" cols="50" name = "comment"/> </textarea> <br>

    Rating 
    <select name = "rating"> <br>
        <option value = "0"> 1 </option>
        <option value = "1"> 2 </option>
        <option value = "2"> 3 </option>
        <option value = "3"> 4 </option>
        <option value = "4"> 5 </option> 

    <input type = "submit" value = "go"/>
</form>


<?php

    $v = $_POST['firstname'];
    $x = $_POST['comment'];
    $y = $_POST['answer'];
    $z = $_POST['rating'];

    echo "Hello $v";

    if ($y == "Yes") {

        echo "I am happy that you like the page :)";

    }
    elseif ($y == "No") {

        echo "I am sorry that you do not like the page :(";

    }

    echo "Thanks for your comment [$x]";
    echo "You rated our page as Rating $z";

?>