0

php代码

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>

逻辑有什么错误??

value "default" is entered into database, but when selected value="txtSetXY"radio and entering x and y values in textfields it is not entering into database?

这是我的数据库输入查询

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

2 回答 2

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";
}
于 2013-01-31T17:12:46.247 回答
0

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

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;
    }
}
于 2013-01-31T17:15:13.867 回答