使用HTML在PHP中创建一个粘性表单

时间:2016-02-16 10:19:24

标签: php html

我正在尝试在PHP中生成一个粘性表单,但它无法正常工作。我的代码是:

<html>
    <h1>Customer Feedback</h1>
    <p1>Please tell us what you think</p1><br><br>
    <form method='POST' action='<?php echo $_SERVER['PHP_SELF'];?>' >
        <p1>Your name:</p1><br>
        <input type="text" name="fullname" value="<?php echo $fullname; ?>"><br><br>
        <p1>Your email address:</p1><br>
        <input type="text" name="email" value="php echo $email;"><br><br>
        <p1>Your feedback:</p1><br>
        <textarea rows="5"  cols="50" name="feedback"><?php echo $text;?>  </textarea><br><br>
        <input type="submit" Value="Send Feedback"><br><br>
        <?php
            error_reporting(E_ALL);


            $name = $_POST['fullname'];
            $email = $_POST['email'];
            $feed = $_POST['feedback'];

            if (empty($name)) 
            {
                echo "Please enter your name.";
            }
            if (empty($email)) 
            {
                echo "Please enter your email and feedback.";
            }
            if (empty($feed))
            {
                echo "Please enter feedback.";
            }
            if (!empty($name) && !empty($email) && !empty($feed))
            {
                echo "You have inserted the correct data";
            }
        ?>
    </form>
</html>

当我运行代码时,电子邮件文本框已经满了php echo $email;,并且当我按下“发送反馈”按钮时,它不希望文本框保持已满。

修改 我将标签围绕电子邮件行的输入值,当我按下“提交反馈”按钮时,表单会进行清空。

2 个答案:

答案 0 :(得分:2)

 <input type="text" name="email" value="<?php echo $email; ?>"><br><br>

此输入值上没有php标记

答案 1 :(得分:1)

如果您想在提交表单后显示数据,则需要此选项:

<?php
$fullname = "";
$email = "";
$text = "";
if(isset($_POST['fullname']) && $_POST['fullname'] != "") {
    $fullname = $_POST['fullname'];
}
if(isset($_POST['email']) && $_POST['email'] != "") {
    $email = $_POST['email'];
}
if(isset($_POST['feedback']) && $_POST['feedback'] != "") {
    $text= $_POST['feedback'];
}
?>
<html>
    <h1>Customer Feedback</h1>
    <p1>Please tell us what you think</p1><br><br>
    <form method='POST' action='<?php echo $_SERVER['PHP_SELF'];?>' >
        <p1>Your name:</p1><br>
        <input type="text" name="fullname" value="<?php echo $fullname; ?>"><br><br>
        <p1>Your email address:</p1><br>
        <input type="text" name="email" value="<?php echo $email;?>"><br><br>
        <p1>Your feedback:</p1><br>
        <textarea rows="5"  cols="50" name="feedback"><?php echo $text;?>  </textarea><br><br>
        <input type="submit" Value="Send Feedback"><br><br>
        <?php
            error_reporting(E_ALL);
            //your rest of PHP code
        ?>
    </form>
</html>