2013-10-01 115 views
0

我已經收到此錯誤信息:收到錯誤消息選擇選項

注意:未定義的變量126

錯誤消息,如果你看看下面的PHP代碼,它來自於這條線

if (fwrite($jobTxt, $positionID . " " . $title . " " . $description . " " . $positionButton . " " . $contractButton . " " . $location1 . " " . $date . " " . $application . "\n")) 

我創建了一個選擇欄的位置,並在PHP中,我需要驗證和確保當用戶提交表單時,數據將進入文件目錄。

我真的堅持這一個,我已經嘗試了一切。 任何幫助將不勝感激。

這是選擇欄

<label for="location">Location:</label> 
      <select name="location" form="locationform"> 
      <option value="---">---</option> 
      <option value="act">ACT</option> 
      <option value="nsw">NSW</option> 
      <option value="nt">NT</option> 
      <option value="qld">QLD</option> 
      <option value="sa">SA</option> 
      <option value="tas">TAS</option> 
      <option value="vic">VIC</option> 
      <option value="wa">WA</option> 
      </select> 

代碼這是在PHP中的位置部分的代碼

<?php 
//Opening, and creating a path for diretories to create 
$newdir = "../../data/jobs2"; 
umask(0007); 

$errors = array(); //sets an error array, this will append to an error string 
$applicationArray = (isset($_POST['applicationby']) ? $_POST['applicationby'] : null); 


if (isset($_POST["positionid"])) 
{ 
    $positionID = $_POST["positionid"];   
    $patternID = "/^P[0-9]{4}$/";      //This regex ensure that the first letter is start with P following number 0-9 in length of 4.             
    if (preg_match($patternID, $positionID))  //check if the statuscode is matches with the regex 
    { 
     $ans = ""; 
     $length = strlen($positionID); 
     echo $positionID . "<br />"; 
    } 
} 
else 
{ 
    array_push($errors, "Please fill in Position ID as they are mandatory field");  //To ensure and detect if there is any error 
} 

if (isset ($_POST["title"])) 
{ 
    $title = $_POST["title"]; 
    $pattern = "/^[a-zA-Z\s\.,!]{0, 20}*$/";   //This regex ensure that alphanumeric character, space, comma, period, and exclamation mark, other symbols or characters are not allowed and maximum of 20 alphanumeric characters. 
    if (preg_match($pattern, $title))  //Check that the status if matches with the regex 
    { 
     echo $title . "<br />"; 
    } 
} 
else 
{ 
    array_push($errors, "<b>Error:</b> Please fill in Status as they are mandatory field!"); 
} 

if (isset ($_POST["description"])) 
{ 
    $description = $_POST["description"]; 
    $patternDescription = "^[a-zA-Z0-9._ \t]{1,260}$";  //This regex ensure that the description is set to maximum character is up to 260 
    if (preg_match('/' . $patternDescription . '/', $description))  //Check that the description is matches with the regex 
    { 
      echo $description . "<br />"; 
    } 
} 
else 
{ 
    array_push($errors, "<b>Error:</b> Please fill in Description as they are mandatory field!"); 
} 

if (isset ($_POST["position"]))   
{ 
    $positionButton = $_POST["position"]; 
    echo $positionButton . "<br />"; 
} 
else 
{ 
    //Not possible unless in exceptional circumstances 
    array_push($errors, "Please choose the position"); 
} 

if (isset ($_POST["contract"]))   
{ 
    $contractButton = $_POST["contract"]; 
    echo $contractButton . "<br />"; 
} 
else 
{ 
    //Not possible unless in exceptional circumstances 
    array_push($errors, "Please choose the contract"); 
} 

if (isset($_POST['location'])) 
{ 
    $location1 = $_POST['location']; 
    echo $location1 . "<br />"; 
} 
else 
{ 
    array_push($errors, "please choose the location"); 
} 

if (isset($_POST["date"]))   
{ 
    // writing the date format 
    $date = date("d/m/y");   
    echo $date . "<br />"; 
} else { 
    //$date = $_POST["date"]; 
} 

if (isset($applicationArray)) 
{ 

    //Display no "array" name, by creating a new variable name 
    foreach($applicationArray as $application) 
    { 
     echo $application . "<br />"; 
    } 
} 
//check if file exist 
if(!file_exists($newdir)){ 
//create a directory 
    mkdir($newdir, 02770); 
} 

$jobTxt = fopen($newdir. "/jobs2.txt", "a+"); 
// This is to ensure that the file is 
if (is_writeable($newdir. "/jobs2.txt")) { 
    //write the following variable 
    if (fwrite($jobTxt, $positionID . " " . $title . " " . $description . " " . $positionButton . " " . $contractButton . " " . $location1 . " " . $date . " " . $application . "\n")) 
{ 
} 
    echo "<p>Your form has succesfully been submit!</p>"; 
} 
fclose($jobTxt); 



if(isset($positionID, $title)) 
{ 
//Validation Check 

    //Check if characters length is less or more than 5 characters 
    if (0 === strlen($positionID > 5 || $positionID < 5)) 
    { 
     array_push($errors, "<b>Error:</b> Your characters length is either less or more than 5 characters<br/>"); 
    } 



     //Check if characters length is less or more than 20 characters 
    if (0 === strlen($title > 20)) 
    { 
     array_push($errors, "<b>Error:</b> Your characters length is more than 20 characters, the maximum number is 20 alphanumeric characters!<br/>"); 
    } 



    //Check if Position ID is left not blank 
    if (0 === preg_match("/\S+/", $positionID)) 
    { 
     array_push($errors, "<b>Error:</b> You forgot to fill in the Position ID!<br />"); 
    } 



    //Check if Title is left not blank 
    if (0 === preg_match("/\S+/", $title)) 
    { 
     array_push($errors, "<b>Error:</b> You forgot to fill in the Title! <br />"); 
    } 


    //Check if user made a mistake with typo 
    if (0 === preg_match($patternID, $positionID)) 
    { 
     array_push($errors, "<b>Error:</b> please make sure that the first letter in Status Code is uppercase 'P' following by 4 numbers. <br />"); 
    } 



    //Remind user to avoid the unnecessary symbol 
    if (0 === preg_match($pattern, $title)) 
    { 
     array_push($errors, "<b>Error:</b> Please make sure to avoid symbols other than \",.!\" <br />"); 
    } 
} 


if (isset($errors)) 
{ 
    //Display no array name by changing the variable 
    foreach ($errors as $error) 
    { 
     echo '<strong>', $error, '</strong>'; 
    } 
} 
//provide back to form page link 
echo '<a href="jobpostform.php">Back to Form page.</a> <br />'; 
//provide back to home page link 
echo '<a href="index.php">Back to Home page.</a>'; 



?> 
+0

如果'$ location1'是不確定的,這意味着在你的PHP代碼段的前幾行不執行 - 即。 '$ _POST ['location']'沒有設置。你可以發佈你的代碼的其餘部分,因爲可能有其他的東西干擾表單提交。 – xiankai

+0

我已經發布了代碼的其餘部分,感謝在看看代碼,我還在努力想出來什麼問題 –

回答

0

是位置$_POST?如果不是location1將不會被定義。

您可以添加此調整:

$location1 = '---'; 
if (isset($_POST['location'])) { 
    $location1 = $_POST['location']; 
} 

這將確保變量LOCATION1總是被定義,將意味着你將不再看到該通知。

如果該位置不被與申報時,它可能會下降到您的選擇元素上使用form屬性。