I am a little lost, I am trying to place a link to go back to the "form" page or "home" page for every error message when user submit the form. As well when user succesfully submit the form, it gives link to go back to form page or home page. Basically I want a fix link return to form or home page. Thank you guys, please let me know what i could do or guide me.
This is my code so far for the process part:
<?php
//
//umask(0007)
//mkdir($newdir, 02770);
//$filename = "../../data/status.txt";
$errors = array();
$permissionsArray = (isset($_POST['permission']) ? $_POST['permission'] : null);
if (isset($_POST["statuscode"]))
{
$statusCode = $_POST["statuscode"];
$patternCode = "/^S[0-9]{4}$/";
if (preg_match($patternCode, $statusCode))
{
$ans = "";
$length = strlen($statusCode);
echo $statusCode . "<br />";
}
}
else
{
array_push($errors, "Please fill in Status Code as they are mandatory field");
}
if (isset ($_POST["status"]))
{
$status = $_POST["status"];
$pattern = "/^[a-zA-Z0-9\s\.,!?]*$/";
if (preg_match($pattern, $status))
{
echo $status . "<br />";
}
}
else
{
array_push($errors, "<b>Error:</b> Please fill in Status as they are mandatory field!");
}
if (isset ($_POST["share"]))
{
$shareButton = $_POST["share"];
echo $shareButton . "<br />";
}
else
{
//Not possible unless in exceptional circumstances
array_push($errors, "Please choose a share");
}
if (!isset($_POST["date"]))
{
$date = date("d/m/y");
echo $date . "<br />";
} else {
$date = $_POST["date"];
}
if (isset($permissionsArray))
{
foreach($permissionsArray as $permission){
echo $permission . "<br />";
}
}
if(isset($statusCode, $status))
{
if(empty($statusCode) || empty($status))
{
array_push($errors, "Please fill in the required part!");
}
if (0 === strlen($statusCode > 5 || $statusCode < 5))
{
array_push($errors, "<b>Error:</b> You characters length is either less or more than 5 characters<br/>");
}
if (0 === preg_match("/\S+/", $statusCode))
{
array_push($errors, "<b>Error:</b> You forgot to fill in Status Code!<br/>");
}
if (0 === preg_match("/\S+/", $status))
{
array_push($errors, "<b>Error:</b> You forgot to fill in the Status! <br/>");
}
if (0 === preg_match($patternCode, $statusCode))
{
array_push($errors, "<b>Error:</b> please make sure that the first letter in Status Code is uppercase 'S' following by 4 numbers. <br/>");
}
if (0 === preg_match($pattern, $status))
{
array_push($errors, "<b>Error:</b> Please make sure to avoid symbols other than \",.?!\" <br/>");
}
}
if (isset($errors))
{
foreach ($errors as $error)
{
echo '<strong>', $error, '</strong>';
}
}
?>
You can use a link to invoke history.go(-1)
in Javascript, which is actually the same as clicking the back button in your browser.
For example in PHP (your file) that would be: echo "<a href=\"javascript:history.go(-1)\"> GO BACK </a>";
Edit:
So strictly PHP? Try this then:
header('Location: ' . $_SERVER['HTTP_REFERER']);
Use this one
$ref = $_SERVER['HTTP_REFERER'];
header("Location:".$ref);
$_SERVER['HTTP_REFERER'] gives you the URL from where you are redirected.