如果没有选择任何内容,PHP setcookie不会显示

时间:2017-01-31 03:04:57

标签: javascript php html setcookie

所以我想做什么以及我遇到了什么问题!

  1. 如果用户未选择货币,则不会显示“全部”!

  2. 如何编写代码,如果加载“all”,它将使用个人代码,如果是每种货币

  3. 如何删除<input type="submit" value="Select currency">按钮的使用,以便在选择货币时自动更改

  4. (在<body>内)

    那么如何删除<input type="submit" value="Select currency">按钮的使用,以便在选择货币时自动更改?

    <form action="currency_switcher.php" method="post">
        <select name="money">
            <option value="all"<?php if( $_COOKIE["currency"] == "all" ) { echo " selected"; } ?>>All Currencies</option>
            <option value="IDR"<?php if( $_COOKIE["currency"] == "IDR" ) { echo " selected"; } ?>>Indonesia RP</option>
            <option value="AUD"<?php if( $_COOKIE["currency"] == "AUD" ) { echo " selected"; } ?>>Australian Dollars</option>
            <option value="USD"<?php if( $_COOKIE["currency"] == "USD" ) { echo " selected"; } ?>>American Dollars</option>
            <option value="SGD"<?php if( $_COOKIE["currency"] == "SGD" ) { echo " selected"; } ?>>Singapore Dollars</option>
        </select>
    <input type="submit" value="Select currency">
    </form>

    因此,如果用户未选择货币,则“全部”未显示!

    如何编写代码,以便来自示例IDR的代码显示所有货币是否已加载,因此我不需要将代码写入两次!。

    <?php
    
        if( $_COOKIE["currency"] == "all") {
    
        echo "code";
        echo "code IDR";
        echo "code";
        echo "code AUD";
        echo "code";
        echo "code USD";
        echo "code";
        echo "code SGD";
        }
    
    
        if ( $_COOKIE["currency"] == "IDR" ) {
    
        echo "code IDR";
        }
    
        if ( $_COOKIE["currency"] == "AUD" ) {
    
        echo "code AUD";
        }
    
        if ( $_COOKIE["currency"] == "USD" ) {
    
        echo "code USD";
        }
    
        if ( $_COOKIE["currency"] == "SGD" ) {
    
        echo "code SGD";
        }
    
        ?>

    currency_switcher.php

    <?
    $money = "all";
    if( isset( $_POST["money"] ) ) {
        $money = $_POST["money"];
        setcookie ( 'currency', $money, time() + 60*60*24*30, '/', 'exampledomain.com');
        header('Location: ' . $_SERVER['HTTP_REFERER']);
        exit;
    }
    ?>

1 个答案:

答案 0 :(得分:0)

设置cookie示例:

echo 'Hello '.($_COOKIE['first_name']!='' ? $_COOKIE['first_name'] : 'Guest'); // Hello David!

获取cookie:

setcookie('first_name',$first_name,time() + (86400* 7),'/~sugar/','davidwalsh.name',true,true);

或者使用更具体的指令设置cookie,例如:

{{1}}