SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    new to php and im having trouble with validation

    im having trouble validating my drop boxes... if someone doesn't select anything i want an error message to appear on the next page.
    im able to notify the user if he hasn't written anything in the text box, how ever if i do write something im unable to pass the code over to the results page
    can anyone help me out
    heres my code


    PHP Code:

    <HTML>

    <HEAD>
    <TITLE>Anime Site Registration</TITLE>
    </HEAD>

    <BODY background="http://www.black-howling.com/images/wood1.gif">


    <p align="center"><font size="14" color="black" face="serif">
    Anime Form Sign Up</font></p>

    <FORM METHOD="POST" ACTION="animeresults.php">


       
    <BR><B>Name:</B>
    <BR><INPUT TYPE="TEXT" NAME="username">
    <BR>


    <!-- radio button -->

    <font size="5" color="black" face="serif">
    <BR>Gender</font>
    <BR><INPUT TYPE="RADIO" NAME="radiobutton"
       VALUE="Male">Male
    <BR><INPUT TYPE="RADIO" NAME="radiobutton"
       VALUE="Female">Female
    <BR>


    <BR><B>Birthday</B>
    <BR><INPUT TYPE="TEXT" NAME="birthday">
    <BR>

    <BR><B>Homeaddress</B>
    <BR><INPUT TYPE="TEXT" NAME="homeaddress">
    <BR>

    <BR><B>City</B>
    <BR><INPUT TYPE="TEXT" NAME="homecity">
    <BR>

    <BR><B>State</B>
    <BR><INPUT TYPE="TEXT" NAME="homestate">
    <BR>

    <BR><B>Zip</B>
    <BR><INPUT TYPE="TEXT" NAME="homezip">
    <BR>



    <!-- single select -->


    <font size="5" color="black" face="serif">
    <BR>Country</font>
    <BR><SELECT NAME="country">
    <OPTION VALUE="Nothing">-----Select Country-----
    <OPTION VALUE="Brazil">Brazil
    <OPTION VALUE="Canada">Canada
    <OPTION VALUE="China">China
    <OPTION VALUE="Cuba">Cuba
    <OPTION VALUE="Cyprus">Cyprus
    <OPTION VALUE="Japan">Japan
    <OPTION VALUE="Korea">Korea
    <OPTION VALUE="Philippines">Philippines
    <OPTION VALUE="Spain">Spain
    <OPTION VALUE="United States of America">United States of America

    </SELECT>


    <BR><B>House Phone</B>
    <BR><INPUT TYPE="TEXT" NAME="phone">
    <BR>

    <BR><B>Cell Phone</B>
    <BR><INPUT TYPE="TEXT" NAME="cell">
    <BR>

    <BR><B>Email</B>
    <BR><INPUT TYPE="TEXT" NAME="email">
    <BR>


    <BR><B>Favorite Website</B>
    <BR><INPUT TYPE="TEXT" NAME="website">
    <BR>

    <BR>
    <BR><B>Comments</B>
    <BR>
    <TEXTAREA NAME="textarea" ROWS="8" COLS=50"
     WRAP="physical">
    </TEXTAREA>



    <!-- multiple select -->

    <font size="5" color="black" face="serif">
    <BR>Favorite Author</font>
    <BR><SELECT MULTIPLE NAME="author[]">
    <OPTION VALUE="Akamatsu Ken">Akamatsu Ken
    <OPTION VALUE="Kaise Sosuke">Kaise Sousuke
    <OPTION VALUE="Maki Yoko">Maki Yoko
    <OPTION VALUE="Peach Pit">Peach Pit
    <OPTION VALUE="Takahashi Rumiko">Takahashi Rumiko
    <OPTION VALUE="Watsuki Nobuhiro">Watsuko Nobuhiro

    </SELECT>

    <BR>

    <!-- check box -->


    <font size="5" color="black" face="serif">
    <BR>Favorite Anime</font>
    <BR><INPUT TYPE="CHECKBOX" 
       NAME="show[]" value="Clover">Clover
    <BR><INPUT TYPE="CHECKBOX" 
       NAME="show[]" value="Eureka 7">Eureka 7
    <BR><INPUT TYPE="CHECKBOX" 
       NAME="show[]" value="Evangelion">Evangelion
    <BR><INPUT TYPE="CHECKBOX" 
       NAME="show[]" value="Full Metal Panic">Full Metal Panic
    <BR><INPUT TYPE="CHECKBOX" 
       NAME="show[]" value="Kenshin">Kenshin
    <BR><INPUT TYPE="CHECKBOX" 
       NAME="show[]" value="Mars">Mars
    <BR><INPUT TYPE="CHECKBOX" 
       NAME="show[]" value="Romeo X Juliet">Romeo X Juliet
    <BR><INPUT TYPE="CHECKBOX" 
       NAME="show[]" vaule="Vandread">Vandread

    <BR>





        <INPUT TYPE=hidden NAME="martial" VALUE="Anime Form">

    <?

        
    echo "<input type=hidden name=ip value=\"$REMOTE_ADDR\">\n"
        echo 
    "<input type=hidden name=browser value=\"$HTTP_USER_AGENT\">\n";
     
    ?>

    <BR>



    <BR><INPUT TYPE="RESET" VALUE="clear all fields">
    <BR>
    <BR><INPUT TYPE="SUBMIT" Name="Submit" VALUE="Proccess Information">
    <BR>


    </FORM>
    </BODY>
    </HTML>
    results

    PHP Code:
    <?php

         
    // File animeresults.php
        

         
    echo date("F j, Y, g:i a");
        echo 
    "<BR>";



             
    $errors=0;
             if (!
    trim($username))
             {
                echo 
    "<BR><B>Username</B> is required.";
                
    $errors++;

             }



    // Gender

    echo "<BR><B>Gender</B> is ";
    if (!
    $_REQUEST['radiobutton']) echo ("not selected."); 

    switch (
    $_REQUEST['radiobutton']) 

    case 
    "Male" : echo "Male"; break; 

    case 
    "Female" : echo "Female"; break; 






             if (!
    trim($birthday))
             {
                echo 
    "<BR><B>Birthday</B> is required.";
                
    $errors++;

             }

          
         if (!
    trim($homeaddress))
             {
                echo 
    "<BR><B>Home Address</B> is required.";
                
    $errors++;
            
         }

             if (!
    trim($homecity))
             {
                echo 
    "<BR><B>City</B> is required.";
                
    $errors++;
             
             }


             if (!
    trim($homestate))
             {
                echo 
    "<BR><B>State</B> is required.";
                
    $errors++;
             
             }


             if (!
    trim($homezip))
             {
                echo 
    "<BR><B>Zip</B> is required.";
                
    $errors++;
             
             }

    //Country

    echo "<BR><B>Country</B>";
    if( !isset( 
    $_POST['country'] ) || empty( $_POST['country'] ) ) 

        echo 
    'Error: You must select a country.'

    else 

        echo 
    'Success: You selected a country.'



    //phone



             
    if (!trim($phone))
             {
                echo 
    "<BR><B>Phone</B> is required.";
                
    $errors++;
             
             }

             if (!
    trim($cell))
             {
                echo 
    "<BR><B>Cell Phone</B> is required.";
                
    $errors++;
             
             }

             if (!
    trim($email))
             {
                echo 
    "<BR><B>Email</B> is required.";
                
    $errors++;
             
             }

             if (!
    trim($website))
             {
                echo 
    "<BR><B>Website</B> is required.";
                
    $errors++;
             
             }


    // multiple select


    //check box





        
    if ($errors 0)
           echo 
    "<BR><BR><BR>Please use your browser's back button ".
                
    "to return to the form, correct ";
        if (
    $errors == 1)
           echo 
    " the error, ";
        if (
    $errors 1)
           echo 
    "the errors, ";
        if (
    $errors 0)
           echo 
    "and re-submit the form.";



    ?>

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you don't expect your list of countries to grow to a huge amount, i would just hardcode them into an array like this:
    PHP Code:
     $countries = array('country1''country2''country3', ..etc ); 
    and then check that the submitted country is in fact allowed by you by checking if their submission is in your array:

    PHP Code:
     if (!in_array($_POST['country'], $countries) {
        echo 
    'please select a valid country';

    that should cover both; no country being selected and a user sending a fake request with garbage data. and at the same time keeping you code clean.

  3. #3
    Non-Member I87's Avatar
    Join Date
    Mar 2006
    Location
    UK
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Tom Brokaw View Post
    if you don't expect your list of countries to grow to a huge amount, i would just hardcode them into an array like this:
    PHP Code:
     $countries = array('country1''country2''country3', ..etc ); 
    and then check that the submitted country is in fact allowed by you by checking if their submission is in your array:

    PHP Code:
     if (!in_array($_POST['country'], $countries) {
        echo 
    'please select a valid country';

    that should cover both; no country being selected and a user sending a fake request with garbage data. and at the same time keeping you code clean.

    another way to do it is to make a new table in your database, dump all the countries in there and use a query to check if the country exists, there will be a lot less overhead.

  4. #4
    Non-Member I87's Avatar
    Join Date
    Mar 2006
    Location
    UK
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also, you should read up about normalizing your database (assuming you'll be storing the country as it's full form... eg "United Kingdom")


    you'll also save bandwidth, 'cause you wont have to have a select like

    PHP Code:
    <select name="country">
    <
    option value="United Kingdom">United Kingdom</option>
    </
    select
    instead something like

    PHP Code:
    <select name="country">
    <
    option value="1">United Kingdom</option>
    </
    select


    oh oh oh! and it'll be easier to validate!
    e.g.


    PHP Code:

    $country 
    = (int) $_POST['country'];

    if(
    $country && $country >= 230){
    die(
    'Invalid Country');


  5. #5
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im having trouble with my text box fields..

    i have the error message showing if the text field is empty.... but if the user inputs his data im having trouble showing the information..

    im trying if statements but im having trouble with it.

    can anyone help me.. thanks for everythign

    PHP Code:

    <BR><B>Name:</B
    <
    BR><INPUT TYPE="TEXT" NAME="username"
    <
    BR

    PHP Code:

             
    if (!trim($username))   echo "<BR><B>Username</B> is required.";
                
    $errors++;
             switch (
    $_REQUEST($username))

             {
                    echo 
    "<BR>Username&nbsp;=&nbsp;$textbox";

        echo 
    "<BR>";
             } 

  6. #6
    Non-Member I87's Avatar
    Join Date
    Mar 2006
    Location
    UK
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (trim($username)==''){

    echo 
    "<BR><B>Username</B> is required."
    $errors++; 

    //don't use request. globals = bad.
    //also why the switch?
    switch ($_POST['username']){

    echo 
    '<BR>Username&nbsp;=&nbsp;'$textbox'<br />';

    }} 


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
  •