dongse5528 2013-08-06 10:50
浏览 45
已采纳

PHP if / else和AND / OR来检查强制和可选字段

I have eight input fields in my form that I need to check whether they are empty. Four of these are text fields, the other four are list boxes. When the user submits the form, the four text boxes need to be filled. The fifth field is a Yes/No selection. If the selection in this field is "No", then the remaining three can be left blank. However, if the selection in the fifth field is "Yes" then the last three also have to be selected using the list boxes.

Let us call the first four (textboxes) "mandatory-text1", "mandatory-text2", "mandatory-text3", and "mandatory-text4", the Yes/No selection list box "yes-no" and the optional selections "a", "b" and "c".

SO:

If mandatory-text1, or mandatory-text2, or mandatory-text3, or mandatory-text4, are blank, the processing php will abort.

However, if mandatory-text1, mandatory-text2, mandatory-text3, and mandatory-text4, are all filled in, the next check is to see whether the yes-no contains a "Yes" or a "No".

  • If it contains a No, then a, b and c have to be necessarily left blank.
  • If it contains a Yes, then a, b and c (list boxes) have to be necessarily selected.

With my level of proficiency in PHP, I have been able to reach this far:

    if ((mandatory-text1 || mandatory-text2 || mandatory-text3 == "") OR (yes-no == "Yes" AND (!(a || b || c  == ""))) {
    echo "blah" ;
    exit();
    }
    else {
    //do some stuff
    }

I haven't coded the 'do some stuff' part yet because when I try this code, I get the 'blah' message even when all the text fields are filled in, the yes-no is "Yes" and a, b and c have been selected.

I tried it first with a simple "||" for all the fields and that works fine - i.e. if even one of the fields is left blank I get the 'blah' message. I have also tried just checking for the yes-no and a,b,c and that too works fine i.e. if there is a Yes in the yes-no i get the blah if the a,b,c fields are blank.

But I need to satisfy all the conditions before I can proceed to the next step. The posts I have read here on SO brought me to the stage I am in right now. But none went to the level I want for my project.

Any hints for the logic would be appreciated!

  • 写回答

4条回答 默认 最新

  • duanfen9983 2013-08-06 10:57
    关注

    Okay first of all, you are supposed to use $ before variable names

    Second, mandatory-text1||mandatory-text2||mandatory-text3=="" is wrong logic. It should be $mandatory-text1=""||$mandatory-text2==""|| ...

    It works because if(mandatory-text1) alone means if mandatory-text1 is defined and not empty or null string, so in this case $mandatory-text1||$mandatory-text2||$mandatory-text3 alone is sufficient

    if ((mandatory-text1 || mandatory-text2 || mandatory-text3 == "") OR (yes-no == "Yes" AND (!(a || b || c  == ""))) {
    echo "blah" ;
    exit();
    }
    else {
    //do some stuff
    }
    

    same for a||b||c

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路