PHP echo html 输入变量在 heredoc 之外
PHP echo HTML input variable outside heredoc
我使用 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;
}
这样,代码仅在表单提交时运行。
相关文章:
- Emberjs应用程序加载在除Index之外的所有路由上
- Regex匹配除“”之外的所有字符;.js”;
- RegExp:匹配Javascript中除regex值之外的所有内容
- 消息显示之外的淘汰验证
- 如何操作iframe之外的元素
- Grunt-connect在根目录之外提供静态文件
- 如何在显示侧边栏时禁用背景,单击除侧边栏之外的任何位置都会关闭侧边栏
- Ng隐藏在Ng-click元素之外
- 如何在函数之外使用变量
- 如何从字符串中删除除字母、数字、空格、感叹号和问号之外的所有内容
- 如何替换JavaScript字符串中除字母和数字之外的所有内容(空格/符号)
- 宽度为100%的CSS元素位于视口之外
- 在函数调用中封装数据除了隐藏数据之外还有什么优点
- 如何在angular中使构造函数之外的服务可用
- 触发器方法在javascript对象中定义,但在对象本身之外
- 使用javascript替换字符串中除第一个和最后一个之外的所有字符
- 如何通过css模糊除当前悬停之外的所有图像
- 访问重复之外的ng重复ng形式
- 如何使用 JavaScript 正则表达式搜索除一个实例之外的所有内容
- PHP echo html 输入变量在 heredoc 之外