Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: checking fields

  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts

    checking fields

    Hi

    I have a form that the user fills in details.
    I want to make sure only numbers are in certain fields and letters in others.
    I also want to make sure all fields are filled in before a user can continue.

    What's the best way of alerting a user?
    Are message boxes a good idea and how can these be done in php?

    thanks

  • #2
    Regular Coder
    Join Date
    Dec 2005
    Posts
    346
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Here is a option for using JS

    http://javascript.internet.com/forms...eric-only.html

    Here is the php solution
    http://us3.php.net/manual/en/function.is-numeric.php

    To make sure the fields aren't just blank use this:
    PHP Code:
        if ($variable1 == '' || $variable2 == '' || $variable3 == '')
            {
                echo
    'Please fill all fields';
            }
        else
            {
                
    //process form
            


  • #3
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    This is how I, personally, do it:

    After the user submits his input, I declare it:

    PHP Code:
    $first_name $_POST['first'];
    $how_many_kids $_POST['kids']; 
    Then I run checks on them with ctype_ functions. For numbers only (no decimal points or anything else), use ctype_digit(). For letters only, use ctype_alpha().

    As for a message, I just throw any errors into a $Message variable and then echo it at the top of my pages. If there is no error, nothing shows up. I also specify $Error_Stat = 0 at the top of my page and change that to 1 if there is an error...and then only run the MySQL query if the $Error_Stat still equals 0.

    PHP Code:
    <?php
    $Error_Stat 
    0;

    if (!
    ctype_alpha($first_name)) {
       
    $Error_Stat 1;
       
    $Message 'Your first name must be only letters!';
    }

    if (!
    ctype_digit($how_many_kids)) {
       
    $Error_Stat 1;
       
    $Message 'Amount of kids must be only numbers!';
    }

    if (
    $Error_Stat == 0) {
       
    //do the query, insert, etc. here
    }
    ?>

    <?=$Message?>


  •  

    Posting Permissions

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