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.

3
  • I have searched everything but nothing seems to work what did not worked? Commented Jul 8, 2015 at 14:17
  • var lat = '<?php echo $lat;?>';-> this didn't work and trying to get value from hidden input field didnt work either Commented Jul 8, 2015 at 14:28
  • Possible duplicate of passing php variable from controller to javascript function in the head of a view Commented Aug 10, 2019 at 6:31

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

Comments

0
var lat = '<?php echo $lat;?>';
show(lat);
function show(lat){
  location.href = 'something.com?latval='+lat;
}

4 Comments

are you sending url from controller to view and you want to have it in js.
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.
something like these stackoverflow.com/questions/13085388/… . do concat the lat value.
This answer is missing its educational explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.