kenrbnsn Posted June 22, 2008 Share Posted June 22, 2008 Ok, after debugging my code, here is a full working example: <?php $default_vals = array('firstname' => '- Enter First Name -', 'lastname' => '- Enter Last Name -','state' => ' - Enter State -'); $err_fields = array(); foreach ($_POST as $fld => $val) { switch ($fld) { case 'firstname': // these are al the fields that must be filled in case 'lastname': case 'street': case 'city': case 'state': case 'zip': if (array_key_exists($fld,$default_vals)) $val = str_replace($default_vals[$fld],'',$val); // remove the default value from the entered value if (strlen(trim(stripslashes($val))) == 0) { //field is blank $err_fields[] = $fld; } break; case 'something': case 'somethingelse': // // do another validation on different content // break; } } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> </head> <body> <?php $flds = array('firstname','lastname','street','city','state','zip'); $tmp = array(); $tmp[] = '<form method="post" action="">'; foreach ($flds as $fld) { $red = (in_array($fld,$err_fields))?' style="color:red"':''; $val = (isset($_POST[$fld]) && $_POST[$fld] != $default_vals[$fld])?htmlentities(trim(stripslashes($_POST[$fld])),ENT_QUOTES):$default_vals[$fld]; $tmp[] = '<label for="' . $fld . '" ' . $red . '>' . ucwords($fld) . '</label>'; $tmp[] = '<input type="text" name="' . $fld . '" id="' . $fld . '" value="' . $val . '"><br>'; } $tmp[] = '<input name="submit" type="submit" value="Enter the information">'; $tmp[] = '</form>'; echo implode("\n",$tmp) . "\n"; ?> </body> </html> Ken Quote Link to comment https://forums.phpfreaks.com/topic/111193-many-variable-many-if-statement-scenario-another-way/page/2/#findComment-571493 Share on other sites More sharing options...
johnseito Posted June 23, 2008 Author Share Posted June 23, 2008 kenrbnsn, thanks, the code looks good. I'll analyzed it in detail. Quote Link to comment https://forums.phpfreaks.com/topic/111193-many-variable-many-if-statement-scenario-another-way/page/2/#findComment-571972 Share on other sites More sharing options...
johnseito Posted July 5, 2008 Author Share Posted July 5, 2008 kenrbnsn -- I have looked at your code, it looks good, but here is the problem I am facing with your code. For example I wanted a form like Quote 1. About yourself My name _________ __________ (first and last name) Birthday _________ ______ _________ (month, day and year) Address _________ _____________ (Address, state) 2. Select an ID and password Email _______ Username _______ Password ________ your code only give one field per line. I did the two field or three field per line but what I am having trouble are with adding 2. Select an ID and password this to the form and adding style and width and height of the form and fields, etc. Do you know how I can do this? examples would be great. thanks, Quote Link to comment https://forums.phpfreaks.com/topic/111193-many-variable-many-if-statement-scenario-another-way/page/2/#findComment-582242 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.