Forum Moderators: coopster

Message Too Old, No Replies

include file in php script

         

Kysmiley

1:35 am on Nov 27, 2004 (gmt 0)

10+ Year Member



Ok now i have another question, with regards to this suggestion box form
I want to now set my error messages to follow the theme of the site. This would mean to have an include header.inc and footer.inc with the message in the div.content section in the middle.
Is there an easy way to include this in a php script without coming out of php them back in for the include file then out then in for the next part of the form
here is an error message that just goes to a plain white page.

if (!isset($_POST['suggestion']) ¦¦ empty($_POST['suggestion'])) $errmsg .= "I'm sorry $name it seems like you have forgotten to include your comments in the form";

I have five different error messages depending on section not filled in, in the form. Or would it be better to set five different error message names like $errmsg1 = url(errmsg1.php);
or somthing to that effect. Sorry I still dont think like a programer and this is the only way I can think of stating what I would like to try

Salsa

4:40 am on Nov 27, 2004 (gmt 0)

10+ Year Member



Pat, are you using using one script to display your form, validate it, and process a successful result, or are you doing it with multiple files?

I think that the most cohesive way to do it is to use just one script containing functions for each part of the process, like form_display(), form_validation(), and process_success(). That way you can use a series of switches to choose the appropriate function, with the default being form_display().

Once the form is submitted by a visitor, it would go to form_validation(). If it validates, you would then call process_success(). But if it doesn't validate, the form_validation() function would set your error messages and again call form_display(), passing back to it the data that the visitor had entered (so he doesn't have to renter it), plus the error messages.

In your form validation function, you can do something like:

$errors = 0; 
if(!/*suggestion validation stuff*/) {
$suggestion error = "<span style=\"color:red;\">I'm sorry....</span><br>\n";
$errors++;
}
...

Then at the end of form_validation(), do something like:

if ($errors > 0) form_display(); 
else process_success();

On failure, a nice way to display the errors is to put them immediately above whatever form field they pertain to. For example, back on your form, immediately above the "suggestion" field, put:

echo $suggestion_error;

Of course, if $suggestion_error is empty, it will echo nothing at all, so it will never display unless $suggestion was a cause of an invalid submission. If $errors > 0, you might also want to set a more generalized error message to put at the top of the form.

Also use the values from the original submission in the form, like:

<textarea cols=40 rows=3 name="suggestion"><? echo $suggestion?></textarea>

Again, because $suggestion will be empty on a first visit to the form, no content will be displayed until the form has been submitted at least once. Of course, at the top of your form_display() function you will want to initialize all of your form variables for when it is first visited, like:

if (!isset($suggestion)) $suggestion = ""; 
if (!isset($suggestion_error)) $suggestion_error = "";

Overall, I think this might give you the consistency and professional appearance that you want.

I wish you well,

Kysmiley

12:53 pm on Nov 27, 2004 (gmt 0)

10+ Year Member



Now that will work. Salsa. Now back to the books, and manual to redue this script the right way.