PHP echo html 输入变量在 heredoc 之外

PHP echo HTML input variable outside heredoc

本文关键字:heredoc 之外 变量 输入 echo html PHP      更新时间:2023-09-26

我使用 heredoc 将我的 HTML 包含在 PHP 中,我想获取 heredoc 中的用户输入变量,并将其回显出来。我尝试使用 $_GET["input"],但我得到错误未定义的索引:输入我可以知道如何获取输入变量吗?

<?php
$htmlfile= <<<html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Seller Evaluation System</title>
<style type="text/css">
    body {background-image:url("images.jpg");}
</style>
</head>
<body>
<h1><center><font color="darkblue">Seller Evaluation System</font><center></h1>
    <p><center>
        <script>
            function searchSite(){
            var input=document.getElementById("searchinput").value;
            var searchForm=document.getElementById("searchForm");
            searchForm.action="http://www.mudah.my/Malaysia/Electronics-3000/"+input+"-for-sale?lst=0&fs=1&q="+input+"y&cg=3000&w=3&so=1&st=s";
            searchForm.submit();
        }
        </script>
        <form method="get" action="ttt.php" id="searchForm">
        <input type="text" id="searchinput" size="33" placeholder="Search Electronic Gadgets..." autofocus>
        <button onclick="searchSite()">Search</button>
        </form>
        <p><label>Mudah<input type="checkbox" name="searchFrom" value="Mudah" checked/></label>
        <label><font color="grey">Lazada</font><input type="checkbox" name="searchFrom" value="Lazada" disabled="disabled"/></label>
        <label><font color="grey">Lelong</font><input type="checkbox" name="searchFrom" value="Lelong" disabled="disabled"/></label>
        <label><font color="grey">Ebay</font><input type="checkbox" name="searchFrom" value="Ebay" disabled="disabled"/></label></p>
    </center></p></br>
</body>
</html>
html;
echo $htmlfile;
$userInput=$_GET["input"];
echo $userInput;
?>

您必须将输入名称称为"搜索输入"。但是,如果您不发送表单,则无法使用表单,并且必须通过"名称"标签调用它,通过"id"来调用它。

您可以在发送后使用Javascript显示它,而不是PHP。

这有效,但表单必须由同一个文件处理,因此"动作"必须为空。如果没有,您可以在示例 ttt 中获取文件处理表单中的 GET 参数.php

<?php
$htmlfile= <<<html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Seller Evaluation System</title>
<style type="text/css">
    body {background-image:url("images.jpg");}
</style>
</head>
<body>
<h1><center><font color="darkblue">Seller Evaluation System</font><center></h1>
    <p><center>

        <form method="get"  id="searchForm">
        <input type="text" name="searchinput" size="33" placeholder="Search Electronic Gadgets..." autofocus>
        <button type="submit">Search</button>
        </form>

    </center></p></br>
</body>
</html>
html;
echo $htmlfile;
$userInput=$_GET["searchinput"];
echo $userInput;
?>

$_GET[..]采用表单元素的名称。因此,在您的HTML中,您需要包含以下内容:

<input type="text" name="input" id="searchinput" size="33" placeholder="Search Electronic Gadgets..." autofocus>

另请注意,您只想在提交表单时获取该项目,您需要首先检查 GET 是否存在:

// If we submitted our form with a element named "input", then echo
if(isset($_GET["input"]) {
    $userInput=$_GET["input"];
    echo $userInput;
}

这样,代码仅在表单提交时运行。