Placing Errors On The Page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mpar612

    #1

    Placing Errors On The Page

    I have code below (due to length I only included a portion). I am
    validating an email address. It checks to see if the email field is
    blank, in the proper format, etc... It prints errors to the screen and
    everything works perfectly, but the errors always print at the very
    top of the page and I am having difficulty moving those errors around
    so they fit nicely into the page design I have. Rather than printing
    the errors I tried assigning them to variables and printing them
    elsewhere on the page, but it didn't work and the errors did not try
    at all. I also tried moving the function to a different part of the
    page where the errors would print where I wanted them, but I had the
    same problem as with the variables. I'm kind of in a bind and need
    this done asap. Does anyone have any thoughts on this?

    Thanks in advance!!!

    <?php
    function process_form() {

    global $db;

    $firstName = addslashes($_PO ST['firstName']);
    $lastName = addslashes($_PO ST['lastName']);
    $email = $_POST['email'];
    $emailHash = md5($email);
    $active = "n";

    // Check syntax
    $validEmailExpr = "^[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$
    %&_-])*" .
    "@[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*
    $";

    // Validate the email
    if (empty($email))
    {
    print "The email field cannot be blank";
    return false;
    }
    elseif (!eregi($validE mailExpr, $email))
    {
    print "The email must be in the name@domain format.";
    return false;
    }
    elseif (strlen($email) 30)
    {
    print "The email address can be no longer than 30
    characters.";
    return false;
    }
    elseif (function_exist s("getmxrr") &&
    function_exists ("gethostbyname "))
    {
    // Extract the domain of the email address
    $maildomain = substr(strstr($ email, '@'), 1);

    if (!(getmxrr($mai ldomain, $temp) ||
    gethostbyname($ maildomain) != $maildomain))
    {
    print "The domain does not exist.";
    return false;
    }
    }

    $db->query('INSER T INTO users (firstName, lastName, email, hash,
    active)
    VALUES (?,?,?,?,?)',
    array($firstNam e, $lastName, $email, $emailHash,
    $active));

    $message = "
    Thank you for signing up for the . Please click on the link below
    to confirm your registration. If the link is not active, copy-and-
    paste it into your browser window.

    http://www.EmailCaptur e/validateSuccess .php?id=$emailH ash
    ";

    $subject = "";
    $from = "";

    mail("$email", "$subject", "$message", "From: $from");

    print "<script>window .location='Than kYou.htm'</script>";

    }
    ?>

  • Jerry Stuckle

    #2
    Re: Placing Errors On The Page

    mpar612 wrote:
    I have code below (due to length I only included a portion). I am
    validating an email address. It checks to see if the email field is
    blank, in the proper format, etc... It prints errors to the screen and
    everything works perfectly, but the errors always print at the very
    top of the page and I am having difficulty moving those errors around
    so they fit nicely into the page design I have. Rather than printing
    the errors I tried assigning them to variables and printing them
    elsewhere on the page, but it didn't work and the errors did not try
    at all. I also tried moving the function to a different part of the
    page where the errors would print where I wanted them, but I had the
    same problem as with the variables. I'm kind of in a bind and need
    this done asap. Does anyone have any thoughts on this?
    >
    Thanks in advance!!!
    >
    <?php
    function process_form() {
    >
    global $db;
    >
    Don't use globals. Globals are bad
    $firstName = addslashes($_PO ST['firstName']);
    $lastName = addslashes($_PO ST['lastName']);
    Why are you calling addslashes? If you're using MySQL, see
    mysql_real_esca pe_string().

    But either way this should be just before you store the data in the
    database.
    $email = $_POST['email'];
    $emailHash = md5($email);
    $active = "n";
    >
    // Check syntax
    $validEmailExpr = "^[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$
    %&_-])*" .
    "@[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*
    $";
    >
    // Validate the email
    if (empty($email))
    {
    print "The email field cannot be blank";
    return false;
    }
    elseif (!eregi($validE mailExpr, $email))
    {
    print "The email must be in the name@domain format.";
    Where are you calling this function? wherever it's *being called* is
    where the text will be inserted in your HTML age.
    return false;
    }
    elseif (strlen($email) 30)
    {
    print "The email address can be no longer than 30
    characters.";
    return false;
    >
    See above.

    }
    elseif (function_exist s("getmxrr") &&
    function_exists ("gethostbyname "))
    {
    // Extract the domain of the email address
    $maildomain = substr(strstr($ email, '@'), 1);
    >
    if (!(getmxrr($mai ldomain, $temp) ||
    gethostbyname($ maildomain) != $maildomain))
    {
    print "The domain does not exist.";
    return false;
    }
    }
    >
    $db->query('INSER T INTO users (firstName, lastName, email, hash,
    active)
    VALUES (?,?,?,?,?)',
    array($firstNam e, $lastName, $email, $emailHash,
    $active));
    >
    $message = "
    Thank you for signing up for the . Please click on the link below
    to confirm your registration. If the link is not active, copy-and-
    paste it into your browser window.
    >
    http://www.EmailCaptur e/validateSuccess .php?id=$emailH ash
    ";
    >
    $subject = "";
    $from = "";
    >
    mail("$email", "$subject", "$message", "From: $from");
    >
    print "<script>window .location='Than kYou.htm'</script>";
    >
    }
    ?>
    >

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Martin Mandl - m2m tech support

      #3
      Re: Placing Errors On The Page

      On Mar 10, 12:02 am, "mpar612" <mpar...@gmail. comwrote:
      I have code below (due to length I only included a portion). I am
      validating an email address. It checks to see if the email field is
      blank, in the proper format, etc... It prints errors to the screen and
      everything works perfectly, but the errors always print at the very
      top of the page and I am having difficulty moving those errors around
      so they fit nicely into the page design I have. Rather than printing
      the errors I tried assigning them to variables and printing them
      elsewhere on the page, but it didn't work and the errors did not try
      at all. I also tried moving the function to a different part of the
      page where the errors would print where I wanted them, but I had the
      same problem as with the variables. I'm kind of in a bind and need
      this done asap. Does anyone have any thoughts on this?
      >
      Thanks in advance!!!
      >
      <?php
      function process_form() {
      >
      global $db;
      >
      $firstName = addslashes($_PO ST['firstName']);
      $lastName = addslashes($_PO ST['lastName']);
      $email = $_POST['email'];
      $emailHash = md5($email);
      $active = "n";
      >
      // Check syntax
      $validEmailExpr = "^[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$
      %&_-])*" .
      "@[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*
      $";
      >
      // Validate the email
      if (empty($email))
      {
      print "The email field cannot be blank";
      return false;
      }
      elseif (!eregi($validE mailExpr, $email))
      {
      print "The email must be in the name@domain format.";
      return false;
      }
      elseif (strlen($email) 30)
      {
      print "The email address can be no longer than 30
      characters.";
      return false;
      }
      elseif (function_exist s("getmxrr") &&
      function_exists ("gethostbyname "))
      {
      // Extract the domain of the email address
      $maildomain = substr(strstr($ email, '@'), 1);
      >
      if (!(getmxrr($mai ldomain, $temp) ||
      gethostbyname($ maildomain) != $maildomain))
      {
      print "The domain does not exist.";
      return false;
      }
      }
      >
      $db->query('INSER T INTO users (firstName, lastName, email, hash,
      active)
      VALUES (?,?,?,?,?)',
      array($firstNam e, $lastName, $email, $emailHash,
      $active));
      >
      $message = "
      Thank you for signing up for the . Please click on the link below
      to confirm your registration. If the link is not active, copy-and-
      paste it into your browser window.
      >
      http://www.EmailCaptur e/validateSuccess .php?id=$emailH ash
      ";
      >
      $subject = "";
      $from = "";
      >
      mail("$email", "$subject", "$message", "From: $from");
      >
      print "<script>window .location='Than kYou.htm'</script>";
      >
      }
      >
      ?>
      Dear mpar612,

      put your error messages in a string instead of printing directly, and
      print them when and where you want to have them ...

      good luck
      Martin

      ------------------------------------------------
      online accounting on bash bases
      Online Einnahmen-Ausgaben-Rechnung

      ------------------------------------------------
      m2m server software gmbh


      Comment

      • Kimmo Laine

        #4
        Re: Placing Errors On The Page

        "mpar612" <mpar612@gmail. comwrote in message
        news:1173481330 .522140.17500@3 0g2000cwc.googl egroups.com...
        >I have code below (due to length I only included a portion). I am
        validating an email address. It checks to see if the email field is
        blank, in the proper format, etc... It prints errors to the screen and
        everything works perfectly, but the errors always print at the very
        top of the page and I am having difficulty moving those errors around
        so they fit nicely into the page design I have. Rather than printing
        the errors I tried assigning them to variables and printing them
        elsewhere on the page, but it didn't work and the errors did not try
        at all.
        The prbolem is variable scope.

        function foo(){
        $x = 'HELLO WORLD!';
        }
        foo();
        echo $x; // Print's nothing

        Regardles of $x being assigned at foo(), nothing is echoed. Why? 'cos $x
        belongs to variable scope of foo(), not main.

        Solutions:
        Bad solution: make $x global. Quick and dirty, don't try this at home.
        Good solution: make the function return an array of errors and use the
        returned array values to echo error messages.


        --
        "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
        http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
        spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


        Comment

        Working...