Best practice question about redirecting or returning to the calling script

Hello,

I have 2 standard scripts, one for user entry form and another script for the form processing script.

part of the form processing script checks whether the user correctly entered the information and if not returns back to the user entry form to displays error messages.

the question more or less is round the method im using to return back to the calling script, is it the right way or is there a better way to return back to the calling script for user correction.


if(count($fieldErrors) > 0)
{	$errorString = '<p>There was an error processing the form.</p>';
	$errorString . 'ul>';
	foreach($fieldErrors as $errorVal)
	{	$errorString .= "<li>$errorVal</li>"; }
		$errorString .= '</ul>';
	include ('../template/projectrequest.inc.php');	//user entry form	
}
else


Hi Robin,

I’d use the [fphp]header[/fphp] function to redirect back to the input page, like this:

header('Location: http://www.mydomain.com/form.php');
exit;

I’d avoid using the include method as you end up showing the user the same page with two different URLs, it might be a little confusing for the user and it’s not ideal from a SEO point of view.

Hi fretburner,

I tried your suggestion and it works the page does go back but there’s small problem. when it returns back to the calling script the user entered information is wiped out. I rather not have the user re-enter the information again… any thoughts on this?

Yeah there are a couple ways you could deal with this. One would be to store the form values and any error messages in session variables so you can get to them from your form page.

A simpler way would be to do the form processing on the same page as the form itself, something like this:


<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Do form processing here. If successful, redirect to another page, otherwise set your error variables.
}
?>

<form method="post">

    <label for="message">Your message:</label>
    <input type="text" name="message" value="<?php echo $form_values['message'] ?>">
    <p class="error"><?php $errors['message'] ?></p>

    // Etc....

</form>

(Not a complete example, but you get the idea.)

In this particular case this is not a problem and the include method (or any other that avoids a redirect) is the best one. The URL of the form page doesn’t change depending on whether the form is displayed the first time (when presented empty to the user) or pre-filled after the user submitted incorrect data. Having a separate URL for the empty form and for the pre-filled form is superfluous and search engines will not go the the latter anyway since its accessed by POST and not by a link.

After the user fills in the form correctly and is about to see another page - that is the good moment to use a redirect, because he leaves the form page and goes to another one.

I use includes and no redirects and I don’t have the two url’s per page problem.

A user wants to use a form: http://domain.com/path/to/form.php

There are two possibilities: he is logged in or not. If logged in he sees the form. If not he sees a form asking him to log in. If he logs in properly he sees the form. If not he gets some feedback. No change in URL.

I usually have a review screen before final submission. This, in fact, is another form with hidden fields and two buttons Edit, Submit, same URL as above.

The form is finally submitted and the user gets some feedback, same URL as above.

Everything that has to do with this form has the same URL. Semantically it makes sense to me. Search engines only see the log in form if a password is required and the suggestion is to exclude them with the robots tag and/or the robots text file.

Lemon Juice, fair point, I usually go with the method described in my second post which is basically what you’re recommending.

The situation described in the OP would result in two URLs with the same content, as it includes the content of the form page on the processing page.

As Lemon Juice points out, it’s not always a problem, but there are situations where you might have a form and some other content on the same page (perhaps some detailed instructions, for example) which you don’t want to have indexed twice. And even if the form method is POST, the URL of the second page could still be linked to from elsewhere and potentially crawled. It’s worth bearing in mind all the angles.

I don’t understand how you can have a “second page” if you have a single URL and no redirect. I realize you might have different content on the one page depending at what point in the processing it’s at.

I don’t redirect at all, I just spit out an error message telling what the user did wrong. I’ll explain

	 if ($result) {
		 $errorMsg .= 'Incorrect Password, try again....';
	 }

then if there are no errors then

if (!errorMsg) {
    // Process login and redirect then do it here if you ****want*****
}

then in the HTML section:

<h2><?php echo (isset($errorMsg)) ? $errorMsg : 'Please Login: '; } ?><h2>
<form action="login.php" method="post">'
//form continue....

Obviously there is more code containing if statements and such, but I think it’s easily figured out. This is how I go about do this, but I would say there really isn’t no ‘best’ practice in redirecting or returning a calling script. In my opinion it’s just how the person likes to code in order to get from point a to point b. Just my .02 cents.