2013-01-31 40 views
0

PHP代码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="txtSetXY"无线电并在文本字段中输入x和y值时,它不会进入数据库?

,这是我的数据库中输入查询

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

它停在第二个if语句处。 – rlatief

+0

@ user543732我们可以使用任何类型的PHP开发平台,在这里我们可以检查我们的编码或干运行,就像我们在turboC编译器中的c和C++一样?这样可以很容易地输出结果和错误? – Sumit

+0

有几个,但我不知道他们中的任何一个是否可以捕获这种错误。 '如果A' - 做A.' elseif不A''做B。它停在那里,永远不会到达它下面的每个'elseif'和'else'。正如你所说的一个逻辑错误,而不是PHP语法。 – rlatief

回答

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"; 
} 
+0

这意味着..第三选择elseif($ choice_loc ==“txtSetXY”)应该在第一选择内? if(isset($ _ POST ['txtLocation'])) – Sumit

+0

@Sumit否,它介于if(isset(...))和else之间。这样,如果位置已设置,它将采用默认位置,如果不是,则会检查是否输入了坐标,如果没有采用这些选项,则会引发错误消息。因此,在测试结束时,您可以使用txtLocation或x和y连接填充$ choice_loc,否则会收到错误消息。 –

+0

其实我输入的代码是不完整的..也就是说,它并不只包含此的if-else ..它是一系列的if else并检查其他许多领域,所以我不认为这钝其他 其他 {$ 消息=“请选择所需的位置或点击默认”; }将有助于.. – Sumit

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; 
    } 
} 
+0

我们的代码是把价值数据库.. concaneted一个也..但没有做空xy验证 – Sumit

+0

编辑:添加X&Y检查 – UnholyRanger

+0

我用这个代码,但再次..它是把x和y分贝..但不检查我是否让他们空置 – Sumit