PHP多了一条评论

时间:2014-09-17 08:13:58

标签: php forms cookies

有没有办法显示更多评论而不是覆盖另一条评论? 我正在考虑使用带索引的变量,因此它不会覆盖另一个变量。 但我不确定是否可能以及将指数放在何处。

PHP

 <?php
    $name = ""; //Variable aanmaken met waarde
    $comment = "";  //Variable aanmaken met waarde
    $gender = "";  //Variable aanmaken met waarde

    if($_SERVER["REQUEST_METHOD"] == "POST") // Controleer of het formulier verzonden is
    { 
        if(isset($_POST["name"], $_POST["comment"], $_POST["gender"] )) // Controleer of benodigde velden wel ingevuld zijn 
        {
            $name = $_POST["name"];
            $comment [] = $_POST["comment"];
            $gender = $_POST["gender"];

            $_POST["name"] = "asds";
        }  
        else
        {
            $name = "Niks ingevuld";
            $comment = "Niks ingevuld";
            $gender = "Niks ingevuld";
        }
    }

HTML:

评论公式

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   Name: <input type="text" name="name" value="<?php echo $name;?>">
   <br><br>
   Comment:  <textarea name[]="comment" rows="5" cols="40"><?php echo $comment;?><?php echo $comment;?></textarea>
   <br><br>
   Gender:
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?>  value="female">Female
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?>  value="male">Male
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>

显示评论的PHP:

    <?php
    echo "<h2>Comments:</h2>";
    echo $name;
    echo "<br>";
foreach($data as $comment){

echo $data.'</br>';
    echo "<br>";
    echo $gender;

3 个答案:

答案 0 :(得分:1)

<textarea name[]="comment" rows="5" cols="40">

比在isset条件之后运行foreach循环

foreach($data as $comment){

echo $data.'</br>';
}

答案 1 :(得分:0)

在这里,你应该为你工作:

<h2>Comment formulier</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   Name: <input type="text" name="name" value="<?php echo $name;?>">
   <br><br>
   Comment: <textarea name="comment[]" rows="5" cols="40"></textarea>
   <br />
   Comment2: <textarea name="comment[]" rows="5" cols="40"></textarea>
   <br />
   <?php
       if (isset($_POST["comment"]))
           $comment = $_POST["comment"];
       foreach ($comment as $key => $value)
           echo 'comment '.$key.' '.$value.'<br />';
   ?>
   </textarea>
   <br><br>
   Gender:
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?>  value="female">Female
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?>  value="male">Male
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>

答案 2 :(得分:0)

是的,你可以使用数组显示更多评论,只需将你的帖子值放在数组中并打印它们