checkboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theinvertedkid
Forum Newbie
Posts: 1
Joined: Thu Jan 01, 2004 11:50 am
Location: Illinois
Contact:

checkboxes

Post by theinvertedkid »

Hello,

I am in the middle of putting together a Nascar fantasy racing site. I am new to php, so I couldn't create the script so I paid Cory Schoop to get the job done. He has done VERY well, but I told him wrong at the beginning, and now I am trying to fix it. Instead of having 8 drop down menus to select your drivers I wanted each driver to have a check box, and have them select 8 drivers that way. This would prevent someone picking two of the same driver. If you have a better way with drop down menus to stop people from selecting the same driver two times, I would be happy to hear it also. Anyways, I have tried to modify the code, but its not exactly working. I will attach both the original, and my attempt to change it. On mine each driver, no matter who you choose is put into the database as "0". Each driver is assigned a value, and "0" is nobody, so that obviously is not working. Anyways, here is the code:

This is my editted version that does not work.

Code: Select all

function saveteam($submit, $teamname, $first, $last, $address, $city, $state, $zip, $email, $driver ) { 
     
    connection(); 
    print("<a href=index.php>Main Menu - saveteam</a><br><br>"); 

    if($submit)     
    { 
        // VALIDATE DATA 
        $goahead = 'yes';$onceonly='0'; 
        for($x=1;$x<=8;$x++) 
        { 
        if($driver[$x]=='foo') 
            {$goahead = 'no'; 
            if($onceonly=='0'){print("Please select " . count($driver) . " drivers<br>");$onceonly='1';} 
            } 
        } 
        if(strlen($teamname) < 4 || strlen($teamname) > 15) 
        { 
             print 'Your Team Name must contain 4 to 15 characters.<br>'; 
        $goahead = 'no'; 
        } 
        if(!preg_match('/^[A-Za-z0-9 \_]+$/',$teamname)) 
        { 
         print 'Your Team Name can only contain the following: capital letters, lower case letters, 0-9 numbers, space, the underscore<br>'; 
        $goahead = 'no'; 
        } 
        if(strlen($first) < 4 || strlen($first) > 15) 
        { 
             print 'Your First Name can only contain 4 to 15 characters.<br>'; 
        $goahead = 'no'; 
        } 
        if(!preg_match("/[A-Za-z]+/",$first)) 
        { 
             print 'Your First Name may only contain capital letters and lower case letters.<br>'; 
        $goahead = 'no'; 
        } 
        if(strlen($address) < 5 || strlen($address) > 100) 
        { 
             print 'Your Address can only contain 5 to 20 characters.<br>'; 
        $goahead = 'no'; 
        } 
        if(strlen($city) < 1 || strlen($city) > 20) 
        { 
             print 'Your City can only contain 1 to 20 characters.<br>'; 
        $goahead = 'no'; 
        } 
        if(!preg_match('/^[A-Za-z]+$/',$city)) 
        { 
             print 'Your City may only contain capital letters and lower case letters.<br>'; 
        $goahead = 'no'; 
        } 
        if(strlen($zip) < 5 || strlen($zip) > 10) 
        { 
             print 'Your Zip Code can only contain 5 to 10 characters.<br>'; 
        $goahead = 'no'; 
        } 
        if(!preg_match('/^[0-9 \-]+$/',$zip)) 
        { 
             print 'Your Zip Code may only contain numbers 0-9 and the hyphen.<br>'; 
        $goahead = 'no'; 
        } 
        if(!preg_match('/^[a-z0-9&''\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*?[a-z]+$/is',$email)) 
        { 
             print 'Your E-mail is invalid.<br>'; 
        $goahead = 'no'; 
        } 
        // this is the check for the salary cap 
        if( !check_salary_cap($driver) ){ 
            $goahead = 'no'; 
        } 
        if($goahead == 'no'){print("<br><br>Please press back and try again<br>");die;} 
        // END DATA VALIDATION 

    // INSERT THE RECORD 
    $query_next_id = "SELECT id FROM teams ORDER BY id DESC"; 
    $query_next_id_res = mysql_query($query_next_id); 
    $next_id_row = mysql_fetch_array($query_next_id_res, MYSQL_ASSOC); 
    $the_next_id = $next_id_row["id"]+1; 

    //echo "NEXT ID: " . $the_next_id; 
    $query = "insert into teams values('$the_next_id', '$teamname', '$first', '$last', '$address', '$city', '$state', '$zip', '$email', '', 
    '$driver[0]', '$driver[1]','$driver[2]', '$driver[3]', '$driver[4]', '$driver[5]', '$driver[6]', '$driver[7]')"; 

    //echo "<br />" . $query; 
    mysql_query($query); 

    $query_team_points = "INSERT INTO teampoints VALUES ('$the_next_id', '$teamname', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)"; 
    mysql_query($query_team_points); 
    //echo $query_team_points; 

    // PRINT A HAPPY MESSAGE 
    print("Thanks a lot man for inserting that team<br>"); 

    }     
} // end of saveteam 
//**************************************************************************** 

