WRXHokie Posted March 25, 2007 Share Posted March 25, 2007 I'm trying to process a form. In my data validation function i try and check for errors in the user submitted fields. In the fields in which are fine i want to be able to keep the values in the form when there are errors and clear the fields that have errors in them. For some reason i cannot get this to work. Here's my code, i've cut it down to the relevant parts. The first file is the index.php file which shows the form but first checks for errors. The second file has all the relevant php functions i call defined. In form.php i just show a small piece of the show form function where i try and present a text field with its default value. <?php require "form.php"; $defaults = array(); if ($_POST['_submit_check']) { // If validate_form() returns errors, pass them to show_form() if ($form_errors = validate_form($defaults)) { show_form($form_errors, $defaults); } else { process_form(); } } else { show_form(); } ?> form.php: <?php function show_form($errors = '', $defaults) { print '<input type="text" name="person" size="1" maxlength="2" value='; print $defaults[person]; print '>'; } function process_form() { //print "Hello, ". $_POST['my_name']; //TODO: MySQL statements to add to database show_form(); } function validate_form($defaults = '') { // Start with an empty array of error messages $errors = array(); $role_array = array(); if (!is_numeric($_POST['person'])) { $defaults[person] = ""; $errors[] = 'Please enter a valid person id (numeric)'; } else { $defaults[person] = $_POST['person']; } if(!isset($_POST['production'])) { $errors[] = 'Please select a production'; } if(isset($_POST['role'])) { $role_array = $_POST['role']; } else $errors[] = 'Please select at least one role'; // Return the (possibly empty) array of error messages return $errors; } ?> I'm lost, so what am i missing? Link to comment https://forums.phpfreaks.com/topic/44270-solved-keeping-values-in-forms/ Share on other sites More sharing options...
WRXHokie Posted March 26, 2007 Author Share Posted March 26, 2007 bump Link to comment https://forums.phpfreaks.com/topic/44270-solved-keeping-values-in-forms/#findComment-215237 Share on other sites More sharing options...
desithugg Posted March 26, 2007 Share Posted March 26, 2007 umm i think you need the quotes try changing the fallowing print '<input type="text" name="person" size="1" maxlength="2" value='; print $defaults[person]; print '>'; to print "<input type='text' name='person' size='1' maxlength='2' value='".$defaults['person']."'>"; Link to comment https://forums.phpfreaks.com/topic/44270-solved-keeping-values-in-forms/#findComment-215272 Share on other sites More sharing options...
WRXHokie Posted March 26, 2007 Author Share Posted March 26, 2007 tried that... didnt work. For some reason it isnt keeping the value of $defaults['person'] in tact when it transitions from one function to another. Its storing it correctly, i can display it in the validate function... but its not there when i get back to the show form function... i'm still at a loss as to why. Link to comment https://forums.phpfreaks.com/topic/44270-solved-keeping-values-in-forms/#findComment-215287 Share on other sites More sharing options...
WRXHokie Posted March 26, 2007 Author Share Posted March 26, 2007 Code as it stands now: form.php: <?php global $defaults function show_form($errors = '') { print '<input type="text" name="person" size="1" maxlength="2" value="'; print "$defaults[person]"; print '">'; } function process_form() { //TODO: MySQL statements to add to database show_form(); } function validate_form() { // Start with an empty array of error messages $errors = array(); $role_array = array(); if (!is_numeric($_POST['person'])) { $defaults[person] = ""; $errors[] = 'Please enter a valid person id (numeric)'; } else { $defaults[person] = $_POST['person']; } if(!isset($_POST['production'])) { $errors[] = 'Please select a production'; } if(isset($_POST['role'])) { $role_array = $_POST['role']; } else $errors[] = 'Please select at least one role'; // Return the (possibly empty) array of error messages return $errors; } ?> index.php: <?php require "form.php"; if ($_POST['_submit_check']) { // If validate_form() returns errors, pass them to show_form() if ($form_errors = validate_form()) { show_form($form_errors); } else { process_form(); } } else { show_form(); } ?> I'm trying to use a global variable outside the scope of the function definitions and then reference that global variable inside the functions but for some reason it still isnt recognized by the html as a default value. Any other ideas? Link to comment https://forums.phpfreaks.com/topic/44270-solved-keeping-values-in-forms/#findComment-215396 Share on other sites More sharing options...
trq Posted March 26, 2007 Share Posted March 26, 2007 You need to move... global $defaults into the function. Link to comment https://forums.phpfreaks.com/topic/44270-solved-keeping-values-in-forms/#findComment-215405 Share on other sites More sharing options...
Lethal.Liquid Posted March 26, 2007 Share Posted March 26, 2007 Doesn't that also need a semicolan ( at the end too? Link to comment https://forums.phpfreaks.com/topic/44270-solved-keeping-values-in-forms/#findComment-215415 Share on other sites More sharing options...
trq Posted March 26, 2007 Share Posted March 26, 2007 Of course. Link to comment https://forums.phpfreaks.com/topic/44270-solved-keeping-values-in-forms/#findComment-215418 Share on other sites More sharing options...
WRXHokie Posted March 26, 2007 Author Share Posted March 26, 2007 @thorpe Tried it... didnt work. Not sure why it doesnt work... been thru this a hundred times. Link to comment https://forums.phpfreaks.com/topic/44270-solved-keeping-values-in-forms/#findComment-215419 Share on other sites More sharing options...
trq Posted March 26, 2007 Share Posted March 26, 2007 In here.... if ($form_errors = validate_form()) { show_form($form_errors); } else { process_form(); } When you call process_form() it just calls show_form() which displays the form again. Why would you expect the last posted results to still be there? Im sorry, but Im finding it hard to see your logic here. Link to comment https://forums.phpfreaks.com/topic/44270-solved-keeping-values-in-forms/#findComment-215427 Share on other sites More sharing options...
WRXHokie Posted March 26, 2007 Author Share Posted March 26, 2007 Alright let me explain each function and its purpose: show_form(): Displays the html based form validate form(): Validates the user entered data in the form. Thus if something is entered wrong, i want to clear that field, but keep all the correctly entered fields in some variable (in this case $default) for displaying when the form gets reprinted with the indicated errors. process form(): This will eventually be mySQL statements to add user entered data into the database. This function only gets called if the user entered data passes the validate form() with no errors. I want to be able to keep the correctly entered fields when a form is submitted, but errors are detected. Then re-display the form with those correct fields still present, and the fields with errors cleared. With $defaults, i'm trying to store those correctly entered fields. Making $defaults global in form.php does not work. Link to comment https://forums.phpfreaks.com/topic/44270-solved-keeping-values-in-forms/#findComment-215442 Share on other sites More sharing options...
WRXHokie Posted March 26, 2007 Author Share Posted March 26, 2007 Figured it out... you gotta add "global $defaults" in every function that accesses it. Thanks for everyone's help. Link to comment https://forums.phpfreaks.com/topic/44270-solved-keeping-values-in-forms/#findComment-215449 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.