【问题标题】:Preserving user input using PHP and htmlentities in form?在表单中使用 PHP 和 htmlentities 保留用户输入?
【发布时间】:2013-02-17 04:16:24
【问题描述】:

我正在尝试在表单中保留用户输入,这工作正常,但如果没有输入并且我按下提交按钮,我会收到以下错误:

 Notice: Undefined variable: name in C:\xampp\htdocs\syncfolder\phpsols\contact_01.php on line 54 value="">  

我使用的代码来自《PHP 解决方案》一书:

    <?php 
    $errors = array();
    $missing = array();
    //check if the form has been submitted
    if (isset($_POST['send'])){
    //email processing script
    $to = 'sampleemail1@gmail.com';
    $subject = 'Mailing script';
    //list of expected fields
    $expected = array('name', 'email','comments');
    //set required fields
    $required = array('name','email', 'comments');
    require('processmail.inc.php');
    }
    ?>
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset=utf-8">
    <title>Japan Journey</title>
    <link href="styles/journey.css" rel="stylesheet" type="text/css" media="screen">
    </head>

    <body>
    <div id="header">
    <h1>Japan Journey</h1>
    </div>
    <div id="wrapper">

    <div id="maincontent">
        <h2>Contact Us </h2>

        <?php if ($missing || $errors) { ?>
    <p class="warning">Please fix the item(s) indicated.</p>
        <?php } ?>




      <p>Ut enim ad minim veniam, quis nostrud exercitation consectetur adipisicing elit. Velit esse cillum dolore ullamco laboris nisi in reprehenderit in voluptate. Mollit anim id est laborum. Sunt in culpa duis aute irure dolor excepteur sint occaecat.</p>
        <form id="feedback" method="post" action="">

            <p>
                <label for="name">Name:
                <?php if ($missing && in_array('name', $missing)) { ?>
                <span class="warning">Please enter your name</span>
                <?php } ?>
                </label>
                <input name="name" id="name" type="text" class="formbox"
                    <?php if ($missing || $errors) {
                    echo 'value="' . htmlentities($name, ENT_COMPAT, 'UTF-8') . '"';
                    } ?>>
            </p>

            <p>
                <label for="email">Email:
                <?php if ($missing && in_array('email', $missing)) { ?>
                <span class="warning">Please enter your email address</span>
                <?php } ?>
                </label>
                <input name="email" id="email" type="text" class="formbox"
                    <?php if ($missing || $errors) {
                    echo 'value="' . htmlentities($email, ENT_COMPAT, 'UTF-8') . '"';
                    } ?>>
            </p>

            <p>
                <label for="comments">Comments:
                <?php if ($missing && in_array('comments', $missing)) { ?>
                <span class="warning">Please enter your comments</span>
                <?php } ?>
                 </label>
                <textarea name="comments" id="comments" cols="60" rows="8">
                <?php
                    if ($missing || $errors) {
                    echo htmlentities($comments, ENT_COMPAT, 'UTF-8');
                    } ?>
                </textarea>
            </p>

            <p>
                <input name="send" id="send" type="submit" value="Send message">
            </p>
        </form>
    </div>

</div>
</body>
</html>

并包含文件:

<?php
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
$missing[] = $key;
} elseif (in_array($key, $expected)) {
// otherwise, assign to a variable of the same name as $key
${$key} = $temp;
    }
  }

?>

除了 html-entities 之外,所有脚本都可以正常工作。

【问题讨论】:

    标签: php forms html-entities


    【解决方案1】:

    在处理程序末尾添加else 语句并设置$variable = ("")

    【讨论】:

    • 我仍然无法使用 else 语句,这是我的代码:>
    猜你喜欢
    • 2016-08-26
    • 1970-01-01
    • 2018-04-09
    • 2010-11-24
    • 1970-01-01
    • 2011-06-05
    • 2010-11-28
    • 2015-02-14
    相关资源
    最近更新 更多