Javascript 在Magento中使用AJAX调用PHP文件
似乎我无法让Ajax调用PHP文件,也无法找出问题所在。我正在尝试制作一个从数据库读取数据的模块,它应该显示一个带有选项的表单。表单已填充,但当我更改选项时,它不会显示消息 基本上,它是一个模块,将列出数据库中的一些可用存储(id、名称和电子邮件),当选择存储时,电子邮件应该打印出来。 我以此为例: 以下是我到目前为止所做的: 在app\code\local\AdiGh\askStore\etc\config.xml中Javascript 在Magento中使用AJAX调用PHP文件,javascript,php,mysql,ajax,magento,Javascript,Php,Mysql,Ajax,Magento,似乎我无法让Ajax调用PHP文件,也无法找出问题所在。我正在尝试制作一个从数据库读取数据的模块,它应该显示一个带有选项的表单。表单已填充,但当我更改选项时,它不会显示消息 基本上,它是一个模块,将列出数据库中的一些可用存储(id、名称和电子邮件),当选择存储时,电子邮件应该打印出来。 我以此为例: 以下是我到目前为止所做的: 在app\code\local\AdiGh\askStore\etc\config.xml中 <frontend> <routers&g
<frontend>
<routers>
<askstore>
<use>standard</use>
<args>
<module>AdiGh_askStore</module>
<frontName>estores</frontName>
</args>
</askstore>
</routers>
<layout>
<updates>
<askstore>
<file>estores.xml</file>
</askstore>
</updates>
</layout>
</frontend>
在c:\Users\Adi\Desktop\askStore\app\design\frontend\default\default\layout\estores.xml中
<layout version="0.1.0">
<default>
<reference name="content">
</reference>
</default>
<askstore_index_index>
<reference name="content">
<block type="askstore/askstore" name="askstore" template="estores/estores.phtml" />
</reference>
</askstore_index_index>
<askstore_ajax_index>
<reference>
<block type="askstore/askstore" name="root" template="estores/estores.phtml" output="toHtml">
</block>
</reference>
</askstore_ajax_index>
{
公共职能响应()
{
$q=intval($\u GET['q']);
如果($q==0)
{
echo“存储为0”;
}
elseif($q>0){
$read=Mage::getSingleton('core/resource')->getConnection('core_read');//从数据库读取
$productTable=Mage::getSingleton('core/resource')->getTableName('adigh_askstore/estores');
$query=“从“$productTable.”中选择*,其中id=”$q.“”;
$result=$read->query($query);
而($row=$result->fetch()){
回显'ID:'.$row['ID'].
;
回显“ID:”.$row['name'].
;
回显“ID:”.$row['email']。
;
}
}
打印(结果);
}
}
因此,正如我所说,它连接到数据库,并用正确的选项和值填充select表单。但是当我在localhost/estores/index下访问它时,它将显示正确的表单,但是onChange将重新加载页面而不显示结果。
如果能读到一些建设性的意见,我将不胜感激
非常感谢 在AJAX请求中检查您的URL
xmlhttp.open(“GET”、“ajax/index/store/q=“+str,true”)代码>
似乎您正在尝试连接到localhost/estores/ajax/index/store
,而不仅仅是localhost/estores/index/
尝试删除存储段。您发送Get请求的storeAction在哪里?我的意思是:ajax/索引/存储/
其中是该函数的代码,当您说看不到任何更改时,该请求似乎会导致一个空字符串或错误
另一件事是,尝试一下:
1) 向您的操作发送POST请求(ajax/index/store/)
2) 在那里处理数据
3) 返回包含更多符合Magento的代码的结果,如:
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode('YOUR DATA'));
这将给我一个错误estores/index/q=404(未找到)当您直接在浏览器中访问ajax/index/store/q=test时会发生什么?那你有404吗?编辑:您在上一篇评论中没有提供ajax片段,这是指向正确控制器所必需的。您应该得到您的昵称:)您的提示帮助我解决了问题,现在问题已经解决了。我通过调用block.php中的响应公共函数创建了storeAction,并从中获取了参数。它就像一个符咒。再次感谢!
<?php
$theIds = $this->getId();
?>
<?php echo get_class($this)."<br />"; ?>
<script type="text/javascript">
function showEmail(str)
{
if (str=="")
{
document.getElementById("response").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("response").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax/index/store/q="+str,true);
xmlhttp.send();
}
</script>
<form name='myForm'>
Choose Store:
<select name="store" id="store" onChange="showEmail(this.value);">
<option value="">Please select a store</option>
<?php foreach ($theIds as $i => $theId) : ?>
<?php
$theNames = $this->getName();
$theName = $theNames[$i];
?>
<option value="<?php echo $theId; ?>"><?php echo $theName; ?></option>
<?php endforeach; ?>
</select>
</form>
<?php echo $this->theResponse() ?>
<?php
?>
Email: <div id="response"></div>
class AdiGh_askStore_Block_AskStore extends Mage_Core_Block_Template
public function getId()
{
$iduri='';
$collection = Mage::getModel('adigh_askstore/estores')->getCollection()->setOrder('id','asc');
foreach($collection as $data)
{
$iduri .= $data->getData('id') .',';
}
Mage::getSingleton('adminhtml/session')->addSuccess('Cool Ca marche !!');
return explode(',' , $iduri );
}
public function getName()
{
$name='';
$collection = Mage::getModel('adigh_askstore/estores')->getCollection()->setOrder('id','asc');
foreach($collection as $data)
{
$name .= $data->getData('name') .',';
}
Mage::getSingleton('adminhtml/session')->addSuccess('Cool Ca marche !!');
return explode(',' , $name );
}
public function getEmail()
{
$email='';
$collection = Mage::getModel('adigh_askstore/estores')->getCollection()->setOrder('id','asc');
foreach($collection as $data)
{
$email .= $data->getData('email') .',';
}
Mage::getSingleton('adminhtml/session')->addSuccess('Cool Ca marche !!');
return explode(',' , $email );
}
$q = intval($_GET['q']);
if($q==0)
{
echo "store is 0";
}
elseif ($q>0){
$read = Mage::getSingleton( 'core/resource' )->getConnection( 'core_read' ); // To read from the database
$productTable = Mage::getSingleton( 'core/resource' )->getTableName( 'adigh_askstore/estores' );
$query = "SELECT * FROM " . $productTable . "WHERE id = '".$q."'";
$result = $read->query($query);
while ( $row = $result->fetch() ) {
echo 'ID: ' . $row['id'] . '<br>';
echo 'ID: ' . $row['name'] . '<br>';
echo 'ID: ' . $row['email'] . '<br>';
}
}
print_r($result);
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode('YOUR DATA'));