function addteam() { 
    connection(); 
    print("<a href=index.php>Main Menu - addteam</a><br><br>"); 
    print(" 

    <html> 
    <body> 

    Please fill in all required fields! 
      <form method="post" action="saveteam.php"> 
      Team Name:<input type="Text" name="teamname"><br> 
      First Name:<input type="Text" name="first"><br> 
      Last Name:<input type="Text" name="last"><br> 
      Address:<input type="Text" name="address"><br> 
      City:<input type="Text" name="city"><br> 
      State:<select name="state"> 
    "); 

    // I KNOW THIS WAS ****TY WAY TO DO STATES BUT I ALREADY HAD THIS ARRAY 
    $abbrev_array[] = "AL";$abbrev_array[] = "AK";$abbrev_array[] = "AZ";$abbrev_array[] = "AR";$abbrev_array[] = "CA";$abbrev_array[] = "CO";$abbrev_array[] = "CT";$abbrev_array[] = "DE";$abbrev_array[] = "FL";$abbrev_array[] = "GA";$abbrev_array[] = "HI";$abbrev_array[] = "ID";$abbrev_array[] = "IL";$abbrev_array[] = "IN";$abbrev_array[] = "IA";$abbrev_array[] = "KS";$abbrev_array[] = "KY";$abbrev_array[] = "LA";$abbrev_array[] = "ME";$abbrev_array[] = "MD";$abbrev_array[] = "MA";$abbrev_array[] = "MI";$abbrev_array[] = "MN";$abbrev_array[] = "MS";$abbrev_array[] = "MO";$abbrev_array[] = "MT";$abbrev_array[] = "NE";$abbrev_array[] = "NV";$abbrev_array[] = "NH";$abbrev_array[] = "NJ";$abbrev_array[] = "NM";$abbrev_array[] = "NY";$abbrev_array[] = "NC";$abbrev_array[] = "ND";$abbrev_array[] = "OH";$abbrev_array[] = "OK";$abbrev_array[] = "OR";$abbrev_array[] = "PA";$abbrev_array[] = "RI";$abbrev_array[] = "SC";$abbrev_array[] = "SD";$abbrev_array[] = "TN";$abbrev_array[] = "TX";$abbrev_array[] = "UT";$abbrev_array[] = "VT";$abbrev_array[] = "VA";$abbrev_array[] = "WA";$abbrev_array[] = "WV";$abbrev_array[] = "WI";$abbrev_array[] = "WY"; 

    for($s=0;$s<count($abbrev_array);$s++) 
    {print("<option>$abbrev_array[$s]</option>");} 

    print("</select> 
    <br> 
      Zip Code:<input type="Text" name="zip"><br> 
      Email:<input type="Text" name="email"><br> 
    "); 

         
        { 
        // The value here ties in to the driver id for the database tables.     
        print(" 
     
            Please select 8 Drivers:<br> 
            <input type="checkbox" name="jgordon" value="1">Jeff Gordon #24 
            <input type="checkbox" name="rwallace" value="2">Rusty Wallace #2 
            <input type="checkbox" name="mkenseth" value="3">M. Kenseth #17 
            <input type="checkbox" name="tstewart" value="4">T. Stewart #20 
            <input type="checkbox" name="mmartin" value="5">M. Martin #6 
            <input type="checkbox" name="blabonte" value="6">B. Labonte #18 
            <input type="checkbox" name="tlabonte" value="7">T. Labonte #5 
            <input type="checkbox" name="djarret" value="8">D. Jarret #88 




<br> 
        "); 
         
        } // end of the for 
     
    print(" 
      <input type="Submit" name="submit" value="Enter information"> 
      </form> 
    </body> 
    </html> 
    "); 
}


Here is the original version.

Code: Select all

function saveteam($submit, $teamname, $first, $last, $address, $city, $state, $zip, $email, $driver ) { 
     
    connection(); 
    print("<a href=index.php>Main Menu - saveteam</a><br><br>"); 

    if($submit)     
    { 
        // VALIDATE DATA 
        $goahead = 'yes';$onceonly='0'; 
        for($x=1;$x<=8;$x++) 
        { 
        if($driver[$x]=='foo') 
            {$goahead = 'no'; 
            if($onceonly=='0'){print("Please select " . count($driver) . " drivers<br>");$onceonly='1';} 
            } 
        } 
        if(strlen($teamname) < 4 || strlen($teamname) > 15) 
        { 
             print 'Your Team Name must contain 4 to 15 characters.<br>'; 
        $goahead = 'no'; 
        } 
        if(!preg_match('/^[A-Za-z0-9 \_]+$/',$teamname)) 
        { 
         print 'Your Team Name can only contain the following: capital letters, lower case letters, 0-9 numbers, space, the underscore<br>'; 
        $goahead = 'no'; 
        } 
        if(strlen($first) < 4 || strlen($first) > 15) 
        { 
             print 'Your First Name can only contain 4 to 15 characters.<br>'; 
        $goahead = 'no'; 
        } 
        if(!preg_match("/[A-Za-z]+/",$first)) 
        { 
             print 'Your First Name may only contain capital letters and lower case letters.<br>'; 
        $goahead = 'no'; 
        } 
        if(strlen($address) < 5 || strlen($address) > 100) 
        { 
             print 'Your Address can only contain 5 to 20 characters.<br>'; 
        $goahead = 'no'; 
        } 
        if(strlen($city) < 1 || strlen($city) > 20) 
        { 
             print 'Your City can only contain 1 to 20 characters.<br>'; 
        $goahead = 'no'; 
        } 
        if(!preg_match('/^[A-Za-z]+$/',$city)) 
        { 
             print 'Your City may only contain capital letters and lower case letters.<br>'; 
        $goahead = 'no'; 
        } 
        if(strlen($zip) < 5 || strlen($zip) > 10) 
        { 
             print 'Your Zip Code can only contain 5 to 10 characters.<br>'; 
        $goahead = 'no'; 
        } 
        if(!preg_match('/^[0-9 \-]+$/',$zip)) 
        { 
             print 'Your Zip Code may only contain numbers 0-9 and the hyphen.<br>'; 
        $goahead = 'no'; 
        } 
        if(!preg_match('/^[a-z0-9&''\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*?[a-z]+$/is',$email)) 
        { 
             print 'Your E-mail is invalid.<br>'; 
        $goahead = 'no'; 
        } 
        // this is the check for the salary cap 
        if( !check_salary_cap($driver) ){ 
            $goahead = 'no'; 
        } 
        if($goahead == 'no'){print("<br><br>Please press back and try again<br>");die;} 
        // END DATA VALIDATION 

    // INSERT THE RECORD 
    $query_next_id = "SELECT id FROM teams ORDER BY id DESC"; 
    $query_next_id_res = mysql_query($query_next_id); 
    $next_id_row = mysql_fetch_array($query_next_id_res, MYSQL_ASSOC); 
    $the_next_id = $next_id_row["id"]+1; 

    //echo "NEXT ID: " . $the_next_id; 
    $query = "insert into teams values('$the_next_id', '$teamname', '$first', '$last', '$address', '$city', '$state', '$zip', '$email', '', 
    '$driver[0]', '$driver[1]','$driver[2]', '$driver[3]', '$driver[4]', '$driver[5]', '$driver[6]', '$driver[7]')"; 

    //echo "<br />" . $query; 
    mysql_query($query); 

    $query_team_points = "INSERT INTO teampoints VALUES ('$the_next_id', '$teamname', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)"; 
    mysql_query($query_team_points); 
    //echo $query_team_points; 

    // PRINT A HAPPY MESSAGE 
    print("Thanks a lot man for inserting that team<br>"); 

    }     
} // end of saveteam 
//**************************************************************************** 

function addteam() { 
    connection(); 
    print("<a href=index.php>Main Menu - addteam</a><br><br>"); 
    print(" 

    <html> 
    <body> 

    Please fill in all required fields! 
      <form method="post" action="saveteam.php"> 
      Team Name:<input type="Text" name="teamname"><br> 
      First Name:<input type="Text" name="first"><br> 
      Last Name:<input type="Text" name="last"><br> 
      Address:<input type="Text" name="address"><br> 
      City:<input type="Text" name="city"><br> 
      State:<select name="state"> 
    "); 

    // I KNOW THIS WAS ****TY WAY TO DO STATES BUT I ALREADY HAD THIS ARRAY 
    $abbrev_array[] = "AL";$abbrev_array[] = "AK";$abbrev_array[] = "AZ";$abbrev_array[] = "AR";$abbrev_array[] = "CA";$abbrev_array[] = "CO";$abbrev_array[] = "CT";$abbrev_array[] = "DE";$abbrev_array[] = "FL";$abbrev_array[] = "GA";$abbrev_array[] = "HI";$abbrev_array[] = "ID";$abbrev_array[] = "IL";$abbrev_array[] = "IN";$abbrev_array[] = "IA";$abbrev_array[] = "KS";$abbrev_array[] = "KY";$abbrev_array[] = "LA";$abbrev_array[] = "ME";$abbrev_array[] = "MD";$abbrev_array[] = "MA";$abbrev_array[] = "MI";$abbrev_array[] = "MN";$abbrev_array[] = "MS";$abbrev_array[] = "MO";$abbrev_array[] = "MT";$abbrev_array[] = "NE";$abbrev_array[] = "NV";$abbrev_array[] = "NH";$abbrev_array[] = "NJ";$abbrev_array[] = "NM";$abbrev_array[] = "NY";$abbrev_array[] = "NC";$abbrev_array[] = "ND";$abbrev_array[] = "OH";$abbrev_array[] = "OK";$abbrev_array[] = "OR";$abbrev_array[] = "PA";$abbrev_array[] = "RI";$abbrev_array[] = "SC";$abbrev_array[] = "SD";$abbrev_array[] = "TN";$abbrev_array[] = "TX";$abbrev_array[] = "UT";$abbrev_array[] = "VT";$abbrev_array[] = "VA";$abbrev_array[] = "WA";$abbrev_array[] = "WV";$abbrev_array[] = "WI";$abbrev_array[] = "WY"; 

    for($s=0;$s<count($abbrev_array);$s++) 
    {print("<option>$abbrev_array[$s]</option>");} 

    print("</select> 
    <br> 
      Zip Code:<input type="Text" name="zip"><br> 
      Email:<input type="Text" name="email"><br> 
    "); 

        for($y=1;$y<=8;$y++) 
        { 
        // The value here ties in to the driver id for the database tables.     
        print(" 
      Driver #$y:<select name="driver[]"> 
            <option value=foo>Select Driver</option> 
            <option value="1">Jeff Gordon #24</option> 
            <option value="2">R. Wallace #2</option> 
            <option value="3">M. Kenseth #17</option> 
            <option value="4">T. Stewart #20</option> 
            <option value="5">M. Martin #6</option> 
            <option value="6">B. Labonte #18</option> 
            <option value="7">T. Labonte #5</option> 
            <option value="8">D. Jarret #88</option> 
            <option value="9">Cory</option> 
            </select><br> 
        "); 
         
        } // end of the for 
     
    print(" 
      <input type="Submit" name="submit" value="Enter information"> 
      </form> 
    </body> 
    </html> 
    "); 
}
Also, for testing purposes there are only 8 drivers on there. Once I get it to work, there will be around 50.

Thanks everyone! Would love to hear any ideas if you think I am going about this wrong.

Bye
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

There are a couple ways to do it. One is with a multiple selection box:

Code: Select all

Driver <select name="driver&#1111;]" multiple size=10> 
<option value=0>Select Driver</option> 
<option value="1">Jeff Gordon #24</option> 
<option value="2">R. Wallace #2</option> 
<option value="3">M. Kenseth #17</option> 
<option value="4">T. Stewart #20</option> 
<option value="5">M. Martin #6</option> 
<option value="6">B. Labonte #18</option> 
<option value="7">T. Labonte #5</option> 
<option value="8">D. Jarret #88</option> 
<option value="9">Cory</option> 
</select><br>
That presents a list box, allows multiple selections selections and returns an array of numbers. It allows more than eight, but you can deal with that on arrival.

The other is chech boxes, but they all have to have the same name:

Code: Select all

Please select 8 Drivers:<br> 
<input type="checkbox" name="driver&#1111;]" value="1">Jeff Gordon #24 
<input type="checkbox" name="driver&#1111;]" value="2">Rusty Wallace #2 
<input type="checkbox" name="driver&#1111;]" value="3">M. Kenseth #17 
<input type="checkbox" name="driver&#1111;]" value="4">T. Stewart #20 
<input type="checkbox" name="driver&#1111;]" value="5">M. Martin #6 
<input type="checkbox" name="driver&#1111;]" value="6">B. Labonte #18 
<input type="checkbox" name="driver&#1111;]" value="7">T. Labonte #5 
<input type="checkbox" name="driver&#1111;]" value="8">D. Jarret #88
That presents a list, allows multiple selections selections and also returns an array of numbers. It too allows more than eight, but ....
LazarusCrusader
Forum Newbie
Posts: 6
Joined: Thu Jan 15, 2004 12:57 pm

How do you deal with this?

Post by LazarusCrusader »

How do you work with the post information to get all the chosen items into a database. Do you parse them out?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

well, sorta but not really ;)

you could do something like this

Code: Select all

foreach ($_POST['driver'] as $driver)
{
   $sql = "INSERT INTO mytable (myfield) VALUES ('".$driver."')";
   $result = mysql_query($sql) or die(mysql_error());
}
probably isn't exactly right, but you get the idea ( at school and can't test... ).
LazarusCrusader
Forum Newbie
Posts: 6
Joined: Thu Jan 15, 2004 12:57 pm

Post by LazarusCrusader »

So, I could just use the foreach and build up a variable with the results, and then use the variable as input into the database?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

well, the foreach just converts the array of your post driver to a variable (in this case $driver).

then, while you are in that while loop, you can just post that value of the variable at that point and time into a query like above.

if that's what you were trying to say, then your answer is yes.
LazarusCrusader
Forum Newbie
Posts: 6
Joined: Thu Jan 15, 2004 12:57 pm

Post by LazarusCrusader »

Code: Select all

foreach($_POST&#1111;'StateDesired'] as $value)
&#123; 
 $desired .= $value . ", ";
&#125;
$sql .= "userStrState = '" . $desired . "',";
I'm trying this but getting this error:
Invalid argument supplied for foreach()

Any ideas?
Post Reply