Jump to content

Recommended Posts

Hello Everyone,

 

I have created a form for a personal project that I am working on. I have some error checking and stuff in the form but what I would like to happen is when an error has occurred for the page to reopen the form with the error message. I know that this can be done with the header function. But how do I get the form to display the error.

 

Here is the block of code that I am working with

if ($file_error > 0) {
                    header ('Location: add_category.php');
                    echo "There was an error uploading file.";
                }
                else {
                    if ($file_type != $image_array) {
                        header ('Location: add_category.php');
                        echo "Invalid Image Type";
                    }
                    if ($file_name == FALSE) {
                        header ('Location: add_category.php');
                        echo "Please choose a image for your category";
                    }
                    if ($file_size > 5000000) {
                        header ('Location: add_category.php');
                        echo "File size is to large. Images must be smaller than 5MB.";
                    }

                    if (str_word_count($description) > 300) {
                        header ('Location: add_category.php');
                        echo "<p>category description must be less than 300 words<br />You have '.str_word_count($description).' words</p>";
                    }

This is the form code that I am working with

<div id="content">
                <form id="add_cat_form" name="add_cat_form" action="do_category.php" method="post" enctype="multipart/form-data">
                    <fieldset>
                        <legend><p>Add Category</p></legend>
                        <p>
                            <label for="cat_name">Category Name:</label> <input class="input_box" type="text" name="cat_name" id="cat_name" />
                        </p>
                        <p>
                            <label for="cat_image">Category Image:</label> <input class="file" type="file" name="cat_image" id="cat_image"  />
                        </p>
                        <p>
                            <label for="cat_desc">Description:</label><br /><textarea class="cat_desc" name="cat_desc" id="cat_desc"></textarea><br />
                            <span id="word_count">300 words Max</span>
                        </p>
                        <input class="submit_button" type="submit" value="Add Category" />
                    </fieldset>
                </form>
            </div>

You shouldn't use a header redirect, because then all of the form data would be lost. If you process the form on the same page as you display it, this task is trivial. This codes a bit ugly but it should give you an idea of the process:

<?php

// check if the form was submitted
if (!empty($_POST)) {
// set an array of required fields to check
$required = array('firstname', 'lastname');

$errors = array();

// loop through the required fields and make sure they are not empty
foreach($required as $key)
{
	if (empty($_POST[$key])) {
		// populate the errors array if a required field is empty
		$errors[$key] = 'This field is required';
	}
}

// if no errors were found, continue processing the script
if (empty($errors)) {
	header('location: http://example.com');
	exit;
}

// if there are errors, the form will be redisplayed
}

echo '<form action="" method="post">
First Name: <input type="text" name="firstname" value="' . (isset($_POST['firstname']) ? $_POST['firstname'] : '') . '" /><br />
' . (isset($errors['firstname']) ? $errors['firstname'] . '<br />' : '') . '
Last Name: <input type="text" name="lastname" value="' . (isset($_POST['lastname']) ? $_POST['lastname'] : '') . '" /><br />
' . (isset($errors['lastname']) ? $errors['lastname'] . '<br />' : '') . '
<input type="submit" name="submit" value="Submit" />
</form>';

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.