2017-08-17 91 views
-1

首先,我是編碼和PHP的新手,我試圖創建一個用戶可以編輯其用戶配置文件的配置文件頁面。PHP在if語句中嵌套elseif語句

表單已創建,但我無法確保填寫正確的字段。從我學到的東西和我的腦海中,在「if」語句中嵌套「elseif」語句將是完成我在尋找的最合乎邏輯的方式,但顯然有些東西無法正常工作,因爲當我刷新時頁面被破壞。任何幫助將不勝感激。

if (isset($_POST['btn-submit'])){ 
    if($firstName == NULL) { 
     $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Please enter your first name to continue!</div>"; 
    } 
    elseif ($lastName == NULL){ 
     $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Please enter your last name to continue!</div>"; 
    } 
    elseif ($txtuname == NULL){ 
     $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Please enter your username to continue!</div>"; 
    } 
    elseif ($txtemail == NULL){ 
     $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Please enter your email to continue!</div>"; 
    } 
    elseif ($busName == NULL) { 
     $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Please enter your business name to continue!</div>"; 
    } 
    elseif ($busEmail == NULL) { 
     $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Please enter your business email to continue!</div>"; 
    } 

    $firstName = trim($_POST['fname']); 
    $lastName = trim($_POST['lname']); 
    $txtuname = trim($_POST['uname']); 
    $txtemail = trim($_POST['email']); 
    $busName = trim($_POST['busname']); 
    $busEmail = trim($_POST['busemail']); 
    $busAddress = trim($_POST['busaddress']); 
    $busSuite = trim($_POST['bussuite']); 
    $busCity = trim($_POST['buscity']); 
    $busState = trim($_POST['busstate']); 
    $busZip = trim($_POST['buszip']); 

    $msg = "<div class='alert alert-success'><button class='close' data-dismiss='alert'>&times;</button> You are finally on the right path!</div>"; 
} 
else { 
    $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Something is really screwed up!</div>"; 
} 
+0

你是什麼意思「的頁面被打破,當我刷新」?現在,如果'isset($ _ POST ['btn-submit'])'爲''',$ msg將始終有最後一個值。您將覆蓋它,因此您在if/elseif鏈上分配的值不會保持不變。此外,您在分配變量之前正在執行這些檢查。 – ishegg

+0

我在瀏覽器中收到「此頁面無法正常工作」錯誤。 – user2507414

回答

0

看起來你,你在ELSEIF測試之前分配的變量。

試試這個:

if (isset($_POST['btn-submit'])){ 
    $firstName = trim($_POST['fname']); 
    $lastName = trim($_POST['lname']); 
    $txtuname = trim($_POST['uname']); 
    $txtemail = trim($_POST['email']); 
    $busName = trim($_POST['busname']); 
    $busEmail = trim($_POST['busemail']); 
    $busAddress = trim($_POST['busaddress']); 
    $busSuite = trim($_POST['bussuite']); 
    $busCity = trim($_POST['buscity']); 
    $busState = trim($_POST['busstate']); 
    $busZip = trim($_POST['buszip']); 

    if($firstName == NULL) { 
     $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Please enter your first name to continue!</div>"; 
    } 
    elseif ($lastName == NULL){ 
     $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Please enter your last name to continue!</div>"; 
    } 
    elseif ($txtuname == NULL){ 
     $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Please enter your username to continue!</div>"; 
    } 
    elseif ($txtemail == NULL){ 
     $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Please enter your email to continue!</div>"; 
    } 
    elseif ($busName == NULL) { 
     $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Please enter your business name to continue!</div>"; 
    } 
    elseif ($busEmail == NULL) { 
     $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Please enter your business email to continue!</div>"; 
    } 

    $msg = "<div class='alert alert-success'><button class='close' data-dismiss='alert'>&times;</button> You are finally on the right path!</div>"; 
} 
else { 
$msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Something is really screwed up!</div>"; 
}