doukuiqian5345 2014-10-25 01:40
浏览 15
已采纳

PHP脚本不显示字段

I have a PHP script that is processing some form data (by posting back to itself) and displaying the relevant form data in the form along with the resulting calculations. The relevant pieces of code that I'm having trouble with are shown below:

First bit of code:

   if ($_POST["country"] == "select") { 
    $formResponse = "Some data was incorrectly entered or missing. Please try again.";
    }
    else {
    $country = ($_POST["country"]);
}

Second bit of code is as follows. Note that my intent is to trigger an error message if data entered into the zip field is not a 5-digit zip code:

   if (empty($_POST["zip"])) { 
    if ($country == "United States") {
        $formResponse = "Some data was incorrectly entered or missing. Please try again."; 
        } 
        else {
            if ($country == "United States") {
                    $zipCode = test_input($_POST["zip"]);
                    if (!preg_match("/^[0-9]{5}$/",$zipCode)){
                        $formResponse = "Some data was incorrectly entered or missing. Please try again.";
                        //$zip="";
            }
        }
    }
}

Relevant code in form for zip code:

<input id="zip" type="text" name="zip" placeholder="Enter 5-digit zip code" value="<?php echo $zipCode;?>" />

With respect to the processing of form data, the zip code isn't working correctly:

  1. The if(empty(($POST["zip"]); piece of code works correctly and throws the correct error upon form submission.
  2. My RegEx doesn't seem to work as it doesn't throw an error if data of any kind is put into the zip field and suggests that all data has been properly submitted.
  3. On a "successful" processing, the $zipCode field is not being displayed.

Clearly, I've got something wrong with my logic / expression, but I've been looking at this for a while. If anyone has some suggestions, it would be appreciated.

Cheers! Mike

  • 写回答

1条回答 默认 最新

  • douhuang1852 2014-10-25 01:46
    关注

    Let's format the code and see what we're dealing with:

    if (empty($_POST["zip"])) {
        if ($country == "United States") { //WHAT? Then if you're from US you're always going to hit this part
            $formResponse = "Some data was incorrectly entered or missing. Please try again.";
        } else {
            if ($country == "United States") { //this is never going to hit because of that top part
                $zipCode = test_input($_POST["zip"]);
                if (!preg_match("/^[0-9]{5}$/", $zipCode)) {
                    $formResponse = "Some data was incorrectly entered or missing. Please try again.";
                    //$zip="";
                }
            }
        }
    }
    

    Hope this helps you, you need to remove that top part really...

    if (empty($_POST["zip"])) { //you can add && $country == "United States" here if you want for exclusive logic, like if other countries don't *require* the zip
        $formResponse = "Some data was incorrectly entered or missing. Please try again.";
    } else {
        if ($country == "United States") {
            $zipCode = test_input($_POST["zip"]);
            if (!preg_match("/^[0-9]{5}$/", $zipCode)) {
                $formResponse = "Some data was incorrectly entered or missing. Please try again.";
                //$zip="";
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名