$ _POST方法从数组中提取并显示结果

时间:2014-07-08 12:25:45

标签: php html arrays forms

所以我希望表单找到用户搜索的内容,然后通过下面的数组返回结果。这可能吗?我自己试着编写php,但我很无能为力。

            <form action="result.php" method="post">
            <input type="text" name="country" size="30" id="autocomplete-ajax" placeholder="Search for country, timezone or city and hit enter!" maxlength="30">
            </form>

                <?php
                $countries = array(   
                "AD" => "Andorra",   
                "AE" => "United Arab Emirates",   
                "AF" => "Afghanistan",
                "AG" => "Antiqua and Barbuda",
                "AI" => "Anguilla",
                "AL" => "Albania",          
                "ZZ" => "Unkown or Invalid Region");

                if (isset($_POST[$countries[0]])){
                echo "<p>Correct!</p>";
                }
                if (isset($_POST[$countries[1]])){
                echo "<p>Correct2</p>";
                }
                ?>

1 个答案:

答案 0 :(得分:0)

You could replace the other issets with this - it will check the post values against the array 

   if (isset($_POST["country"])){
        foreach($countries as $k => $c){
            if(strtolower($c) == strtolower(trim($_POST["country"]))){
                 echo "<p>Correct!</p>";
            }
        }
    }