Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    38
    Rep Power
    15

    3 fields for Telephone, instead of one ?


    I was wondering ... how exactly would I be able to have users enter in their phone number in three separate fields in a form, instead of just one big field?

    field 1 = 3 characters (area code)
    field 2 = 3 characters (prefix)
    field 3 = 4 characters (suffix)

    and have my php script email it to be in this format ...

    ### - ### - ####

    ie. 213-123-1234

    Previously I had it as one long field, where the user can enter in whatever they wanted ..

    ie. (213)123-1234, 2131231234, 213-123-1234, etc. etc.

    Which I don't want to do anymore. And instead have it split into three separate fields, and have a maximum character of 3 on the first two fields, and 4 on the last field.


    Is there an easy way to do this? While still keeping the value of the three fields the same? [or maybe using a different value name for each of the three fields, and then somehow "add them together" after submitted?

    Hope that made sense

    http://www.agregardie.com/form/php.txt

    You can go there to view the script the form is using for validation.

    the current phone field value name, is phone_no, which is the one I want to chanage.

    Thanks for any help
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    UK
    Posts
    262
    Rep Power
    19
    This is really very simple. Just make the 3 fileds submit their individual values as you normally would, then do this on the page that they get submitted to in order to 'combine' them:

    PHP Code:
    $phone_number $_POST['field1'] . "-" $_POST['field2'] . "-" $_POST['field3']; 
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    38
    Rep Power
    15
    Do I add that line into my script? or onto the html form page?

    Should I rename it from $phone_number to $phone_no , since that's the values I gave to the original form field [the one long form field] ?

    Also, is there a way to make sure the number entered is ONLY numbers, no alphanumeric?

    Thanks for your help!
  6. #4
  7. No Profile Picture
    Shine On Me
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Yes
    Posts
    55
    Rep Power
    16
    You make each field stand on their own. Like you would phone number as a whole or address or city.
    make 3 ones that look like this (but name is field2 or field3)
    <input type="text" name="field1" id="home_address" value = "">
    or however you have it in your html
    $phone_number = $_POST['field1'] . "-" . $_POST['field2'] . "-" . $_POST['field3'];
    add that line in your code
    is_numeric() to see if something is a number
    "Reality is slowly loosing its grip on me."
    - Slipping Grip
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    UK
    Posts
    262
    Rep Power
    19
    Yes, no, doesn't matter, and yes. In that order :P

    Firstly, this will need to go in the php script - it doesn't really matter where, so long as you define the variable before you need to use it.

    You can rename any of the variables as you want, I just gave an example. It does not make any difference to use $phone_number or $phone_no.

    Lastly, I have always used the 'is_numeric()' function to test that the data is actually numbers. You could use a code like this in your page to do all of this:

    PHP Code:
    <?
    if (is_numeric($_POST['field1']) && is_numeric($_POST['field2']) && is_numeric($_POST['field3']))
    {
        
    $phone_no $_POST['field1'] . "-" $_POST['field2'] . "-" $_POST['field3'];
    } else {
        echo 
    "Incorrect data inputted for the telephone number fields";
    }
    ?>
    That 'should' work, but I have not tested it. See how it does, and let me know if you have any further problems.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    38
    Rep Power
    15
    PHP Code:
    if_(is_numeric($_POST['field1'])_&&_is_numeric($_POST['field2'])_&&_is_numeric($_POST['field3']))

    {

    ____$phone_no_=_$_POST['field1']_._"-"_._$_POST['field2']_._"-"_._$_POST['field3'];

    }
    _else_{

    ____echo_"Incorrect data inputted for the telephone number fields";


    do i just take out the echo part if I want to use "print_error" instead?





    i will test it now, and tell you my results

    thanks so much for the help!
    Last edited by lazyjunk; October 21st, 2003 at 04:36 PM.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    UK
    Posts
    262
    Rep Power
    19
    You can put anything you like in the 'else' part of this structure. The purpose was just to define the $phone_no variable only if each of the fields were numbers, so that is all that really matters in the if-else structure. In fact, you do not even need to have an 'else' at all - I just find it good practice to display an error message.
  14. #8
  15. No Profile Picture
    Shine On Me
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Yes
    Posts
    55
    Rep Power
    16
    All the underscores for spaces is annoying.
    do i just take out the echo part if I want to use "print_error" instead?
    what is meant by this? if print_error is a command then I am not familiar with it. what you have should work.
    "Reality is slowly loosing its grip on me."
    - Slipping Grip
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    38
    Rep Power
    15
    PHP Code:
    <?
    if (is_numeric($_POST['field1']) && is_numeric($_POST['field2']) && is_numeric($_POST['field3']))
    {
        
    $phone_no $_POST['field1'] . "-" $_POST['field2'] . "-" $_POST['field3'];
    } else {
        echo 
    "Incorrect data inputted for the telephone number fields";
    }
    ?>
    That 'should' work, but I have not tested it. See how it does, and let me know if you have any further problems. [/B]

    it came up with a parse error.

    If you look at the php script text above, I put this code (and overwrote) the section of the script that validates the value $phone_no

    I changed field1, field2, field3, to phone1, phone2, phone3. Which is how the area code, prefix, suffix is defined in the html form.

    ?
  18. #10
  19. It
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,222
    Rep Power
    371
    Hmmm... if I remember rightly, is_numeric counts things containing . and - as numeric values, because they could be. If you're checking for a phone number and strictly want numbers and nothing else, you might want to use a simple regular expression for it instead? Maybe?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    38
    Rep Power
    15
    PHP Code:
    if_(!ereg("^[0-9]{3}$",$phone1)_&&_(!ereg("^[0-9]{3}$",$phone2)_&&_(!ereg("^[0-9]{3}$",$phone3))

    {

    ____$phone_no_=_$_POST['phone1']_._"-"_._$_POST['phone2']_._"-"_._$_POST['phone3'];

    }
    _else_{

    ____print_error("your <b>phone number</b> is invalid");


    i just tried this... & it came up w/a parse error also ??
  22. #12
  23. No Profile Picture
    Senior Member
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    Boston Ma.
    Posts
    1,529
    Rep Power
    0
    Hi


    I would just put the $variables phone (1, 2, 3) in an array then loop the array! Anyways here is the code the way you want to do it, I only changed ereg(); to preg_match();!


    Also get rid of the editor your using, because having to clean all those under_scores is a pain in the behind!

    PHP Code:

    <?

    if (!preg_match('/^[0-9]{3}$/'$_POST['phone1']) || 
    !
    preg_match('/^[0-9]{3}$/'$_POST['phone2']) || 
    !
    preg_match('/^[0-9]{3}$/'$_POST['phone3']))
    {
    echo 
    'your <b>phone number</b> is invalid';
    }
     else
         {

    echo 
    $_POST['phone1'] . '-' $_POST['phone2'] . '-' $_POST['phone3']; 

    }

    ?>

    F!
    Last edited by Fataqui; October 21st, 2003 at 06:58 PM.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    38
    Rep Power
    15
    my apologies for the underscores earlier, i need to find out what's causing that in my editor.


    I got a parse error again, using the code above ... I entered this code in the "phone validating" section of the php script I posted above [the text file with the complete php script code]


    PHP Code:
    // check for phone number validity

    if (!preg_match('/^[0-9]{3}$/'$_POST['phone1']) && 

    !
    preg_match('/^[0-9]{3}$/'$_POST['phone2']) &&

    !
    preg_match('/^[0-9]{3}$/'$_POST['phone3']))

    {

    echo_'your <b>phone number</b> is invalid';

    }

     else

          {



    echo_$_POST['phone1'] . '-' $_POST['phone2'] . '-' $_POST['phone3']; 




    ??
    Last edited by lazyjunk; October 21st, 2003 at 07:03 PM.
  26. #14
  27. No Profile Picture
    Senior Member
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    Boston Ma.
    Posts
    1,529
    Rep Power
    0
    Hi


    Sorry, I fixed my code above, I should of used (||) instead of (&&), copy the code above again and try that!


    Sleepy I am



    If that does not work, what is the error you are getting? Please post it!


    F!
    Last edited by Fataqui; October 21st, 2003 at 07:08 PM.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    38
    Rep Power
    15
    Now the form is submitting, but ...

    it doesn't validate if it's a numeric or not.

    Also, the email I'm getting has three different values ..

    phone1

    phone2

    phone3

    instead of consolidating them together

    i'm sleepy too :X
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo