Jump to content

form errors


amarkabove

Recommended Posts

i am unable to get the errors to print on the page. if i fill in the required fields the form writes to the data base. but if i leave out one or more of the required fields i lose all of the inputed form data when the form gets posted back, the errors don't print and nothing is written to the data base.

 

if ($_POST['_submit_check']) {

if ($form_errors = validate_form()){

show_form($form_errors);

 

}else{

process_form();

}

}else {

show_form();

}

 

function show_form($errors = '') {

 

if ($errors) {

$error_text = '<p> These fields are required.<br>';

$error_text .= '<ul><li>';

$error_text .= implode('</li><li>',$errors);

$error_text .= '</li></ul></p>';

}else{

$error_text = 'else';

}

}

 

function validate_form() {

$errors = array();

if (! strlen(trim($_POST['CompanyName']))) {

$form_errors[] = 'Please enter your Company Name';

}

if (! strlen(trim($_POST['CompanyContact']))) {

$errors[] = 'Please enter the Company Contact';

}

if (! strlen(trim($_POST['PhoneNumber']))) {

$errors[] = 'Please enter the Contact Phone Number';

}

return $errors;

}

Link to comment
Share on other sites

Leave the processing of the inputs on the same page. Or include them from another file so they process on the same page. Include a variable, I call mine $error, that you set to TRUE if anything fails. Then, right before you try to update the database, wrap the updating in an if. if !$error, then do it. Otherwise, it will display the form again.

 

At the top of the page, I'm assuming that you've grabbed all the data from the form by $_POST method and then saved them into their own unique variables so updating the db is easier.

 

Just give each value attribute for the inputs a PHP echo statement that will echo whatever it pulled from that input previously on the form submission.

 

So ... <input type="text" value="<?php echo $username; ?>" name="username" />

 

Then at the bottom you have a div that holds a generic error message. You check to see if $error is set to something and if so display the error message. You could also have separate error messages and variables to store messages and states for each input and display multiple errors messages.

 

That is how I do all my pages, if that's confusing, I could probably code up a VERY SIMPLE example. But I think it's kind of straight forward.

Link to comment
Share on other sites

i change this section of the code. now the errors print, but they print to the top of the page above everything else. when i view the page source the closing /p is not there.

 

function show_form($errors = '') {

 

if ($errors) {

$error_text = '<p> These fields are required.<br>';

echo $error_text .= implode('<br>',$errors);

$error_text .= '</p>';

}else{

$error_text = '';

}

return $error_text;

}

Link to comment
Share on other sites

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.