I am brand new to PHP and I am attempting to recreate a PDF in the shape of a PHP page.
Essentially, the user has to provide input in a number of fields, and then upon submitting, if the form validates successfully, it passes the html form to a new php page in a new window.
If it does not validate then the page reloads and indicates which fields need to be adjusted. This is the form:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" name="form">
What I have right now, is that when the form is submitted, it passes the form to itself:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["schoolIn"])) {
$schoolErr = "School Name is required";
} else {
if (!preg_match("/^[a-zA-Z ]*$/",$_POST["schoolIn"])) {
$schoolErr = "Only letters and white space allowed";
}
else{
$school = $_POST["schoolIn"];
}
}
/*the rest of the validation goes here */
This seems to work great, however I am unsure how to submit the form to a new PHP document if all of these checks validate successfully.
On the form submission button, is there any way I can have some sort of conditional that checks if the form validates, and if so, submits it again to a new PHP file (say "submitted.php)? or am I thinking about this all wrong.
Method 1:
just pass value as a query parameter like this
header('Location:submitted.php?schoolIn='.$_POST['schoolIn']);
Access the value on destination page using $_GET['schoolIn'];
Method 2:
you can store the post values into session and access it in anywhere .
$_SESSION['form_values']=$_POST;
Access the value on destination page like this
$_SESSION['form_values']['schoolIn'];
Note: Don't forgot to start the session start on wherever your using session .