0

第一个之后的任何进一步提交都不会保存并在目录中创建新行

任何帮助或教学将不胜感激!

下面是表单代码,后面是php代码

<body>

<?php
      if (!isset ($_POST["date"])){
        $date = date("d/m/y");
        }else{
        $date = $_POST["date"];
        }
?>

    <div id="formWrapper">
        <form action="jobsprocess.php" method="POST">
        <fieldset class="first">
            <h1>Job Vacancy System</h1>
            <label class="labelOne" for="positionid">Position ID:</label>
            <input type="text" name="positionid" id="positionid" />

            <br />

            <label for="title">Title:</label>
            <input type="text" name="title" id="title" />

            <br />

            <label for="description">Description:</label>
            <textarea name="description" id="description" rows="5" cols="25"></textarea>

            <br />

            <label for="date">Closing Date:</label>
            <input type="text" name="date" id="date" value="<?php echo $date?>" />

            <br />

            <label for="position">Position:</label>
            <input type="radio" name="position" value="full time">Full Time
            <input type="radio" name="position" value="part time">Part Time

            <br />

            <label for="contract">Contract</label>
            <input type="radio" name="contract" value="on-going">On-going
            <input type="radio" name="contract" value="fixed term">Fixed Term

            <br />

            <label for="applicationby">Application by:</label>
            <label for="post">Post</label>
            <input type="checkbox" name="applicationby[]" id="post" value="Post" />
            <label for="mail">Mail</label>
            <input type="checkbox" name="applicationby[]" id="mail" value="Mail" />

            <br />

            <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>
            </fieldset>

            <fieldset>
            <input class="btn" name="post" type="submit" value="Post">
            <input class="btn" name="reset" type="reset" value="Reset">
            </fieldset>
            <fieldset>
            <a class="home" href="index.php">Return to Homepage</a>
            </fieldset>

        </form>

</body>

下面是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"]))
{
    $location = $_POST["location"];
    echo $location . "<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 . " " . $location . " " . $date . " " . $applicationby . "\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 'S' 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

检查文件是否存在时缺少一个结束括号:

if (!file_exists($newdir)) {
    mkdir($newdir, 02770);
} // <-- Missing closing bracket.

$jobTxt = fopen($newdir . "/jobs2.txt", "a");
if (is_writeable($newdir . "/jobs2.txt")) {
    if (fwrite($jobTxt, $positionID . " " . $title . " " . $description . " " . $positionButton . " " . $contractButton . " " . $location . " " . $date . " " . $applicationby . "\n")) {
        // I also believe that you want this in the if statement, not outside
        echo "<p>Your form has succesfully been submit!</p>";
    }

}
fclose($jobTxt);

现在你只有在目录不存在时才做所有事情。

于 2013-09-30T21:47:21.147 回答