PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I swipe this code off the net that sets up a user registration form with a couple field validations, but the validation seems to utilize this die() function that kills the form submission and displays the error message only - - - the form element and etc are no longer vidible. However, I would rather the error messages fire by the applicable field input elements, which I think I have a handle on. Is there something else I can use instead of the die() function to stop the form posting while and error message of my choosing fires?
if(isset($_POST['subjoin'])){
/* Make sure all fields were entered */
if(!$_POST['user'] || !$_POST['pass']){
[color=#FF0040]die[/color]('You didn\'t fill in a required field.');
}
/* Spruce up username, check length */
$_POST['user'] = trim($_POST['user']);
if(strlen($_POST['user']) > 30){
[color=#FF0000]die[/color]("Sorry, the username is longer than 30 characters, please shorten it.");
}
Last edited by edawson003 on Sun Sep 13, 2009 12:08 pm, edited 2 times in total.
Both great suggestions, pytrin and Darhazer. Thank you much! I went with the array collection method.
When the form is submitted with certain errors that prevent the post, I would like the form to refesh with the previously entered field values retained. Right now the all seem to clear after pressing submit. I have a feeling that might be moving into Javascript territory.
Last edited by edawson003 on Sun Sep 13, 2009 5:21 pm, edited 1 time in total.
You don't need javascript - or rather, javascript can't help you after the page has been refreshed. check for the existence of a POST value for each of the inputs. For example:
Most people build functions to handle those kind of repetitive form element building.
Textareas are similar to regular inputs, only the data goes between the textarea tags:
The suggestion for the text box is pretty straight forward. Easy enough, I will apply the syntax for that. Now the option for setting up an array, looks a little more involved. I'll give it a shot. Thanks!
Last edited by edawson003 on Sun Sep 20, 2009 4:20 pm, edited 1 time in total.
Okay, so now I got most of my form fields to retain values once post submit is selected and page refreshes. How do I make a condition that once a form is success fully submitted all post data points are reset to empty?
Last edited by edawson003 on Thu Sep 17, 2009 10:44 pm, edited 1 time in total.
Or simply redirect to another page (success page or something similar). That is usually the preferred option (so the user won't refresh and resend the form).
Alright, I applied the header redirect approach and it worked well. Also, tested the Array drop down solution and it seems to work. Is there anyway I can store the selectable options in mysql table and have the Array pull the selecable options from the table.