变量 php 标题标签不起作用

Variable php title tag doesn't work

以下代码似乎不起作用,我想知道为什么?

<head>
<?php
$pageTitle = $_POST["title"];
"<title>". $pageTitle."</title>";
?>
</head>

我收到以下消息:

Notice: Undefined index: title

我只想根据用户的输入设置网页标题。

忘了说我有两个页面。一个 HTML 页,包含一个表格和一个 php 文件。 HTML 文件具有以下形式:

<form action="executer.php" method="POST">

Titel: <input style="margin-left: 83px" type="text" name="titel"><br><br>
Achtergrondkleur:   <select>
                        <option name="blauw" value="blauw">Blauw</option>
                        <option name="groen" value="groen">Groen</option>
                        <option name="geel" value="geel">Geel</option>
                    </select><br><br>
Lettertype: <input style="margin-left: 50px" type="radio" name="lettertype" value="ari">Arial<br>
            <input style="margin-left: 124px" type="radio" name="lettertype" value="comi">Comic Sans<br>
            <input style="margin-left: 124px" type="radio" name="lettertype" value="timi">Times New Roman<br><br>

Lettergrootte:  <select style="margin-left: 32px">
                    <option name="5" value="5">5</option>
                    <option name="10" value="10">10</option>
                    <option name="15" value="15">15</option>
                </select><br><br>
<input type="submit" name="verzenden" value="Verzenden!">

</form>

你是不是忘记回显了?

<head>
<?php
$pageTitle = $_POST["title"];
echo "<title>". $pageTitle. "</title>";
?>
</head>

I get the following message: Notice: Undefined index: title

A:您表单中的元素没有名称属性。

例如:

<input type="text" name="title">
                   ^^^^^^^^^^^^

此外,请确保您的表单确实具有 POST 方法。

即:

<form action="handler.php" method="post">

为了争辩,您缺少 echo 用于:

"<title>". $pageTitle."</title>";

也使用isset()

if(isset($_POST["title"])){
    $pageTitle = $_POST["title"];
     echo "<title>". $pageTitle. "</title>";
}
else{
    $pageTitle = "<title>Title for page was not set.</title>";
     echo $pageTitle;
}

error reporting 添加到文件顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:错误报告只能在试运行中进行,绝不能在生产中进行。