Reputation: 2588
I have a form that submits many different fields filled out by the user.
A PHP page receives the form and checks if all fields are filled. If one of them is missing, it should return to the form and specify which one is missing.
I was wondering what the best way is to pass all the fields caught in my PHP site back to the form page so the user does not have to re-fill the whole form.
Here is how I check:
if (empty($firsName) || empty($lastName) || empty($address) || empty ($city) || empty ($state) || empty($zip))
{
header('Location: checkout.php');
}
I don't want to pass them through URL since there might be many other fields to be checked and it seems a bit heavy... which way is the usual to achieve this?
Thanks!
Upvotes: 0
Views: 1240
Reputation: 1413
Try something like below
$validate_fields_arr = array('firsName', 'lastName', ...)
$returntourl = false;
foreach($validate_fields_arr as $v){
if(empty($_POST[$v]))
$returntourl = true;
}
if($returntourl){
$return_str = '<html><title>ERROR!</title><body><h3>ERROR on data submitted!</h3>';
$return_str .= '<form action="checkout.php" method="post" name="retfrm">';
foreach($_POST as $k=>$v){
$return_str .= '<input type="hidden" name="'.$k.'" value="'.$v.'">';
}
$return_str .= '</form>';
$return_str .= '</body><script>document.retfrm.submit()</script></html>';
echo $return_str;
exit;
}
Upvotes: 0
Reputation: 3127
The best way if you're not sure how to do it is to use a framework. If that's not an option, then I usually have a function that checks if there's a value that has been posted, and prints it out where necessary:
function getElementValue($elementName)
{
if (isset($_POST[$elementName]))
{
return htmlspecialchars($_POST[$elementName]);
}
return '';
}
And in your page:
<input type="text" name="firstName" value="<?php echo(getElementValue('firstName')); ?>" />
You'll need a similar, but different function for checkboxes etc.
Upvotes: 1
Reputation: 23009
Save the variables in a session, then on the form page, echo out the session variables. You can also pass back error messages that way, by creating another session variable like $_SESSION['error_msg'], and adding to it.
session_start();
$errors = array();
if(empty($firstName)){
$errors[] = 'Please enter your first name';
}
if(empty($lastName)){
$errors[] = 'Please enter your last name';
}
if(empty($address)){
$errors[] = 'Please enter your address';
}
if(empty($city)){
$errors[] = 'Please enter your city';
}
if(empty($state)){
$errors[] = 'Please enter your state';
}
if(empty($zip)){
$errors[] = 'Please enter your zip code';
}
if(!empty($errors)){
$_SESSION['firstName'] = $firstName;
$_SESSION['lastName'] = $lastName;
$_SESSION['address'] = $address;
$_SESSION['city'] = $city;
$_SESSION['state'] = $state;
$_SESSION['zip'] = $zip;
$_SESSION['errors'] = $errors;
header('Location: checkout.php');
exit;
}
Upvotes: 0
Reputation: 72
why don't you just check if they are empty before posting to the other page if they're all good redirect else, display the error
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Anyways the answer to your question
you have a few choices, you could store them individually into session or store them as an array into session and just unset the index after you've retrieved everything
you could pass via GET parameters though seems lengthy and get isn't the safest thing to use, as for it being heavy you won't notice any visible difference in the page load time and shouldn't be too concerned with the way the url looks
if you have a database you could just store it in a table and read&delete it upon the next page load
Upvotes: 0