0

PHP を使用してブログのような Web ページを作成するプロジェクトに取り組んでいます。フォームの上の画面にテキストを印刷したいのですが$_GET、データが入力される前に変数がフォームからデータを取得しようとしているため、印刷できないようです。フォームの上にテキストを配置することは可能ですか?

これまでの私のコードは次のとおりです:(PHPは、「basic.php」(ファイル名)をタグのaction属性に入れることで画面を更新します)<form>

<!-- this file is called basic.php-->
<!DOCTYPE html>
<html>
<head>
<title>My Blog</title>
<style type = "text/css">
h2
{
color:#FF2312;
text-align:center;
font-family:Impact;
font-size:39px;
}
p
{
font-family:Verdana;
text-align:center;
color:#000000;
font-size:25px;
}
</style>
</head>
<body>
<?php 
    $subject=$_GET["msg"];//variable defined but attempts to get unentered data
?>

<i>  <?php print $subject;//prints var but gets error message because $subject can't get form data ?></i>
<!--want to print text above form-->
<form name = "post" action = "basic.php" method = "get">
<input type = "text" name = "msg">
<input type = "submit">
</form>

</body>
</html>
4

3 に答える 3

3

存在する場合にのみメッセージを表示したいようですよね?

<?php if ( ! empty($_GET['msg'])) : ?>
<i><?= $_GET['msg']; ?></i>
<?php endif; ?>
于 2013-03-08T19:44:25.520 に答える
1

セッション変数を使用:

...
</head>
<body>
<?php
    session_start(); //if is not started already
    if(isset($_GET["msg"]))
        $_SESSION['subject']=$_GET["msg"];
?>

<i>  <?php if(isset($_SESSION['subject']))
            print $_SESSION['subject']; ?></i>
<!--want to print text above form-->
<form name = "post" action = "basic.php" method = "get">
...
于 2013-03-08T19:39:35.033 に答える
1

通常、次の形式の隠し変数を使用してこれを解決します。

<form name = "post" action = "basic.php" method = "get">
<input type = "text" name = "msg">
<input type = "hidden" name="processForm" value="1">
<input type = "submit">
</form>

次に、フォームを処理する前にその変数を確認します。

<?php 
    if($_GET["processForm"]){
        $subject = $_GET["msg"];//variable defined but attempts to get unentered data
    }else{
        $subject = "Form not submitted...";
    }
?>

これは一般に、フォームが送信される前に処理されるのを防ぐ良い方法です。これは、自己送信フォームの危険です。

于 2013-03-08T19:45:02.820 に答える