-1

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.

sadeee nadeee
  • 145
  • 1
  • 1
  • 11
  • Don't use `addError('Invalid Pin Number'); $this->_redirect('enterpintoverify'); ?>` to execute client side right? That will execute on generation. – chris85 Nov 17 '17 at 11:21

3 Answers3

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.

mega6382
  • 9,211
  • 17
  • 48
  • 69
1

Try using concatenate php code in your echo

echo "<script type='text/javascript'>
                        var datastring=".$data['pin'].";
                        $.ajax({....
SelVazi
  • 10,028
  • 2
  • 13
  • 29
1

It may happen due to parse error. Try concatenation for PHP Code.

var datastring=".$data['pin'].";
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
Harry
  • 11
  • 2