0

我正在尝试将一些数据从控制器发送到 phtml以在页面ajax/jason上显示它们textareaphtml

在我的控制器中,我有循环

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('*/*');
    }

    }

我的 phtml 文件:

<div class="content-header">
    <table cellspacing="0" class="grid-header">
        <tr>
            <td><h3><?=$this->__('Upload the Monthly Remittance')?></h3></td>

        </tr>

        <tr>

        </tr>
    </table>
</div>
<div class="entry-edit">
    <form id="edit_form" name="edit_form" method="post" enctype="multipart/form-data" action="<?=$this->getUrl('*/*/runremit')?>">
        <input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />

        <h4 class="icon-head head-edit-form fieldset-legend"><?=$this->__('Remittance file')?></h4>
        <?php echo $this->getBlockHtml('formkey') ?> 
        <fieldset>
            <table cellspacing="0" class="form-list">
                <tr>
                    <td><label for="fileinputname">Upload the file </td>
                    <td><input type="file" name="fileinputname" id="fileinputname" /></td>
                    <td>

                <button onclick="editForm.submit()" class="scalable save" type="button"><span><span>Submit & Run</span></span></button>

            </td>

            </table>

        </fieldset>
    </form>
</div>
 <input name="test" type="text" value="<?php echo Mage::getSingleton('core/session')->getData('variable1') ?>" />

 <script type="text/javascript">
    var editForm = new varienForm('edit_form');

</script>
4

2 回答 2

0

尝试使用 :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 事件添加到您的按钮,例如<button value="" onclick="sendTest()"> 在 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>

在控制器中这样做。

 public function runremitAction()
    {
echo $_GET['testemail'];
}
于 2013-11-01T06:06:14.677 回答
0

我在 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>
于 2013-11-04T05:53:05.760 回答