2013-01-31 69 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