www.webdeveloper.com
Results 1 to 10 of 10

Thread: Form validation to appear in an 'alert' box

  1. #1
    Join Date
    Apr 2009
    Posts
    60

    Form validation to appear in an 'alert' box

    Hello

    I have a php form that I'm trying to cobble together from various other forms!

    I need some kind of mandatory fields validation and I have some script that does the job. The problem is that the error messages appear on a new page (requiring the user to click the back button on the browser. I'd like the error messages to appear in a little popup window.

    Here's the code that I imagine I need to change for this to happen?
    Code:
    $yourname = check_input($_POST['name'], "Enter your name!");
    If the text box 'name' is empty they get the error message "Enter your name!"

    In case you need it, here's the rest of the validation code:
    Code:
    // for required fields...
    function check_input($data, $problem='')
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        if ($problem && strlen($data) == 0)
        {
            die($problem);
        }
        return $data;
    }
    Any help much appreciated!

  2. #2
    Join Date
    Aug 2007
    Location
    New Jersey, US
    Posts
    1,779
    I would probably include a popup window script and do something like this:

    PHP Code:
    <?php
    function check_input($data$problem='')
    {
        
    $data trim($data);
        
    $data stripslashes($data);
        
    $data htmlspecialchars($data);
        if (
    $problem && strlen($data) == 0)
        {
         
    ?>
            //some sort of popup window, from javascript or whatever
         <?php

            
    die();
        }
        return 
    $data;
    }
    Or something of the sort (but I'm fairly new with PHP, so there may be better ways)

    Good luck with it.

  3. #3
    Join Date
    Nov 2008
    Posts
    2,477
    What you are asking for (i think) is to have a popup before as soon as the user submits the form if there are errors. That isn't possible with PHP as it is a sever side language. You would have to use javascript, but relying on javascript for validation is a very bad plan.

    You have a few options. You could just submit the form back to itself and keep your validation in a single script That way you can check the contents of $_POST and do whatever right there on the same page.

    You could store the data in the $_SESSION superglobal and the redirect back the originating form on an error, using the contents of $_SESSION as necessary.

    My personal preference would be to combine one of the above with validating as you go using AJAX. In terms of user experience (which seems to be your concern from the question) AJAX validation is easily the best imo.

  4. #4
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    The easiest way to do form checking before submittal is with Javascript. You then just create alert (error_message) and you have a pop-up. You can also do the checking after submittal and do the checking in PHP, but the var would have to echoed to the Javascript variable to get the message to the alert().

  5. #5
    Join Date
    Apr 2009
    Posts
    60
    Thanks for the responses guys - much appreciated!

    I'll look into the Ajax and Javascript idea.

    The form I have has some code that validates the email address by checking there's an '@' symbol etc... If the format is wrong you get an alert box so I was hoping I could get it to do something like that if a required field was left empty?

    The code for the email validation is:
    Code:
    if (!preg_match("(^[-\w\.]+@([-a-z0-9]+\.)+[a-z]{2,4}$)i",$_POST['email'])) $alert = "Please enter a valid email address.";

  6. #6
    Join Date
    Aug 2007
    Location
    New Jersey, US
    Posts
    1,779
    here's a few methods to do this:
    http://www.zend.com/forums/index.php...&t=msg&th=3704

    i'm not sure which is the best method, so hopefully we'll get other opinions here.

  7. #7
    Join Date
    Aug 2007
    Location
    New Jersey, US
    Posts
    1,779
    edit: double post

  8. #8
    Join Date
    Apr 2009
    Posts
    60
    Thanks dtm32236.

    I've taken a look but I have a real 'blind-spot' when it comes to coding.
    I've Googled using preg_match to detect empty text fields but I can't find anything about it.

  9. #9
    Join Date
    Nov 2008
    Posts
    2,477
    You don't need to use preg_match.

    PHP Code:
    if (empty($foo)) {
       
    // empty
    } else {
       
    // not empty


  10. #10
    Join Date
    Apr 2009
    Posts
    60

    Smile

    Quote Originally Posted by Mindzai View Post
    You don't need to use preg_match.

    PHP Code:
    if (empty($foo)) {
       
    // empty
    } else {
       
    // not empty

    Midzai you are a superstar!!! All sorted. I put this code in:
    PHP Code:
     if (empty($name)) {
       
    $alert "Please enter your name.";

    Therefore if the text field named 'name' is left empty you get the error message. Just what I wanted! Thanks again... very much appreciated.

    Thanks also to everyone else that chipped in

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles