amarkabove Posted March 6, 2008 Share Posted March 6, 2008 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 https://forums.phpfreaks.com/topic/94787-form-errors/ Share on other sites More sharing options...
bpops Posted March 6, 2008 Share Posted March 6, 2008 It looks like you've used the wrong array name on the 3rd line of your validate_form() function $form_errors[] = 'Please enter your Company Name'; should be $errors[] = 'Please enter your Company Name'; Link to comment https://forums.phpfreaks.com/topic/94787-form-errors/#findComment-485402 Share on other sites More sharing options...
bpops Posted March 6, 2008 Share Posted March 6, 2008 And then under show_form(), you either need to echo $error_text; or else return $error_text; and then echo the result in your main program. Link to comment https://forums.phpfreaks.com/topic/94787-form-errors/#findComment-485404 Share on other sites More sharing options...
soycharliente Posted March 6, 2008 Share Posted March 6, 2008 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 https://forums.phpfreaks.com/topic/94787-form-errors/#findComment-485412 Share on other sites More sharing options...
amarkabove Posted March 6, 2008 Author Share Posted March 6, 2008 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 https://forums.phpfreaks.com/topic/94787-form-errors/#findComment-485496 Share on other sites More sharing options...
soycharliente Posted March 7, 2008 Share Posted March 7, 2008 It seems like we might need to see more code. Attach your file, let me look at it, and I'll give you my opinion of where I would place sections of code if I were building the page. Link to comment https://forums.phpfreaks.com/topic/94787-form-errors/#findComment-486484 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.