Jump to content

Forms : Required Fields


The Praetorian

Recommended Posts

Hm. I'll give that a try. How do I add multiple fields, though? Would it be like this?

$validate=$_POST['NAMEOFFEILD'];$validate=$_POST['NAME of second field'];if ($Validate== ){echo "Error";}

Or would I need to just repeat the whole array for each field?Also.. does the code need to be embedded in the form php? Or would I need a separate <?php and stuff for it?

Link to comment
Share on other sites

you can put it on the same page, you must save the file as a php file (.php) and must be in a php freindly enviroment,you would have to do the same for each fieldThe form does not have to be in the <?php ?> tagsthe code is basically saying:get the form input, if the input is blank display error.if this doesn't work you will have to use javascript.

Link to comment
Share on other sites

Okay, I'm still not getting it. Where exactly does the array go? Does it go above theif (isset part? And which part needs to be repeated?

<?phpif (isset($_REQUEST['email'])){$name = $_REQUEST['name'];$username = $_REQUEST['username'];$age = $_REQUEST['age'];$gender = $_REQUEST['gender'];$nation = $_REQUEST['nation'];$guild = $_REQUEST['guild'];$history = $_REQUEST['history'];$writingsample = $_REQUEST['writingsample'];$alias = $_REQUEST['alias'];$agetwo = $_REQUEST['agetwo'];$icq = $_REQUEST['icq'];$aim = $_REQUEST['aim'];$msn = $_REQUEST['msn'];$email = $_REQUEST['email'];mail( "author@tsrealms.com", "Subject: $email","~Character Information~\r\n\r\n\r\n~Name~\r\n$name\r\n\r\n~User Name~\r\n$username\r\n\r\n~Age~\r\n$age\r\n\r\n~Gender~\r\n$gender\r\n\r\n~Nationality~\r\n$nation\r\n\r\n~Guild~\r\n$guild\r\n\r\n~Brief  History~\r\n$history\r\n\r\n~Writing Sample~\r\n$writingsample\r\n\r\n\r\n~Personal Information~\r\n\r\n\r\n~Name/Alias~\r\n$alias\r\n\r\n~Age~\r\n$agetwo\r\n\r\n~ICQ~\r\n$icq\r\n\r\n~AIM~\r\n$aim\r\n\r\n~MSN~\r\n$msn", "From: $email" );echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><hr class='blocktext' /><p class='blocktext'>Thank you for your application! One of the admins should be getting back to you via email within 24 hours.</p><hr class='blocktext' />";}else{echo "<form method='post' action='appmail.php' name='app' onSubmit='return CheckRequiredFields()'><br /><hr /><h2>~      Character Information      ~</h2><hr /><br /><br /><b>Name:</b><br /><input type='text' name='name' size='20'><br /><p>Please read the Naming guideline for the country you've chosen for your character. Or if you'd rather have the writing staff come up with an appropriate name for you, just put a ~ here.</p><br /><br /><b>User Name:</b><br /><input type='text' name='username' size='20'><br /><p>This will be your user name on the forum, so please make sure it's appropriate. If you leave this field blank then your character's name will be your user name.</p><br /><br /><b>Age:</b><br /><input type='text' name='age' size='2'><br /><br /><b>Male:</b><input type='radio' name='gender' value='Male'><br /><b>Female:</b><input type='radio' name='gender' value='Female'><br /><br /><b>Nationality:</b><br /><select name='nation'><option value=''></option><option value='Siik'>Siik</option><option value='Shuni'>Shuni</option><option value='Ithian'>Ithian</option><option value='Athwani'>Athwani</option><option value='Tuiani-iiken'>Tuiani - Iiken</option><option value='Tuiani-kiil'>Tuiani - Kiil</option></select><br /><br /><b>Pathfinders</b><br /><input type='checkbox' name='guild' value='Yes'><br /><p>Check this box if you would like your character to be a member of the Pathfinders. (<b>Required reading: </b><a href='laws.shtml' target='_blank'>Laws of the Deep</a>, <a href='pathfinders.shtml' target='_blank'>The Pathfinders</a>.)</p><br /><br /><b>Brief History:</b><br /><textarea name='history' rows='15' cols='60'></textarea><br /><br /><b>Writing Sample:</b><br /><textarea name='writingsample' rows='15' cols='60'></textarea><br /><br /><br /><hr /><h2>~      Personal Information      ~</h2><hr class='blocktext' /><br /><br /><b>Email:</b><br /><input name='email' type='text' size='20'><br /><p>Needless to say, please make sure your email is correct, otherwise you could be waiting a long time to hear back from us.</p><br /><br /><b>Name/Alias:</b><br /><input name='alias' type='text' size='20'><br /><p>This isn't a required field. If you want to put your first name here you can, or you can put a nickname you'd like people to use. It's entirely up to you.</p><br /><br /><b>Age:</b><br /><input name='agetwo' type='text' size='2'><br /><p>Please be honest. None of this information will be given out to anyone but Admin.</p><br /><br /><b>ICQ:</b><br /><input type='text' name='icq' size='20'><br /><br /><b>AIM:</b><br /><input type='text' name='aim' size='20'><br /><br /><b>MSN:</b><br /><input type='text' name='msn' size='20'><br /><br /><hr /><br /><br /><input type='submit' value='Apply'></form>";}?>

Link to comment
Share on other sites

The PHP code goes in the form processing page, not on the page with the form itself. Or, you can make them both the same page, wihich may keep the code a little cleaner. You can define your array like this:

$formvars = array();$formvars['first'] = $_POST['first'];$formvars['second'] = $_POST['second'];$formvars['third'] = $_POST['third'];$errors = "";foreach ($formvars as $key => $val){  if ($val == "")	$errors .= "The field '{$key}' is required.";}if ($errors != ""){  echo $errors;  //show the form again}else{  //process the form}

Here's an example of how to process a form on the same page:http://w3schools.invisionzone.com/index.ph...44&hl=howto

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...