Javascript PHP echo HTML输入变量位于heredoc外部
我使用herdoc将我的HTML包含在PHP中,我想获取herdoc中的用户输入变量,并将其回显出来。我尝试使用$_GET[“input”],但未定义索引时出错:input 我可以知道如何获取输入变量吗Javascript PHP echo HTML输入变量位于heredoc外部,javascript,php,html,Javascript,Php,Html,我使用herdoc将我的HTML包含在PHP中,我想获取herdoc中的用户输入变量,并将其回显出来。我尝试使用$_GET[“input”],但未定义索引时出错:input 我可以知道如何获取输入变量吗 <?php $htmlfile= <<<html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Seller Evaluation S
<?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;
?>
$\u GET[…]
采用表单元素的名称。因此,在HTML中,您需要包括:
<input type="text" name="input" id="searchinput" size="33" placeholder="Search Electronic Gadgets..." autofocus>
这样,只有提交表单时,代码才会运行。您必须调用输入名称“searchinput”。但是,如果你不发送表单,它将不可用,你必须用“name”标签来称呼它,而不是用“id”来称呼它 您可以在发送后使用Javascript显示它,无需使用PHP 这是可行的,但是表单必须由同一个文件处理,因此“action”必须为空。如果没有,您可以在表单的文件进程中获取get参数,在您的示例ttt.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;
?>
如果输入
未在url中传递,则不会进行设置。使用$\u GET['input']
将抛出一个错误,直到您的表单提交了一个名为input
的元素,或者您使用?input=无论什么
。谢谢,我已经添加了if-else。现在还可以,非常感谢你的帮助,感谢你的解释和例子,我是网络新手,所以我所做的也很糟糕。
<?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;
?>