0

我不确定为什么这不起作用,它没有给我任何错误,但它不会显示结果。我只想显示 3 个文本字段的结果。我想做的下一步是将数据输入数据库,然后显示它,但我想接下来会出现。谢谢你。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$Pword = $_POST["Pword"];

if (!isset($_POST['submit'])) { // if page is not

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>

<form method = "post" action = "<?php echo $PHP_SELF;?>">

First name<input name="Fname" type="text" maxlength="12"
/>
<br />
<br />

Last name<input name="Lname" type="text" maxlength="12"
/>
<br />
<br />

<!--Address <input name="register_address" type="text" maxlength="12"
/>
<br />
 <br />-->
<!--<select name="state" value="State"></select>-->


Password<input name="Pword" type="text" maxlength="12"
/>
<br />
<br />

<!--Re-type Password<input name="register_password_confirm" type="text" maxlength="12"
/>
<br />
<br />-->

<input name="submit" type="button" value="Submit" />



</form>

<?
} else {
echo "Hello, ".$Fname." ".$Lname.".<br />";
echo "Your Password is, " .$Pword.".<br />";
}
?>




</body>
</html>
4

4 回答 4

3

我看到的三个问题

1 - 您的按钮不是提交按钮

改变:

<input name="submit" type="button" value="Submit" />

<input name="submit" type="submit" value="Submit" />

2 - 注册全局变量可能被关闭

改变:

<?php echo $PHP_SELF;?>

<?php echo $_SERVER['PHP_SELF'];?>

3 - 短开标签可能会被关闭

改变:

<?
} else {
echo "Hello, ".$Fname." ".$Lname.".<br />";
echo "Your Password is, " .$Pword.".<br />";
}
?>

<?php
} else {
echo "Hello, ".$Fname." ".$Lname.".<br />";
echo "Your Password is, " .$Pword.".<br />";
}
?>
于 2013-03-06T19:58:49.910 回答
1

您的提交按钮需要更改为type="submit",您可能还需要在 else 语句中移动变量声明以避免 PHP 警告:

<input name="submit" type="submit" value="Submit" />

和:

<?php
} else {
    $Fname = $_POST["Fname"];
    $Lname = $_POST["Lname"];
    $Pword = $_POST["Pword"];
    echo "Hello, ".$Fname." ".$Lname.".<br />";
    echo "Your Password is, " .$Pword.".<br />";
}
?>

最后,我会尽量减少你把它放在if语句中的 html 的数量,如果你将来改变条件的处理方式,这可能会导致冲突:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Untitled Document</title>
    </head>
    <body>

    <?php
    if (!isset($_POST['submit'])) { // if page is not
        ?>
        <form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>">
            First name<input name="Fname" type="text" maxlength="12" /><br /><br />
            Last name<input name="Lname" type="text" maxlength="12" /><br /><br />
            <!--Address <input name="register_address" type="text" maxlength="12" /> <br /> <br />-->
            <!--<select name="state" value="State"></select>-->
            Password<input name="Pword" type="text" maxlength="12" /><br /><br />
            <!--Re-type Password<input name="register_password_confirm" type="text" maxlength="12" /><br /><br />-->
            <input name="submit" type="submit" value="Submit" />
        </form>
        <?php
    } else {
        $Fname = $_POST["Fname"];
        $Lname = $_POST["Lname"];
        $Pword = $_POST["Pword"];
        echo "Hello, ".$Fname." ".$Lname.".<br />";
        echo "Your Password is, " .$Pword.".<br />";
    }
    ?>
    </body>
</html>
于 2013-03-06T19:59:35.757 回答
0

尝试添加一个

<input type="hidden" name="submitted" value="1" />

并检查

$_POST['submitted'] == 1
于 2013-03-06T19:56:51.120 回答
0

尝试像这样回显结果:

echo "Hello, $Fname $Lname.<br />
      Your Password is, $Pword.<br />";

我忘了补充一点,当使用双引号回显时,您不需要变量后的点。

于 2013-03-06T19:58:03.407 回答