我有php页面,用户可以在其中输入共同基金股票代码。
我试图让用户点击一个按钮,并在页面上显示基金的名称。
我假设我需要将输入框中的值传递到一个单独的php页面。我有一个php页面,它使用php简单的html dom解析器,如果我硬编码股票代码,php页面将从网页中提取基金的名称。
我不清楚如何将变量从输入框传递到另一个php文件,也不清楚是否需要一个单独的php文件。
有没有可能将这些都保存在同一个php页面上?现在,在单独的php页面上,我有一个包含基金名称的变量。
我不知道如何在第一个php页面上显示该值。如果有人能帮我理解这个概念,我可以写一些代码。
下面是一个名为getFundName.php
的页面,它获得了基金名称。
<?php
include('simple_html_dom.php');
$firstvar = "http://www.marketwatch.com/investing/fund/";
$secondvar = "LLPFX";
$combovar = $firstvar . $secondvar;
$html = new simple_html_dom();
$html->load_file($combovar);
$getFundName = $html->find('#instrumentname',0);
print_r($getFundName->plaintext);
//echo $getFundName->plaintext;
$html->clear();
unset($html);
?>
Below is my guess at the other code, but obviously it's not functioning.
<?php
some php code here
?>
<html>
<body>
<script language="JavaScript">
function getFundName() {
var xhr=createXHR();
xhr.open("GET", "getFundName.php",true);
xhr.onreadystatechange=function()
{
if(xhr.readyState == 4)
{
document.getElementById("displayFundNameHere").innerHTML= xhr.responseText;
}
};
}
</script>
<div id="displayFundNameHere"><div>
Fund Symbol: <input type="text" id='fundSymbolBox'/>
<input type="button" value="Get Fund Name" onclick="getFundName()"/>
</body>
</html>
您要做的是设置页面以接受输入,并对PHP页面执行AJAX请求,该请求将返回您想要的数据。
查看您的代码,您似乎已经完成了大部分工作,不要忘记,在分配回调后需要调用xhr.send()
。
如果你想把所有内容都放在同一个页面上,你可以把所有内容包装成一个发布到自己的表单,让PHP代码从POST变量中提取数据并填充数据,而不需要使用AJAX或javascript。