php if elseif然后是什么?

时间:2013-02-09 12:48:24

标签: php

这是我的PHP代码

我在代码部分的评论中提到的问题点

if(isset($_POST['submit']))
{
    if(isset($_POST['rdoption1']))
    {
        $var1 = $_POST["rdoption1"];
    }
    if(isset($_POST['rdoption2']))
    {
        $var2 = $_POST["rdoption2"];
    }
    if(!isset($_POST['rdoption1']))
    {
        $message = "Please select Option1";
    }
    elseif(!isset($_POST['rdoption2']))
    {
        $message = "Please select Option2";
    }
    elseif($_POST['rdoption2'] == "checkSetXY")
    {
        if($_POST["valXLocation"] == "")
        {
            $message = "You forget to enter X value.";
        }
        elseif($_POST["valYLocation"] == "")
        {
            $message = "You forget to enter Y value.";
        }
    } // till here all is good. I get all error messages if anything is left vacant or not clicked on radio button
    elseif(empty($_POST['txtoption3'])) //this is not working //the issue is if i select rdoption1  any option and rdoption2 checkDefault next code logic work.. but next code logic does not work when i click on the radio of checkSetXY and enter x and y values.. It simply does not execute code further..
    {
        $message = "Please enter your name.";
    }
                else
    {               
        //insert into db
                 }
       }

这是带有PHP echos的html表单 在这里,我会收到消息,但不是在我选择checkSetXY

        <?php if(!empty($message)){ echo $message; } ?>

        <form id="form1" name="form1" method="post" action="form1.php">

          Space portion:
              <input type="radio" name="rdoption1" value="RJ"/>space 1
              <input type="radio" name="rdoption1" value="SM" />space 2


            Pixel Location
                <div class="formText">
                <input type="radio" name="rdoption2" value="checkSetXY"/> Specify Location
                    X: <input type="text" id="locField" name="valXLocation">  
                    Y: <input type="text" id="locField" name="valYLocation"> 
                <input type="radio" name="rdoption2" value="checkDefault"/>Default

                <input type="text" class="input" name="txtoption3">
                <input type="submit" name="submit" value="Submit"> 
           </form>

现在我很困惑,为什么不采用elseif

txtoption3

有任何帮助吗?提前致谢

3 个答案:

答案 0 :(得分:3)

这不起作用,因为它总会被设置。因此,请使用empty();

elseif(empty($_POST['txtoption3'])) //this is not working

<强>解释

您正在发布表单input。当您发送它而不填充任何内容时,它只发送此值。 ""

空字符串不等于null未设置

此外,正如Peter Szymkowski所述,请查看fiddle

答案 1 :(得分:1)

如果Textfields为空,则设置它们。您必须查看empty($_POST['txtoption3'])

答案 2 :(得分:-1)

这是其他的 if($ _ POST ['rdoption2'] ==“checkSetXY”)

我猜这是真的所以它不会进入那个