Javascript Magento我如何从控制器发送数据以在phtml文件中显示它们
我正试图使用Javascript Magento我如何从控制器发送数据以在phtml文件中显示它们,javascript,php,jquery,ajax,magento,Javascript,Php,Jquery,Ajax,Magento,我正试图使用ajax/jason将一些数据从控制器发送到phtml,以在phtml页面的textarea上显示它们 在我的控制器中,我有一个循环 foreach($varlines as $vars =>$variable) { $completestatus->setData('comment',$oldcoment.' <br/ > Paid on LEC chq # '.$chqnumber." ".$datepaid." ".$amount)->
ajax/jason
将一些数据从控制器发送到phtml,以在phtml
页面的textarea
上显示它们
在我的控制器中,我有一个循环
foreach($varlines as $vars =>$variable)
{
$completestatus->setData('comment',$oldcoment.' <br/ > Paid on LEC chq # '.$chqnumber." ".$datepaid." ".$amount)->save();
//because Mage::getSingleton('adminhtml/session')->addSuccess comes after load page
//I need ajax to show the updated order while its running the loop
//and displaying it on phtml
}
<?php
class Ahb_LecRemittance_Adminhtml_LecRemittanceController extends Mage_Adminhtml_Controller_Action
{
public function indexAction()
{
$this->loadLayout()->renderLayout();
}
public function runremitAction()
{
$post = $this->getRequest()->getPost();
try {
if (empty($post)) {
Mage::getSingleton('adminhtml/session')->addError("Invalid form data.");
Mage::throwException($this->__('Invalid form data.'));
}
if(isset($_FILES['fileinputname']['name']) and (file_exists($_FILES['fileinputname']['tmp_name']))) {
try {
$uploader = new Varien_File_Uploader('fileinputname');
$uploader->setAllowedExtensions(array('csv')); // or 'jpg','jpeg','gif','png' pdf or anything
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media')."/tmp". DS ;
$originalfilename=str_replace(' ','',stripslashes($_FILES['fileinputname']['name']));
$originalfilename = trim(preg_replace('/\s*\([^)]*\)/', '', $originalfilename));
$uploader->save($path,$originalfilename );
$data['fileinputname'] = $originalfilename;
//run the file
$path = Mage::getBaseDir('media')."/tmp". DS ;
$filename=$path.$originalfilename;
$file=fopen($filename,"r");
$readchq=false;
$chqnumber="";
if($file!==false){
while(!feof($file))
{
$line=fgets($file);
$varlines=explode(",",$line);
$ordernum=0;
foreach($varlines as $vars =>$variable)
{
$i=0;
if(strpos($variable,"YICP")!==false){ //YICP exist
$ordernum=str_replace('YICP','',$variable);
$amount = str_replace('"','',end($varlines));
$date1=str_replace('"','',$varlines[count($varlines)-2]);
$date2 = str_replace('/', '-', $date1);
$datepaid= date('Y-m-d', strtotime($date2));
$historyid=0;
//get complete status to append its comments
$remittmodel = Mage::getModel('ahb_lecremittance/remittance')
->setOrderid($orderid)
->setAmount($amount)
->setCreated_at($datepaid)
->setChqnum($chqnumber)
->save();*/
$result = array("Order "=>$ordernum,"Amount"=>$amount,"CHQ Num"=>$chqnumber);
//$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
//HERE I need to view the order number added in table while running the foreach loop
//How I do this in ajax
Mage::getSingleton('core/session')->setData('variable1', 'value');
}
}
}
}
fclose($file);
unlink($filename);
Mage::getSingleton('adminhtml/session')->addSuccess($message);
} //end of if fopen
else
Mage::getSingleton('adminhtml/session')->addError("Error: Couldn't open the file ".$filename);
$this->_redirect('*/*');
}catch(Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
$this->_redirect('*/*');
}
}
foreach($varlines作为$vars=>$variable)
{
$completestatus->setData('comment',$oldcoment.。$chqnumber.。“$datepaid.”“$amount”->save();
//因为Mage::getSingleton('adminhtml/session')->addSuccess出现在加载页面之后
//我需要ajax在运行循环时显示更新的顺序
//并将其显示在phtml上
}
尝试使用:Mage::register('variable','value')
和ge这个变量类似:Mage::registry('variable')代码>
另一种方法是使用会话处理数据。
设置变量:Mage::getSingleton('core/session')->setData('variable_name','value')代码>
获取变量:Mage::getSingleton('core/session')->getData('variable_name')代码>
如果您想向ajax显示数据,而不仅仅是像这样在循环之后回显变量
echo $varaible;.
向按钮添加onclick事件,如
在ajax返回中,您将获得数据
为ajax请求编写Js代码,如下所示:
<script>
function sendTest()
{
// formdata contains the variable data that you want to send with ajax request
var formData = {testemail:jQuery('#test_recipient').val(),customeremail:jQuery('#test_customer_email')};
var form_key = '<?php echo Mage::getUrl("*/*/runremit/key/".$this->getFormKey());?>';
jQuery.ajax({
url: form_key+'?isAjax=true',
type: 'GET',
data: formData,
success: function( data ) {
alert(data);
}
});
}
</script>
我在phtml文件中使用了这个方法
<script type="text/javascript">
function update() {
var url=document.getElementById("url_key").value;
var textarea = document.getElementById("chatlog"); // Replace with the ID of your txtarea
var client = new XMLHttpRequest();
client.open('GET', url,true);
client.onreadystatechange = function() {
textarea.value = client.responseText;
}
client.send();
//setTimeout('scroll(document.width, document.height);', 100); // Scroll to the bottom of the page.
}
setInterval('update()', 500); // Loop every 1000 milliseconds (i.e. 1 second)
</script>
函数更新(){
var url=document.getElementById(“url_key”).value;
var textarea=document.getElementById(“chatlog”);//替换为txtarea的ID
var client=new XMLHttpRequest();
client.open('GET',url,true);
client.onreadystatechange=函数(){
textarea.value=client.responseText;
}
client.send();
//setTimeout('滚动(document.width,document.height);',100);//滚动到页面底部。
}
setInterval('update()',500);//每1000毫秒循环一次(即1秒)
如果您使用ajax,则必须回显数据;如果您使用json,则必须使用json编码。一次尝试一种方法。您知道如何将数据从控制器传递给phtmlyes吗?为什么不知道。有很多方法,如会话,设置变量并获取phtml.in控制器中的phtml.in数据,当我在每次迭代后运行循环foreach时,我需要在phtml中显示添加的订单号,并在textarea或div中显示,以此类推,谢谢您的帮助,建议的解决方案在加载页面后在phtml页面中显示值,但是我在循环运行时需要它,显示值就像ajaxI没有得到它一样,循环在控制器中,ajax应该在phtml文件中。您认为$this->getResponse()->setBody可以帮助您将ajax调用(HTML)和控制器数据放入问题中吗,我编辑了我的问题,实际上我需要在phtml中添加ajax调用,我不知道我在magentoHi中是怎么做的,我需要在循环的每次迭代中调用ajax调用,以在html输入上显示迭代的结果,你知道magento中的导入功能吗?它在导入产品时向你显示产品计数器和结果,我需要这样的东西。。。谢谢
public function runremitAction()
{
echo $_GET['testemail'];
}
<script type="text/javascript">
function update() {
var url=document.getElementById("url_key").value;
var textarea = document.getElementById("chatlog"); // Replace with the ID of your txtarea
var client = new XMLHttpRequest();
client.open('GET', url,true);
client.onreadystatechange = function() {
textarea.value = client.responseText;
}
client.send();
//setTimeout('scroll(document.width, document.height);', 100); // Scroll to the bottom of the page.
}
setInterval('update()', 500); // Loop every 1000 milliseconds (i.e. 1 second)
</script>