Jump to content

Recommended Posts

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

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.