TheJoey Posted September 5, 2009 Share Posted September 5, 2009 Its currently not displaying errors for email & age. <?php // Function to display form function showForm($erroremail=false,$errorfname=false,$errorlname=false,$errorage){ if ($erroremail) $errorTextemail = "Email must be valid"; if ($errorfname) $errorTextfname = "First name cant be empty"; if ($errorlname) $errorTextlname = "Last name cant be empty"; if ($errorage) $errorTextage = "Age must be between 16-90"; echo '<div id="center"><form action="save.php" method="POST"><table>'; // Display email field an error if needed echo '<tr><td>Email:</td><td><input type="text" name="email"></td></tr>'; if ($erroremail) echo "<tr><td colspan='2'>$errorTextemail</td></tr>"; // Display First name field an error if needed echo '<tr><td>FirstName:</td><td><input type="text" name="fname"></td></tr>'; if ($errorfname) echo "<tr><td colspan='2'>$errorTextfname</td></tr>"; // Display Last name field an error if needed echo '<tr><td>LastName:</td><td><input type="text" name="lname"></td></tr>'; if ($errorlname) echo "<tr><td colspan='2'>$errorTextlname</td></tr>"; // Display Last name field an error if needed echo '<tr><td>Age:</td><td><input type="text" name="age"></td></tr>'; if ($errorage) echo "<tr><td colspan='2'>$errorTextage</td></tr>"; echo '<tr><td>Address:</td><td><input type="text" name="address"></td></tr>'; echo '<tr><td>City:</td><td><input type="text" name="city"></td></tr>'; echo '<tr><td><input type="submit" name="SubmitForm" value="Send"></td></tr>'; echo '<form></div>'; } if (!isset($_POST['SubmitForm'])) { showForm(); } else { //Init error variables $erroremail = false; $errorfname = false; $errorlname = false; $errorage = false; $email = isset($_POST['email']) ? trim($_POST['email']) : ''; $fname = isset($_POST['fname']) ? trim($_POST['fname']) : ''; $lname = isset($_POST['lname']) ? trim($_POST['lname']) : ''; $age = isset($_POST['age']) ? trim($_POST['age']) : ''; if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) $errorEmail = true; if (strlen($fname)<1) $errorfname = true; if (strlen($lname)<1) $errorlname = true; if ($age <16 && $age >90) $errorage = true; // Display the form again as there was an error if ($erroremail || $errorfname || $errorlname || $errorage) { showForm($erroremail,$errorfname,$errorlname,$errorage); } else { $email = $_POST['email']; $fname = $_POST['fname']; $lname = $_POST['lname']; $age = $_POST['age']; $address = $_POST['address']; $city = $_POST['city']; $fp = fopen("users.txt","a"); $fp1 = fopen("data.txt","a"); if(!$fp) { echo 'Error: Cannot open file.'; exit; } fwrite($fp, $email.":".$fname."\r\n"); fwrite($fp1, $lname."||".$age."||".$address."||".$city."\r\n"); fclose($fp); echo 'Added'; } } ?> Link to comment https://forums.phpfreaks.com/topic/173185-validation-problem-in-php/ Share on other sites More sharing options...
Philip Posted September 5, 2009 Share Posted September 5, 2009 function showForm($erroremail=false,$errorfname=false,$errorlname=false,$errorage) I wouldn't recommend the last argument be the only one that is required Link to comment https://forums.phpfreaks.com/topic/173185-validation-problem-in-php/#findComment-912909 Share on other sites More sharing options...
TheJoey Posted September 5, 2009 Author Share Posted September 5, 2009 well i have other text fields i just dont want them validated. Link to comment https://forums.phpfreaks.com/topic/173185-validation-problem-in-php/#findComment-912912 Share on other sites More sharing options...
Philip Posted September 5, 2009 Share Posted September 5, 2009 I'd suggest either: function showForm($erroremail=false,$errorfname=false,$errorlname=false,$errorage=false) or function showForm($errorage,$erroremail=false,$errorfname=false,$errorlname=false) That way you don't have a chance to run across: Warning: Missing argument 4 for showForm(), called in -.php on line # and defined in -.php on line # Otherwise, it should echo the message. Do you have error_reporting/display turned on? Link to comment https://forums.phpfreaks.com/topic/173185-validation-problem-in-php/#findComment-912915 Share on other sites More sharing options...
TheJoey Posted September 5, 2009 Author Share Posted September 5, 2009 thanks ill give it a shot. Age & Email errors still arent showing up.. mabye its the validation technique im trying to use. Link to comment https://forums.phpfreaks.com/topic/173185-validation-problem-in-php/#findComment-912917 Share on other sites More sharing options...
TheJoey Posted September 5, 2009 Author Share Posted September 5, 2009 thanks ill give it a shot. Age & Email errors still arent showing up.. mabye its the validation technique im trying to use. Scratch that its Lastname & Age Link to comment https://forums.phpfreaks.com/topic/173185-validation-problem-in-php/#findComment-912918 Share on other sites More sharing options...
TheJoey Posted September 5, 2009 Author Share Posted September 5, 2009 it seems when i rearange function showForm($erroremail=false,$errorfname=false,$errorlname=false,$errorage=false) the error output changes from ethier email and firstname to displaying firstname and lastname but never all of them. Link to comment https://forums.phpfreaks.com/topic/173185-validation-problem-in-php/#findComment-913038 Share on other sites More sharing options...
TheJoey Posted September 5, 2009 Author Share Posted September 5, 2009 it seems when i rearange function showForm($erroremail=false,$errorfname=false,$errorlname=false,$errorage=false) the error output changes from ethier email and firstname to displaying firstname and lastname but never all of them. Link to comment https://forums.phpfreaks.com/topic/173185-validation-problem-in-php/#findComment-913142 Share on other sites More sharing options...
TheJoey Posted September 6, 2009 Author Share Posted September 6, 2009 > pulling my hair out trying to work this out, its prob something stupid. Link to comment https://forums.phpfreaks.com/topic/173185-validation-problem-in-php/#findComment-913508 Share on other sites More sharing options...
TheJoey Posted September 7, 2009 Author Share Posted September 7, 2009 nothing new i havent determined the problem Link to comment https://forums.phpfreaks.com/topic/173185-validation-problem-in-php/#findComment-913846 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.