开发者

Setting PHP cookie from dropdown menu

I have a PHP script designed to allow users to decide which language a page is displayed in. The information is stored in a cookie and then read when needed to display the correct content.

Currently, I use an HTML dropdown box to allow the user to select 开发者_运维问答the language and then they must press the form submit button to set the cookie. How can I make it so when they select the language in the dropdown menu it automatically selects that and submits the form? I hope you can understand my question.

My current PHP code is:

<?php
$user_lang = null;
if (isset($_POST["setc"])) {
    $expire = time() + 60 * 60 * 24 * 30;
    setcookie("mycookie", $_POST["sel"], $expire);
    header("location: " . $_SERVER["PHP_SELF"]);
} else if (isset($_COOKIE["mycookie"])) {
    $user_lang = $_COOKIE["mycookie"];
}
?>

<meta charset='utf-8'>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
    <select name="sel" size="1">
        <option value="en"<?php echo (!is_null($user_lang) && $user_lang === "en" ? " selected" : ""); ?>>English</option>
        <option value="es"<?php echo (!is_null($user_lang) && $user_lang === "es" ? " selected" : ""); ?>>Español</option>
        <option value="fr"<?php echo (!is_null($user_lang) && $user_lang === "fr" ? " selected" : ""); ?>>Français</option>
        <option value="de"<?php echo (!is_null($user_lang) && $user_lang === "de" ? " selected" : ""); ?>>Deutsch</option>
    </select>
    <input name="setc" value="save setting" type="submit">
</form>


Change your select tag to:

<select name="sel" size="1" onchange="this.form.submit();">

to make the form submit when users selects a language.

If you wish to do that without JavaScript you can use multiple submit buttons method instead:

<input name="set_language[en]" value="English" type="submit">
<input name="set_language[es]" value="Español" type="submit">
<input name="set_language[fr]" value="Français" type="submit">
<input name="set_language[de]" value="Deutsch" type="submit">

Processing this form is simple as it is:

if (isset($_POST["set_language"])) {
    $language = key($_POST["set_language"]);
    // $language contains user-selected language code now
}

This can't be done with a select field but without JavaScript or any other client-side scripting language.


You need to do it using JavaScript:

<select name="sel" size="1" onchange="this.form.submit();">

Or if you use jQuery:

$("select[name='sel']").change(function() {
    $(this).parent().submit();
});

This will submit the form whenever a user changes value in the dropdown list.


You can add an onchange event to the select:

<select onChange="document.forms[0].submit();">

Remember that it's a good thing to leave the submit button for those users who don't have javascript enabled. You can hide the button using javascript, so it won't clutter the window of those that doe have a regular browser.


I think you have to use javascript like the others said, but you can also use the submit button as a backup if the user doesn't have javascript activated:

<noscript><input value="save setting" type="submit"></noscript>

And then, since the submit button will only be shown when the user has javascript disabled, you will need to add a hidden input to check if the form has been submitted on the server side

<input type="hidden" name="setc" value="true" />


The answer is in JavaScript not PHP. You could do it unobtrusively with JavaScript like this:

<script>
    window.onload = function() {
        document.getElementById("idOfDropDown").addEventListener("change", function() {
           document.forms["formName"].submit();
        });
    }
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