I am trying to redirect to another page when condition is true using ajax. But I cannot do it when I use the php base_url. But If I give the direct path (http://localhost/CRH/......), it works. I cannot find what the issue is use of base url. Please help...
1 Answers
If you're seeing PHP code client-side then your PHP isn't executing at all.
This is probably because you've put this PHP code in a .js
file. That's for JavaScript, not for PHP.
It's understandable that you're going to want to keep your JavaScript separate from your HTML. However, PHP-generated values are probably still going to have to be in the HTML since they need to come from PHP files.
(You could configure your web server to process .js
files for PHP code, or put your JavaScript code in a PHP file and set the headers to return JavaScript. But those seem like a bit of overkill for this.)
What you could do is emit a simple JavaScript value to your HTML in your .php
file, something like this:
<script type="text/javascript">
var baseUrl = '<?php echo base_url(); ?>';
</script>
Then in your JavaScript file you'd be able to reference that value:
window.location = baseUrl + 'index.php?etc.';

- 208,112
- 36
- 198
- 279