Php 收到选择选项的错误消息

Php 收到选择选项的错误消息,php,forms,file,validation,select,Php,Forms,File,Validation,Select,我已收到此错误消息: 注意:未定义变量:第126行/home/students/accounts///www/htdocs/jobsassign1b/jobsprocess.php中的location1 错误消息,如果您查看下面的php代码,它出现在这一行 if (fwrite($jobTxt, $positionID . " " . $title . " " . $description . " " . $positionButton . " " . $contractButton . " "

我已收到此错误消息:

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

错误消息,如果您查看下面的php代码,它出现在这一行

if (fwrite($jobTxt, $positionID . " " . $title . " " . $description . " " . $positionButton . " " . $contractButton . " " . $location1 . " " . $date . " " . $application . "\n"))
我已经为location创建了一个select字段,在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>'; 



?>

是否位于
$\u POST
中?如果不是,则不会定义位置1

您可以添加此调整:

$location1 = '---';
if (isset($_POST['location'])) {
    $location1 = $_POST['location'];
}
这将确保始终定义变量location1,这意味着您将不再看到通知


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

如果
$location1
未定义,则意味着您的PHP代码段中的前几行未执行-即未设置
$\u POST['location']
。你能把剩下的代码贴出来吗,因为可能还有其他东西干扰了表单的提交。我已经把剩下的代码贴出来了,谢谢你看了代码,我还在努力找出问题所在