0

I have this,

    $(document).ready(function(){

        $('#link').click(function(){
            var user_login = <?php $base_url; ?>;
            window.location = user_login + '/login';
        });

    });

note that $base_url is global variable. This works in FF but not on Chrome and IE. Thanks.

10
  • Right click on the webpage then Click View Source and see how it is rendered in Chrome and IE. Commented Nov 27, 2012 at 18:42
  • In which kind of file is this code: in a *.php page I suppose ? Commented Nov 27, 2012 at 18:42
  • @RomainBruckert, in a separate *.js file. Commented Nov 27, 2012 at 18:42
  • 3
    How can PHP work in a JS file... ? Sorry, did i miss something ? :p Commented Nov 27, 2012 at 18:44
  • 1
    just put your script in the php file. External js file don't process the php. Commented Nov 27, 2012 at 18:47

5 Answers 5

5

You need to add quotes around the php tags to designate that the value of $base_url is a string:

$(document).ready(function(){

    $('#link').click(function(){
        var user_login = "<?php echo $base_url; ?>";
        window.location = user_login + '/login';
    });

});

That way, when the browser gets this block, it will look something like:

$(document).ready(function(){

    $('#link').click(function(){
        var user_login = "http://www.example.com"; // after php is executed
        window.location = user_login + '/login';
    });

});
Sign up to request clarification or add additional context in comments.

4 Comments

it just treats the thing between the quotes as string.
should be var login = <?php '\''.$base_url.'\''; ?>;
@CHiRiLo - I mentioned above as a comment to your post about processing PHP in an external js file. It won't work, because php isn't executed on that file. That file is only included after the php page is sent to the browser. The server doesn't know to use php to parse the .js file, so the variable is never replaced with the value. - Since this is a server issue, I don't see how this works in FF and not in others - unless we're missing some information.
@Akash - no that's not correct. What we're looking for here is a value of $base_url that is used as a string in the javascript. The quotes are not necessary in the PHP block, but are necessary in the javascript block.
3

You need to echo the variable, not just simply have it in a code block. And for safety, to prevent introducing JS syntax errors, you should json-encode the value:

        var login = <?php echo json_encode($base_url); ?>;

Comments

2

See below, note the quotes around the PHP echo

$(document).ready(function(){
    $('#link').click(function(){
        var login = '<?php echo $base_url; ?>';
        window.location = user_login + '/login';
    });

});

Comments

1

I suppose its a PHP file, you need to surround your variable contents by quotes to javascript

 $(document).ready(function(){

        $('#link').click(function(){
            var login = <?php '\''.$base_url.'\''; ?>;
            window.location = user_login + '/login';
        });

    });

Comments

0

Run this script in a PHP file, not in a .js file, and don't forget tho ECHO your variable and add quotes (in javascript), as said in other answers !

5 Comments

Well thought out answer. Good job.
How is this downvoted, beeing accepted, solving the problem, straightforward and true ?? Sorry, don't get it
you basically just said "look at all the other answers here - that's your answer" - I wouldn't call that an acceptable answer
I basically said that any of the answers would work.. IF ONLY not in a JS file... which was the main problem here.
It's OK, I'll move on. I'd just say to be careful: meta.stackexchange.com/questions/138517/…

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.