2

I'm trying to change the include path of a php file with javascript if the user changes the language of the website.

I'm using this code to check if the language of the website is english or not.

<script type="text/javascript">
        $(function() {
            var pathname = window.location.pathname;
            if (pathname.search('/en/') != -1){
                //I wanna insert the new path here
            }
        });
    </script>

And then I wanna change the include function from

<?php include 'navigation.php'; ?>

to

<?php include 'navigation_en.php'; ?>

Does anybody know how to make this work? Or do I have to check the pathname with php to change the php include function?

6
  • php runs server side, its finished before the javascript starts, anyway I don't see why you need the js at all, it can all be done with php Commented Jun 26, 2012 at 9:19
  • You can't change the include path with javascript. Commented Jun 26, 2012 at 9:19
  • When that Javascript code is called, PHP is done working. You have to reload your site and work with parameters. Commented Jun 26, 2012 at 9:19
  • @Dagon I'm not that good in PHP. Do you know how I can achieve the same result in PHP? Commented Jun 26, 2012 at 9:20
  • @linzprod substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) gives you the language code in PHP Commented Jun 26, 2012 at 9:21

3 Answers 3

3

You cannot do this with pure javascript, because javascript is executed after the php processing completes. You can do the same thing with pure PHP though:

$uri = $_SERVER['REQUEST_URI'];
if(preg_match('/^\/en\//', $uri)) {
    include_once('include_en.php');
}
else {
    include_once('include.php');
}

This code assumes that your URL's would be in the form http://www.mydomain.com/en/some/page.php

EDIT:

As this code runs inside wordpress, $_SERVER['REQUEST_URI] does not return the language part. Thanks to the OP, the way to do it in wordpress would be:

$lang = get_bloginfo('language');
if(preg_match('/^\/en-/', $lang)) {
    include_once('include_en.php');
}
else {
    include_once('include.php');
}
Sign up to request clarification or add additional context in comments.

8 Comments

This answer actually makes sense.
@AleksG This answer makes sense even to me. But since I'm using Wordpress with readable permalinks the $uri is the same with both languages. When I'm echoing out the $uri. Is there a different solution that the REQUEST_URI? Since the $uri starts after the /en/
@linzprod I never worked with wordpress myself, though have some ideas about it. What do your URL's look like?
@AleksG This is the German Version: restaurant-salzhaus.ch/salzhaus/konzept and this is the english version of the site: restaurant-salzhaus.ch/en/salzhaus/konzept
In wordpress, there are function site_url() and home_url() - maybe one of them will help. If not, then have a look at get_bloginfo function - there's got to be a parameter for this.
|
1

You cannot use just JavaScript alone to change the variables in PHP. But you can do one thing. You can use AJAX Queries, when the value of the <select> tag changes, by firing a request to the server.

$("select.language").change(function(){
    $.ajax({
        url: 'changeLang.php?lang=' + $(this).val(),
        success: function(data) {
            alert("Successfully set the language to " + data);
        }
    });
});

In the backend PHP Code, you can do this way:

if(isset($_GET["lang"]))
    // Set the lang.
    die("English");

Comments

0

I'm fairly certain this isn't possible, since the PHP generates markup before it's sent to the client. Another solution would be to reload the page with JavaScript, and include some kind of parameter in the URL which then tells PHP which include tag to use.

Comments

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.