www.webdeveloper.com
Results 1 to 10 of 10

Thread: checkbox into sessions

  1. #1
    Join Date
    Mar 2007
    Posts
    91

    checkbox into sessions

    Ok i have a 3 page form that I am creating for a site and on page2 (aka step2.php) I have 30 checkboxes each representing a type of food. The customer will check all boxes that apply of foods they make at their establishment. I need to find a way to take all checked checkboxes and insert them into a session to be displayed onto "page4" and then able to be changed if needed (go back to page2 to make corrections/changes) and once complete I need to insert all the checked checkboxes into my db. Now I have taken many swings at this with no luck yet. Here is my code.
    This is the form...
    PHP Code:
    <form method="POST" enctype="multipart/form-data" name="cuisine" action="step3.php">
    <
    div align="left"><h2><font color="#b80101">Types of Cuisine</font></h2></div>
    <
    fieldset align="center">
    <
    table>
    <
    tr>
    <
    td><input type="checkbox" name="food[]" value="afghan"/></td><td>Afghan</td>
    <
    td><input type="checkbox" name="food[]" value="caribbean"/></td><td>Caribbean</td>
    <
    td><input type="checkbox" name="food[]" value="greek"/></td><td>Greek</td>
    <
    td><input type="checkbox" name="food[]" value="korean"/></td><td>Korean</td>
    <
    td><input type="checkbox" name="food[]" value="pizza"/></td><td>Pizza</td>
    <
    td><input type="checkbox" name="food[]" value="swedish"/></td><td>Swedish</td>

    </
    tr>

    <
    tr>
    <
    td><input type="checkbox" name="food[]" value="american"/></td><td>American</td>
    <
    td><input type="checkbox" name="food[]" value="chinese"/></td><td>Chinese</td>
    <
    td><input type="checkbox" name="food[]" value="indian"/></td><td>Indian</td>
    <
    td><input type="checkbox" name="food[]" value="lebanese"/></td><td>Lebanese</td>
    <
    td><input type="checkbox" name="food[]" value="peruvian"/></td><td>Peruvian</td>
    <
    td><input type="checkbox" name="food[]" value="sweets"/></td><td>Sweets</td>

    </
    tr>

    <
    tr>
    <
    td><input type="checkbox" name="food[]" value="appetizers"/></td><td>Appetizers</td>
    <
    td><input type="checkbox" name="food[]" value="cuban"/></td><td>Cuban</td>
    <
    td><input type="checkbox" name="food[]" value="irish"/></td><td>Irish</td>
    <
    td><input type="checkbox" name="food[]" value="mediterranean"/></td><td>Mediterranean</td>
    <
    td><input type="checkbox" name="food[]" value="portuguese"/></td><td>Portuguese</td>
    <
    td><input type="checkbox" name="food[]" value="thai"/></td><td>Thai</td>

    </
    tr>

    <
    tr>
    <
    td><input type="checkbox" name="food[]" value="brazilian"/></td><td>Brazilian</td>
    <
    td><input type="checkbox" name="food[]" value="french"/></td><td>French</td>
    <
    td><input type="checkbox" name="food[]" value="italian"/></td><td>Italian</td>
    <
    td><input type="checkbox" name="food[]" value="mexican"/></td><td>Mexican</td>
    <
    td><input type="checkbox" name="food[]" value="southwestern"/></td><td>South Western</td>
    <
    td><input type="checkbox" name="food[]" value="turkish"/></td><td>Turkish</td>

    </
    tr>

    <
    tr>
    <
    td><input type="checkbox" name="food[]" value="cajun&creole"/></td><td>Cajun Creole</td>
    <
    td><input type="checkbox" name="food[]" value="german"/></td><td>German</td>
    <
    td><input type="checkbox" name="food[]" value="japanese"/></td><td>Japanese</td>
    <
    td><input type="checkbox" name="food[]" value="middle_eastern"/></td><td>Middle Eastern</td>
    <
    td><input type="checkbox" name="food[]" value="spanish"/></td><td>Spanish</td>
    <
    td><input type="checkbox" name="food[]" value="vietnamese"/></td><td>Vietnamese</td>

    </
    tr>
    </
    table>
    </
    fieldset>
    <
    a href="step3.php"><img src="img/step3.gif" alt="" border="0"></a>
    </
    form
    So first do I need to do name="food[]" for all check boxes?
    second, how do I now go about capturing all checked boxes displying the text for each of them onto another page and then set it up to be inserted into a db?

    Any help on this would be greatly appreciated since all attempts I have made have failed.
    thanks for all the help.

  2. #2
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    When the form is submitted, only checkboxes that are checked are submitted in the query. So you can just loop through each "food[]" and assign it to the session.
    PHP Code:
    session_start()
    foreach (
    $_POST["food[]"] as $val) {
      
    $_SESSION['foods'][] = $val;

    Then $_SESSION['foods'] will be an array containing all the foods.

  3. #3
    Join Date
    Mar 2007
    Posts
    91
    Ok I put that up, then on page4 (to display the info) I put this...
    PHP Code:
    <?php if( isset($_SESSION['foods']) ) echo $_SESSION['foods']; ?>
    But I get this error for some reason...
    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\viewable\hsrevised\live\header.php on line 9
    any ideas?

  4. #4
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    Sorry, there was an error in my syntax. Also, you should do a check to make sure there were foods selected. It should be:
    PHP Code:
    if ($_POST['food']) {
      foreach (
    $_POST["food"] as $val) {
        
    $_SESSION['foods'][] = $val;
      } 

    This should be at the top of each page, not just page 4, as the POST values will disappear after the next page is submitted.


    Then for testing purposes, on page 4 you can have something like:
    PHP Code:
    if ($_SESSION['foods']) {
      
    print_r($_SESSION['foods']);

    That will print information about the variable/array. Then when you want to print all of the foods, use a foreach loop like I did to print each individual food item.
    PHP Code:
    if ($_SESSION['foods']) {
      print 
    "<h2>Selected Foods</h2>";
      print 
    "<ul>";

      foreach(
    $_SESSION['foods'] as $val) {
        print 
    "<li>$val</li>";
      }
      print 
    "</ul>";


  5. #5
    Join Date
    Mar 2007
    Posts
    91
    Well I dont know if it will matter or not but I am doing an include on all 4 pages to a header...this is my header incase you need to see it or if this would cause problems...

    PHP Code:
    <?php session_start(); ?>
    <?php
    foreach( $_POST as $__key => $__val )
    {
        
    $_SESSION['form'][$__key] = $__val;
    }
    ?>
    <?php
    if ($_POST['food']) { 
      foreach (
    $_POST["food"] as $val) { 
        
    $_SESSION['foods'][] = $val
      } 
    }
    ?>
    <?php
    session_start
    ();
    foreach( 
    $_POST as $__key => $__val )
    {
        
    $_SESSION['form3'][$__key] = $__val;
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>

    <title>Twincitieshotspot.com - -Your source for the hottest places in Minnesota-</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <link href="css/styles.css" rel="stylesheet" type="text/css">
    <link href="css/account.css" rel="stylesheet" type="text/css">
    <!--[if IE]>
    <link href="ie_styles.css" rel="stylesheet" type="text/css">
    <![endif]-->

    </head>

    <body>
    <div id="outer">
    <div id="head">

    <div id="banner"><a href="index.php"><img src="img/567x100.jpg" alt="" border="0"></a></div>

        <div id="search"> 
          <ul>
            <li><a href="signup.php">Sign Up</a> |</li>
            <li><a href="login.php">Login</a> </li>
          </ul>
          <table align="right" cellpadding="0" cellspacing="0">
            <tr> 
              <td valign="top"><a href="search.php"><img src="go_but.jpg" alt="" border="0"></a></td>
              <td valign="top"> 
    <form action="search.php" method="get" name="search">
    <input onclick="this.value='';" name="q" type="text" value="Place, City, Zip, Area Code" style="width:200px">
    </form>
    </td>
            </tr>
          </table>
        </div>
    </div>

    <div class="redline">&nbsp;</div>
    <?php
    $menu 
    file_get_contents("links_nav.php");
    $menu preg_replace("|<li><a href=\"" basename($_SERVER['PHP_SELF']) . "\">(.*)</[^>]+></li>|U""<li class=\"current\">$1</li>"$menu);
    echo 
    $menu;
    ?>
    Thanks for your help

  6. #6
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    1) session_start() has to be called before you can do anything with the $_SESSION superglobal. (edit, just saw your including it at the top, dunno why your calling it a second time?).
    2) There's no reason to prepend the variables with "__". The $key and $val variables will not exist outside the foreach statement. From the PHP manual:
    PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.
    3) Your looping through POST 3 times, each time assigning it two a new second-dimensional array in the $_SESSION superglobal. This is a bit redundant.

    Here's how it should be done:
    PHP Code:
    <?php
      session_start
    ()
      foreach(
    $_POST as $key => $val) {
        if (
    $key == "food") {
          
    $_SESSION['foods'][] = $val;
        } else {
          
    $_SESSION['forms'][$key] = $val;
        }
      }

  7. #7
    Join Date
    Mar 2007
    Posts
    91
    Your looping through POST 3 times,
    i was starting to wonder why some of my other stuff wasnt working. Here is the deal. I have 3 forms, each on their own page. I need to put it all into sessions to be displayed onto a "final" page where all the data from all 3 forms will be displayed, then all the data inserted into my db. So if I have 3 forms how would I go about doing this the right way?
    i have included a zip file so you can view it all and so i dont take all sorts of space in here.

    thanks for your help
    Attached Files Attached Files

  8. #8
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    Use sessions to store the user's location in the forms. Then just forward them accordingly using a process page. So on the link to this form, set it to "process.php". Also, all of the forms should have an action of "process.php".

    "process.php":
    PHP Code:
    session_start();
    if (!
    $_SESSION['step']) {
      
    $_SESSION['step'] == 0;
    }

    switch(
    $_SESSION['step']) {
      case 
    1:
        foreach(
    $_POST as $key=>$val) {
          
    $_SESSION[$key] = $val;
        }
        break;
      case 
    2:
        foreach (
    $_POST['food'] as $val) {
          
    $_SESSION['foods'][] = $val;
        }
        break;
      case 
    3:
        foreach(
    $_POST as $key=>$val) {
          
    $_SESSION['form3'][$key] = $val;
        }
        break;
    }
    $_SESSION['step']++;
    header("Location:step"$_SESSION['step'] .".php"); 
    This is a really simple form script, and I think your making it a little over complicated. Simplicity is key.

  9. #9
    Join Date
    Mar 2007
    Posts
    91
    Ok I have process.php now and have changed the action of my forms to "process.php" and got rid of the session code out of my header file. Now this session stuff is still a bit new to me so I hope im not driving you nuts yet. So now I have just a few questions...

    Use sessions to store the user's location in the forms.
    How exactly do sessions get used to "store a users location"? I have never heard of this before.

    So on the link to this form
    Just so its clear, what link are you refering to, and to what form, considering i technicaly have 3 forms?

    Thank you much again for your help. I greatly appreciate it.

  10. #10
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    By storing their location in the forms, I mean remembering how many forms they have completed, and forwarding them appropriately. Ie, if they complete form 2, then for some reason browse away from your page, then browse back later (as long as their browser hasn't been closed or the session hasn't expires), they will be redirected to form 3 when they click on the link to the forms.

    By "links to the form", I mean any links on your website (menu's, inline, anyplace there's an <a>), point them to process.php instead of "form1.php", "form2.php", etc.

    While I'm thinking about it, you could put a link on step4.php that says "Start Over" or something.
    step4.php:
    HTML Code:
    <a href="process.php?reset=1">Start Over</a>
    process.php:
    PHP Code:
    session_start();
    if (!
    $_SESSION['step'] || $_GET['reset'] == 1) {
      
    $_SESSION['step'] = 0//edit: this was incorrect on the one i posted earlier
    //i used $_SESSION['step'] == 0.
    }

    //then the rest stays the same 
    Last edited by TJ111; 12-11-2007 at 09:59 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles