SitePoint Sponsor

User Tag List

Page 1 of 4 1234 LastLast
Results 1 to 25 of 76

Hybrid View

  1. #1
    SitePoint Addict mak-uk's Avatar
    Join Date
    Dec 2001
    Location
    Midlands, UK
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Quick question.. thanks :)

    Hi

    Could someone enlighten me as to how to validate whether a select box option has been chosen. For example, with the box below I want the user to select their gender:
    PHP Code:
    <tr>
        <
    td>Gender</td>
        <
    td><p>
          <
    label>      
          <
    select name="gender_select" id="gender_select">
       <
    OPTION SELECTED VALUE="">---------------</OPTION>
            <
    option value="M">Male</option>
            <
    option value="F">Female</option>
          </
    select>
          </
    label>
          <
    br>
        </
    p></td>
      </
    tr
    Thanks.

    Mak

  2. #2
    SitePoint Evangelist Aska's Avatar
    Join Date
    Aug 2003
    Location
    Melbourne, Australia
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mak, I assume you want to do the validation in PHP? If so it may look something like this:
    PHP Code:
    $valid_gender = array( 'M''F');
    if( 
    in_array$_REQUEST['gender_select'], $valid_gender)) {
      
    // valid gender selected, do something
    }
    else {
      
    // invalid gender.
      
    die( "Please specify your gender.");


  3. #3
    SitePoint Addict mak-uk's Avatar
    Join Date
    Dec 2001
    Location
    Midlands, UK
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Is there a way of doing it without the array (I am sure I have seen it but can't think where)?

    join_form.php has the select box and it posts to join_code.php. It is in join_code.php that I want the validation code.

    Thanks.

    Mak

  4. #4
    SitePoint Evangelist Aska's Avatar
    Join Date
    Aug 2003
    Location
    Melbourne, Australia
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mak-uk
    Hi

    Is there a way of doing it without the array (I am sure I have seen it but can't think where)?

    join_form.php has the select box and it posts to join_code.php. It is in join_code.php that I want the validation code.

    Thanks.

    Mak
    Certainly, although I find validation with arrays more flexible (when you need to add/modify acceptable values) There are many much better, full blown form validation scripts out there, unlike the following code which should only be used in non-mission-critical web apps:
    PHP Code:
    if( $_REQUEST['gender_select'] == 'M' || $_REQUEST['gender_select'] == 'F') {
      
    // okay, do something
    }
    else {
      
    // bad
      
    die( 'Please select your gender');
    }

    /* To illustrate other types of simple validation, assume we have 2 more fields: name and email */

    /* Check Name */
    if( strlen$_REQUEST['name']) > 3) {
      
    // okay do something
    }
    else {
      
    // bad
    }

    /* Check Email */
    $ereg_string "^[\'+\\./0-9A-Z^_\`a-z{|}~\-]+@" "[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+){1,3}$"
    /* above taken from PHP manual, user contributed notes */
    if( eregi$ereg_string$_REQUEST['email'])) {
      
    // good email
    }
    else {
      
    // bad email


  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ($_POST['gender_select'] == 'M' || $_POST['gender_select'] == 'F') {
       
    // user selected their gender
    }
    else {
       
    // user did not select their gender

    Another way of writing that if condition which will work just as well (but which does not reveal the purpose of the data validation as well as the above)
    PHP Code:
    if ( $_POST['gender_select'] ) 
    We can use that shorter condition because in PHP an zero length string will evaluate to false and a non-zero length string will evaluate to true.

  6. #6
    SitePoint Addict mak-uk's Avatar
    Join Date
    Dec 2001
    Location
    Midlands, UK
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    freakysid.. is there a way of saying:

    if empty {
    print error msg?
    }

    Thanks.

    Mak

  7. #7
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    United States
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if (empty($var)) {
    echo "whatever";
    }

  8. #8
    SitePoint Addict mak-uk's Avatar
    Join Date
    Dec 2001
    Location
    Midlands, UK
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thanks for that people.

    What I have used so far is:
    PHP Code:
    // Validate Forename
    if (!ereg ("[a-zA-Z]{1,}"$Forename)) {
    $ErrorArray["Forename"] = "Please enter a Forename consisting of alphabetical characters.";
    }
    // Validate Surname
    if (!ereg ("[a-zA-Z]{1,}"$Surname)) {
    $ErrorArray["Surname"] = "Please enter a Surname consisting of alphabetical characters.";
    }
    // Validate E-Mail1
    if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$EMail1)) {
    $ErrorArray["EMail1"] = "E-Mail address not valid! Please try again.";
    }
    if (!
    eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$EMail)) {
    $ErrorArray["EMail"] = "Re-entered E-Mail address not valid! Please try again.";
    }
    if (
    $EMail !== $EMail1) { 
    $ErrorArray["EMailRes"] = "Email addresses do not match!"

    // Validate Gender
    if (empty($gender_select)) {
    $ErrorArray["Gender"] = "Gender not selected!"

    However, what I was wondering is:

    1. I did not write the first four regular expressions above.. I can't see amywhere, where, they take into account length?

    2. Seperately, if I wanted to make sure the length of a text field was between 6 and 20 characters, for example, how would I go about this?

    Thanks.

    Mak

  9. #9
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Answering the second question (I am not a regex person):

    $length = strlen($myString);

    if ( $length < 6 || $length > 20 )

  10. #10
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first two regex require a string which is atleast 1 char long. The second two just check for valid email adresses, you don't really need some min/max char count in them, do you?
    Anyway, to have a max character count with the names add a number after {1,

  11. #11
    SitePoint Addict mak-uk's Avatar
    Join Date
    Dec 2001
    Location
    Midlands, UK
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Okay, what I have done for the forename and surname is allow a minimum of 1 character and a maximum of 25 characters:
    PHP Code:
    // Validate Forename
    if (!ereg ("[a-zA-Z]{1,25}"$Forename)) {
    $ErrorArray["Forename"] = "Please enter a Forename consisting of alphabetical characters.";

    As for the rest, I may want it so that a certain field, such as 'mUsername' is at least 6 characters but no more than 20. Using what freakysid said above, could I do:
    PHP Code:
    if (strlen($mUsername 6) || ($mUsername) > 20) {
    $ErrorArray["Username"] = "Please enter a Username of at least 6 characters.";

    ??

    Thanks.

    Mak

  12. #12
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE=mak-uk]
    PHP Code:
    if (strlen($mUsername 6) || ($mUsername) > 20) {
    $ErrorArray["Username"] = "Please enter a Username of at least 6 characters.";

    [QUOTE]
    PHP Code:
    if (strlen($mUsername) < 6) || strlen($mUsername) > 20) {
    $ErrorArray["Username"] = "Please enter a Username of at least 6 characters.";


  13. #13
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You forgot the strlen after the OR operator, but yes, after adding it, it would work.

  14. #14
    SitePoint Addict mak-uk's Avatar
    Join Date
    Dec 2001
    Location
    Midlands, UK
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thanks, fixed that and also $Forename which should be $mForename.

    Secondly, in regards to $mUsername being between 6 and 20 characters, should I stipulate anything else? At the moment, this is the only condition I have and then I do:
    PHP Code:
    $mUsername stripslashes($mUsername); 
    Is this sufficient for a username?

    Thanks.

    Mak

  15. #15
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just don't forget the mysql_escape_string before using the data in a SQL query.

  16. #16
    SitePoint Addict mak-uk's Avatar
    Join Date
    Dec 2001
    Location
    Midlands, UK
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    How do I go about using that?

    Thanks.

    Mak

  17. #17
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sql 'SELECT * FROM foo WHERE bar = \''.mysql_escape_string($value).'\''

  18. #18
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    freakysid
    You forgot the strlen after the OR operator, but yes, after adding it, it would work.
    Thanks, fixed that and also $Forename which should be $mForename.

  19. #19
    SitePoint Addict mak-uk's Avatar
    Join Date
    Dec 2001
    Location
    Midlands, UK
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thanks all.

    freakysid, shouldn't that be:
    PHP Code:
    if ((strlen($mUsername) < 6) || (strlen($mUsername) > 20)) { 
     
    $ErrorArray["Username"] = "Please enter a Username of at least 6 characters."

    ?

    Gaheris, so I use the mysql escape string when I want to use a query on anything I have 'stripped slashes' on?

    For example, I have:
    PHP Code:
    $sql_email_check mysql_query("SELECT mEMail FROM members WHERE mEMail='$mEMail'"); 
    $sql_username_check mysql_query("SELECT mUsername FROM members WHERE mUsername='$mUsername'"); 
    $email_check mysql_num_rows($sql_email_check); 
    $username_check mysql_num_rows($sql_username_check); 
    So I can use it on the above as earlier I do:
    PHP Code:
    $mEMail stripslashes($mEMail); 
    $mUsername stripslashes($mUsername); 
    So then it would become:
    PHP Code:
    $sql_email_check mysql_query("SELECT mEMail FROM members WHERE mEMail=\''.mysql_escape_string($mEMail).'\''"); 
    $sql_username_check mysql_query("SELECT mUsername FROM members WHERE mUsername=\''.mysql_escape_string($mUsername).'\''"); 
    $email_check mysql_num_rows($sql_email_check); 
    $username_check mysql_num_rows($sql_username_check); 
    Is that right??

    Thanks.

    Mak

  20. #20
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The theory is right, but you will have to check your ' and ". You got them a bit wrong.
    PHP Code:
    $sql_email_check mysql_query('SELECT mEMail FROM members WHERE mEMail=\''.mysql_escape_string($mEMail).'\''); 

  21. #21
    SitePoint Addict mak-uk's Avatar
    Join Date
    Dec 2001
    Location
    Midlands, UK
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thanks for that

    Second thing is, if I want to display a form again (if a user has entered data the first time but there are errors in it) and I want the form to now show again but with the correct fields already filled in.. how do I go about this?

    Thanks.

    Mak

  22. #22
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do this for example
    PHP Code:
    function safe_post($_index)
    {
        return (isset(
    $_POST[$_index]) ? $_POST[$_index] : '';
    }

    echo 
    '<input name="foo" value="'.safe_post('foo').'" />'

  23. #23
    SitePoint Addict mak-uk's Avatar
    Join Date
    Dec 2001
    Location
    Midlands, UK
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Would you be able to explain that with a short example please?

    Say for example if I want to re-display the form field "Forename".

    Thanks.

    Mak

  24. #24
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And I thought I posted an example.

    PHP Code:
    /** ...
      * Okay, some fields are invalid, let's have the user edit them
      * The form data is still in the $_POST array
      */
    echo '<input name="age" value="'.safe_post('age').'" />'
    echo 
    '<input name="location" value="'.safe_post('location').'" />'

  25. #25
    SitePoint Addict mak-uk's Avatar
    Join Date
    Dec 2001
    Location
    Midlands, UK
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Would that change at all for select boxes?

    For example, what I have is:
    PHP Code:
    <?php
    include_once "./inc/db.php";
    // ------------------- START DROP-DOWN BOX QUERIES -------------------
     
    $createdby_query  mysql_query("SELECT createdbyID, createdbyName FROM createdby") or die(mysql_error());
    ...
    etc
     
    function safe_post($_index

        return (isset(
    $_POST[$_index]) ? $_POST[$_index] : '';

    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "[url="http://www.w3.org/TR/html4/loose.dtd"]http://www.w3.org/TR/html4/loose.dtd[/url]">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <form name="form1" method="post" action="join_code.php">
    <table width="600" border="1">
      <tr>
        <td>Profile created by </td>
        <td><select name="createdby_select" id="createdBy">
          <OPTION SELECTED VALUE=value="'.safe_post('createdby_select').'"></OPTION>
        </select></td>
      </tr>
    ...etc
    Then I get this error:

    Parse error: parse error, unexpected ';' in d:\inetpub\wwwroot\site\join_form_reloaded.php on line 18

    Which is referring to this line:
    PHP Code:
    return (isset($_POST[$_index]) ? $_POST[$_index] : ''
    Any ideas on what I am doing wrong?

    Thanks.

    Mak
    Last edited by mak-uk; Dec 9, 2003 at 18:00.


Bookmarks

Posting Permissions

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