0

我收到此错误消息:

注意:未定义的变量:第 126 行 /home/students/accounts///www/htdocs/jobassign1b/jobsprocess.php 中的 location1

错误信息,如果你看下面的 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>'; 



?>
4

1 回答 1

0

位置在$_POST?如果不是location1将不会被定义。

您可以添加此调整:

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

这将确保变量 location1 始终被定义,这意味着您将不再看到通知。

如果该位置未与表单一起提交,则可能是您使用form了 select 元素上的属性。

于 2013-10-01T01:57:45.293 回答