0

i have a variable $lat and i need it to pass to javascript function show() where $lat is passed to the url through window.location.href. I have searched everything but nothing seems to work. BTW i dont want to use ajax for this. can it be done with other methods? I am using php codeigniter.

Nabin
  • 166
  • 3
  • 11
  • `I have searched everything but nothing seems to work` what did not worked? – Shaiful Islam Jul 08 '15 at 14:17
  • var lat = '';-> this didn't work and trying to get value from hidden input field didnt work either – Nabin Jul 08 '15 at 14:28
  • Possible duplicate of [passing php variable from controller to javascript function in the head of a view](https://stackoverflow.com/questions/9405742/passing-php-variable-from-controller-to-javascript-function-in-the-head-of-a-vie) – mickmackusa Aug 10 '19 at 06:31

2 Answers2

0

Without using Ajax, you can only pass that variable through your view. Imagine the following scenario:

function yourController()
{
  $data['lat'] = 'http://google.com';
  $this->load->view('yourView/index', $data);
}

<html>
  <head>
  </head>
  <body>
     <script>
        function show()
        {
           window.location.href = '<?php echo $lat; ?>';
        }
     </script>
  </body>
</html>
Linesofcode
  • 5,327
  • 13
  • 62
  • 116
0
var lat = '<?php echo $lat;?>';
show(lat);
function show(lat){
  location.href = 'something.com?latval='+lat;
}
  • are you sending url from controller to view and you want to have it in js. – Prem Bikram Limbu Jul 08 '15 at 14:51
  • Actually i have several other javascript variables which i have passed to a link with window.location.href..... now i need to add this $lat variable as a parameter to that link. $lat variable was passed in current view from controller. – Nabin Jul 08 '15 at 15:00
  • something like these http://stackoverflow.com/questions/13085388/sending-an-url-but-staying-on-the-same-page-php-codeigniter-javascript?rq=1 . do concat the lat value. – Prem Bikram Limbu Jul 08 '15 at 16:05
  • This answer is missing its educational explanation. – mickmackusa Mar 02 '22 at 05:14