Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

php - Wordpress: Use method="post" for multiple language selection

I'm building a website that has dual languages with two flags as an entry page. I'm planning on using <form method="post"> around the flags so the user can select the language they want.

Then on the following pages I want to use something like:

<?php
if( $_POST['language']=='uk' ){
    echo $uk;
}elseif( $_POST['language']=='french' ){
    echo $french;}
?>

So on clicking the flag, they have selected the language they want. Will that only work on the next page after they have clicked the flag or can they carry on navigating to different pages and it still pick up what language was selected?

If that doesn't work, how else can it be done?


UPDATE:

I don't think I made it clear before that I'm using Wordpress, which apparently doesn't like $_SESSION.

I have this on a template called region.php to submit the language selection:

    <form action="<?php the_permalink(); ?>/home" name="region" method="post">              
        <div id="uk">
            <a href="javascript:document.region.submit()" name="UK">
                <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/-uk.png" width="259" height="160" alt="UK" />
            </a>
            <h1 style="color:black!IMPORTANT;">Enter United Kingdom site</h1>
        </div>

        <div id="world">
            <a href="javascript:document.region.submit()" name="World">
                <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/
world.png" width="258" height="160" alt="
Rest of the World" />
            </a>
            <h1 style="color:black!IMPORTANT;">Enter Rest of the World site</h1>                    
            </div>  
    </form>

What do I need to put on every other template to check what language was selected? To help with the example if UK has been selected then it can just echo "UK", if the rest of the world was selected then it can just show "World".

This needs to work across several pages so if they goto the about page it checks the language, then if they navigate to the contact page it checks the language again - all that has to come from the initial language selection.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would drop the selection into a $_SESSION variable. That will stay with them until they leave. You could also use a cookie quite nicely. Actually a combination of the two would be great, it ties up users who don't allow cookies to run on a visit to visit basis, and folks who have cookies enables will only have to pick once.

Edit: Example of working code:

<form action="<?php the_permalink(); ?>/home" name="region" method="post">              
    <div id="uk">
    <a href="javascript:document.region.submit()" name="UK">
        <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/=uk.png" width="259" height="160" alt="UK" />
    </a>
    <h1 style="color:black!IMPORTANT;">Enter United Kingdom site</h1>
    </div>

    <div id="world">
    <a href="javascript:document.region.submit()" name="World">
        <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/world.png" width="258" height="160" alt="Rest of the World" />
    </a>
    <h1 style="color:black!IMPORTANT;">Enter Rest of the World site</h1>                    
    </div>  
</form>
// I assume that this form sends a POST request to the following page URL.

page that the form redirects to:

<?php

    session_start();
    if(isset($_POST['country'])) 
    // assuming that your form returns a field called 'country'...
    {
        $_SESSION['myCountry'] = htmlspecialchars($_POST['country']);
        // Assumes that myCountry is now 'UK' or 'World'...
    }
    header('Location: http://www.example.com/yourIndexPage.php');
    // You want to use a quick snippet to process the form and 
    // then redirect them away. This makes for less stupid BACK actions
    // in the browser as well as data being resent to the code.
?>

yourIndexpage.php

<?php

    // As the $_SESSION is now set, we can use it in the page as follows:
    session_start();
    switch($_SESSION['myCountry'])
    {
        case 'UK':
            echo $UK;
            // And anything else you want to do with the UK language.
            break;
        case 'World':
            echo $world;
            // etc etc etc...
            break;
        default:
            // echo out default stuff, probably 'World' at a guess.
            break;
    }

?>

If you are using wordpress you should probably read this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...