Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:表单值未正确传递给处理程序脚本_Php - Fatal编程技术网

PHP:表单值未正确传递给处理程序脚本

PHP:表单值未正确传递给处理程序脚本,php,Php,我使用hidden.php将两个值传递给“hidden_handler.php”,以便它们显示在网页上。正在使用的两个变量是hidden.php中的$user和$time。但是,当它们被传递到hidden_handler.php时,这两个值显示为 “$user”和“$time”,而不是它们的指定值,即“Hunt”和实际时间 我试图弄清楚这件事已经有好一阵子了,但找不出是什么原因造成的。提前感谢您的宝贵反馈 下面是两个文件及其代码 hidden.php <?php date_default_

我使用hidden.php将两个值传递给“hidden_handler.php”,以便它们显示在网页上。正在使用的两个变量是hidden.php中的$user和$time。但是,当它们被传递到hidden_handler.php时,这两个值显示为 “$user”和“$time”,而不是它们的指定值,即“Hunt”和实际时间

我试图弄清楚这件事已经有好一阵子了,但找不出是什么原因造成的。提前感谢您的宝贵反馈

下面是两个文件及其代码

hidden.php

<?php
date_default_timezone_set(' UTC ');
$time = date(' H:i, F j');
$user = 'Hunt';
?>

<form action = "hidden_handler.php" method = "POST">
    <fieldset>
        <legend>Send us your comments</legend>
        <textarea rows="5" cols="20" name="comment">
        </textarea>

        <input type="hidden" name="user" value="$user">
        <input type="hidden" name="time" value="$time">


    </fieldset><p><input type="submit" ></p></form>
?>
<?php
if (!empty($_POST['comment'])) {
    $comment = $_POST['comment'];
} else {
    $comment = NULL;
    echo 'You must enter a comment';
}

$time = (!isset($_POST['time']) ) ? NULL : $_POST['time'];

$user = (!isset($_POST['user']) ) ? NULL : $_POST['user'];

if (( $comment != NULL ) &&
        ( $time != NULL ) && ( $user != NULL )) {
    echo "<p>Comment received :\" $comment\" <br>
                                        From $user at $time </p>";
}
?>

将您的意见发送给我们

?>
隐藏的\u handler.php

<?php
date_default_timezone_set(' UTC ');
$time = date(' H:i, F j');
$user = 'Hunt';
?>

<form action = "hidden_handler.php" method = "POST">
    <fieldset>
        <legend>Send us your comments</legend>
        <textarea rows="5" cols="20" name="comment">
        </textarea>

        <input type="hidden" name="user" value="$user">
        <input type="hidden" name="time" value="$time">


    </fieldset><p><input type="submit" ></p></form>
?>
<?php
if (!empty($_POST['comment'])) {
    $comment = $_POST['comment'];
} else {
    $comment = NULL;
    echo 'You must enter a comment';
}

$time = (!isset($_POST['time']) ) ? NULL : $_POST['time'];

$user = (!isset($_POST['user']) ) ? NULL : $_POST['user'];

if (( $comment != NULL ) &&
        ( $time != NULL ) && ( $user != NULL )) {
    echo "<p>Comment received :\" $comment\" <br>
                                        From $user at $time </p>";
}
?>

当hidden.php运行时,我将注释输入为“test” 而$user和$time是硬编码的[$user='Hunt'和$time=date('H:i,F j')]

结果如下所示:

收到的评论:“测试” 在$time从$user开始

当你

<input type="hidden" name="user" value="$user">
<input type="hidden" name="time" value="$time">

您使用的是HTML,而不是PHP,因此这些值是按字面意思取的——变量不会被替换

你需要

<?php
   echo "<input type=\"hidden\" name=\"user\" value=$user>"
   echo "<input type=\"hidden\" name=\"time\" value=$time>"
?>

您应该使用:

<input type="hidden" name="user" value="<?php echo $user;?>">
<input type="hidden" name="time" value="<?php echo $time?>">

提示:使用浏览器查看生成的HTML的源代码。您将看到
value=“$user”
。不,just.srad,请不要在Stack Overflow@Mawg上发帖,请不要要求人们在其他网站上再次发帖;如果人们投票赞成,当前的问题将被迁移。或者,为了更好的可读性,我觉得,只需在HTML中使用PHP块:
,然后随意将其标记为答案,以帮助将来看到它的其他人