0

In my website files, I have 3 files, a file that has some basic PHP code such as collecting user data, and to make the code organized, I include the templates and the form files instead of writing their code in the same page.

So, I have the form included after the templates file. Now, after submitting the form, I wanna show the error code (if exists) but I cannot send it to the first page (that is included before it) because it will say Undefined variable. and I tried sending the variable using $_SESSION but it wasn't a good idea because even after refreshing the page, the error will stay.

So, I decided to make it with GET requests. (ex: http://website.com/form?status=failed&error=gender).

And the code that gets the GET value is:

$status = $_GET['status'];
$error = $_GET['error'];

$error1 = "You have to choose the gender.";

if ($status == "failed"){
    if ($error == "gender"){
        $message = $error1;
        echo '<div class="message">'.$message.'</div>';
    }
} else {
    echo '<h4 class="tab-info">Complete these steps in order to get posts related to your interests.</h4>';
}

In case that $status or $error were empty (http://website.com/form) not submitted yet, it will give the following error: Undefined index: status in **path**, how can this be solved?

Thanks in advance.

1 Answers1

1

You always have to code for the first time a page loads and the $_GET or $_POST values have not been set because the form has not been submitted or the page has been run without the required querystring.

So always check that the parameters exists before using them

$status = isset($_GET['status']) ? $_GET['status'] : '';
$error = isset($_GET['error'] ? $_GET['error'] : '';

This will make sure you dont attempt to use $_GET['status'] usless it actually exists and that your 2 variables a) exist and b) have some value even if it is a default like '' in this case

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149