php编码逻辑按钮

时间:2013-01-31 17:10:39

标签: php

php code

if(isset($_POST['txtLocation']))
{
    $choice_loc = $_POST["txtLocation"];
}
elseif(!isset($_POST['txtLocation']))
{
    $message = "Please select the desired location or click on default";
}
elseif($choice_loc == "txtSetXY")
{
    $x = $_POST["txtXLocation"];
    $y = $_POST["txtYLocation"];
    if($x == "")
    {
        $message = "You forget to enter X location.";
    }
    elseif($y == "")
    {
        $message = "You forget to enter Y location.";
    }
    else
    {
        $choice_loc = $x . "," . $y;
    }
}

这是html表格

<div class="formText">
  <input type="radio" name="txtLocation" value="txtSetXY"/> Specify Location<br />
  <div style="padding-left:20px;">
       X: <input type="text" id="locField" name="txtXLocation">
       Y: <input type="text" id="locField" name="txtYLocation">
   </div>
   <input type="radio" name="txtLocation" value="Default" checked="checked"/>Default
</div>

逻辑中的错误是什么?

值&#34;默认&#34;进入数据库,但是当选择value="txtSetXY"无线电并在文本字段中输入x和y值时,它是否进入数据库?

这是我的数据库输入查询

$insert = "INSERT INTO dbform (dblocation) VALUES ('{$choice_loc}')";

2 个答案:

答案 0 :(得分:2)

您的测试无法进入第三种选择:

elseif($choice_loc == "txtSetXY")

由于

if(isset($_POST['txtLocation']))
{
...
}
elseif(!isset($_POST['txtLocation']))
{
...
}

涵盖所有可能的路径,可以替换为

if(isset($_POST['txtLocation']))
{
...
}
else
{
...
}

你会看到你无法添加另一个测试用例。

也许您应该尝试颠倒测试中的顺序:

if(isset($_POST['txtLocation']))
{
    $choice_loc = $_POST["txtLocation"];
}
elseif($choice_loc == "txtSetXY")
{
    $x = $_POST["txtXLocation"];
    $y = $_POST["txtYLocation"];
    if($x == "")
    {
        $message = "You forget to enter X location.";
    }
    elseif($y == "")
    {
        $message = "You forget to enter Y location.";
    }
    else
    {
        $choice_loc = $x . "," . $y;
    }
}
else
{
    $message = "Please select the desired location or click on default";
}

答案 1 :(得分:0)

在第一个逻辑部分,你的elseif有点过分了。您应该尝试以下方法:

if(!empty($_POST['txtLocation']))
{
    $choice_loc = $_POST["txtLocation"];
}
else
{
    $message = "Please select the desired location or click on default";
}
if(isset($choice_loc) && $choice_loc == "txtSetXY")
{
    if(!empty($_POST["txtYLocation"]))
        $y = $_POST["txtYLocation"];
    else
        $message = "You forget to enter Y location.";

    if(!empty($_POST["txtXLocation"]))
        $x = $_POST["txtXLocation"];
    else
        $message = "You forget to enter X location.";

    if(isset($x) && isset($y))
    {
        $choice_loc = $x . "," . $y;
    }
}