skip to Main Content

I am working on Magento project.There I have a controller which is a php file.I want to write a JavaScript function inside that php file.
This is the function which results in the error,

 public function verifyPinAction()
         {
           $data = $this->getRequest()->getPost();
             echo "<script type='text/javascript'>
           var datastring=<?php echo $data['pin'];?>;
           $.ajax({
type: 'POST',
url: 'http://xxxxx.com/xxxxxx/ErrorProcessing1.php',
data : datastring,
//dataType: 'json',

success: function(html) {



   //alert(html);


   if(html=='wrong text entered')
  {
    <?php Mage::getSingleton('core/session')->addError('Invalid Pin Number');
                  $this->_redirect('enterpintoverify'); ?>
  } 
  else{
    <?php Mage::getSingleton('core/session')->addSuccess('Your Email is verified');
                $this->_redirect('enterpintoverify');?>
  } 


}
});
           </script>
           ";

         }

It gives the following error

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /home/xxxxx/public_html/newtradesdev/app/code/local/Customer/Register/controllers/IndexController.php on line 65

Line 65 refers to
var datastring=<?php echo $data['pin'];?>;
Can someone tell me where have I gone wrong?

I have used xxxx marks beacause I am working on hosted site.Please skip the urls.

3

Answers


  1. Change your echo to this instead:

    public function verifyPinAction()
    {
       $data = $this->getRequest()->getPost();
    ?>
    <!--HTML STARTS HERE-->
    <script type='text/javascript'>
               var datastring=<?php echo $data['pin'];?>;
               $.ajax({
    type: 'POST',
    url: 'http://xxxxx.com/xxxxxx/ErrorProcessing1.php',
    data : datastring,
    //dataType: 'json',
    
    success: function(html) {
    
    
    
       //alert(html);
    
    
       if(html=='wrong text entered')
      {
        <?php Mage::getSingleton('core/session')->addError('Invalid Pin Number');
                      $this->_redirect('enterpintoverify'); ?>
      } 
      else{
        <?php Mage::getSingleton('core/session')->addSuccess('Your Email is verified');
                    $this->_redirect('enterpintoverify');?>
      } 
    
    
    }
    });
               </script>
    <!--HTML ENDS HERE-->
    <?php
    
    }
    
    ?>
    

    This presents more readable code and will not cause any errors either.

    Login or Signup to reply.
  2. Try using concatenate php code in your echo

    echo "<script type='text/javascript'>
                            var datastring=".$data['pin'].";
                            $.ajax({....
    
    Login or Signup to reply.
  3. It may happen due to parse error. Try concatenation for PHP Code.

    var datastring=".$data['pin'].";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search