[英]Send JSON data from one page and receive dynamically from another page using AJAX when both page are open
[英]Magento displays same page inside the visible page when trying to send/receive data from another php
我正在尝试在几周前创建的自定义后端模块上实现一项新功能。 新功能包括进行查询,以便在“选择选项”中列出特定客户的一些数据。 然后,当我选择这些选项之一时,我制作了一个脚本,该脚本捕获了该选项,并使用GET
方法将其发送到另一个.php,然后php使用选定的数据进行处理,然后进行回显与任何。
代码如下( data.phtml
):
<script language="JavaScript" type="text/javascript">
function showPresu(str){
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
alert('nothing here');
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
alert('makes request');
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
alert('new object');
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","receiveselection.php?q="+str,true);
xmlhttp.send();
}
</script>
如您所见,我已经编写了一些警报,以确保代码输入到脚本中。
选择/选项如下( data.phtml
):
echo '<select name="presus" onchange="showPresu(this.value)">';
for($n=1;$n<=$h; $n++){
echo "<option value='$status_id[$n]'>$status_id[$n]";
}
echo "</select>";
$ status_id [$ x]:这是一个数组,其中包含我一开始就介绍的一些数据。
并且必须显示最终数据的div是( data.phtml
):
<div id="txtHint">
<b>result info will be listed here.</b>
</div>
最后,必须与通过GET
方法发送的信息进行交互的PHP是( receiveselection.php
,与data.phtml
在同一文件夹中。请注意,此PHP将更改为另一个代码,我的意思是,我将更改以下代码进行另一个查询,但现在可以进行测试了):
<?php
$q=$_GET["q"];
echo $q;
?>
唯一的问题是,当我尝试在为此创建的div中显示“ receiveselecton.php”时,而不是观看“ receiveselection.php”的结果时,它显示的是与之前相同的magento页面( data.phtml
),我的意思是,它两次出现在同一页面上,一个作为“正常”页面,另一个在div
。
有谁知道如何调用receiveselection.php
而不是创建另一个data.phtml
?
编辑:如果我将此代码放在magento之外,它可以工作,但在它里面却不行。 因此,问题可能出在我不知道的与magento不一致的地方。 或者,如果有人知道更好的方法,请告诉我。
我在我的电脑上尝试了您的代码,它工作正常。.我发现的问题是,您发送的请求无法到达文件receiveselection.php
解决您的问题的方法是,您可以为receiveselection.php创建一个控制器动作,可以说receiveselectionAction将您的代码更改为此
xmlhttp.open("GET","your_module/your_controller/receiveselection.php?q="+str,true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.