SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session setting problem in PHP

    Hello all,

    I'm having a problem with three session variables that don't get set when they'r suposed to.

    I have three files involved:

    index.php
    functions.php
    global.php

    index.php contains the following at the begining:
    PHP Code:
    <?php
    include 'db.php';
    include 
    'global.php';
    include 
    'functions.php';
    ?>
    <html>
    <head>
    ...
    <?php
    if($HTTP_SESSION_VARS['yr'] != '' && $HTTP_SESSION_VARS['mk'] != '' && $HTTP_SESSION_VARS['md'] != ''){
         
    wehicle_products($HTTP_SESSION_VARS['yr'],$HTTP_SESSION_VARS['mk'],$HTTP_SESSION_VARS['md']);
        }
    ?>
    global.php contains:
    PHP Code:
    <?php
    session_start
    ();
    session_register('yr');
    session_register('mk');
    session_register('md');
    $yr $HTTP_POST_VARS['year'];
    $mk $HTTP_POST_VARS['make'];
    $md $HTTP_POST_VARS['model'];
    ?>
    and functions.php contains (among others):
    PHP Code:
    /********************************************************
    RAIL VEHICLE SELECTOR
    ********************************************************/
    function rail_vehicle_selector($session_year = '',$session_make = '',$session_model = ''){
     global $db;
     ?>
     <img src="/media/image/rail_title_active_vehicle.gif" width="125" height="16"><div id="rail_vehicle_selector">
     <table width="115" border="0" cellspacing="0" cellpadding="0">
      <tr>
       <td>
        <form action="<?php echo $PHP_SELF?>" method="post" enctype="multipart/form-data" name="rail_vehicle_selector_year">
         <select name="year" onChange="rail_vehicle_selector_year.submit();">
          <option value="">Year</option>
          <?php
          $year_sql
    =" (SELECT year 
             FROM vehicle,tire_fitment
             WHERE vehicle.vehicle_id=tire_fitment.vehicle_id
             ORDER BY year DESC)
             UNION
             (SELECT year
             FROM vehicle,wheel_fitment
             WHERE vehicle.vehicle_id=wheel_fitment.vehicle_id
             ORDER BY year DESC)"
    ;
          
    $year_rs=mysql_query($year_sql,$db);
          while(
    $year=mysql_fetch_array($year_rs)){
           echo 
    "   <option value=\"" $year['year'] . "\"";
           if(
    $year['year'] == $session_year){
            echo 
    "selected";
           }
           echo 
    ">" $year['year'] . "</option>\n";
          }
          
    ?>
         </select>
        <input name="make" type="hidden" value="">
        <input name="model" type="hidden" value="">
        <noscript>
        <input type="submit" name="Submit" value="Submit">
        </noscript>
        </form>
       </td>
      </tr>
      <tr>
       <td>
        <form action="<?php echo $PHP_SELF?>" method="post" enctype="multipart/form-data" name="rail_vehicle_selector_make">
          <?php
          
    if(isset($session_year) && $session_year != ''){//if cookies IS set and populated
           
    $make_sql=" (SELECT manufacturer.name AS manufacturer, vehicle.year AS year
              FROM vehicle,tire_fitment
              LEFT JOIN manufacturer ON manufacturer.manufacturer_id = vehicle.manufacturer_id
              WHERE vehicle.vehicle_id=tire_fitment.vehicle_id AND year = " 
    $session_year "
              ORDER BY manufacturer)
              UNION
              (SELECT manufacturer.name AS manufacturer, vehicle.year AS year
              FROM vehicle,wheel_fitment
              LEFT JOIN manufacturer ON manufacturer.manufacturer_id = vehicle.manufacturer_id
              WHERE vehicle.vehicle_id=wheel_fitment.vehicle_id AND year = " 
    $session_year "
              ORDER BY manufacturer)"
    ;
           
    $make_rs=mysql_query($make_sql,$db);
           echo 
    "<select name=\"make\" onchange=\"rail_vehicle_selector_make.submit();\">";
           echo 
    " <option value=\"\">Make</option>";
           while(
    $make=mysql_fetch_array($make_rs)){
            echo 
    "   <option value=\"" $make['manufacturer'] . "\"";
            if(
    $make['manufacturer'] == $session_make){
             echo 
    "selected";
            }
            echo 
    ">" $make['manufacturer'] . "</option>\n";
           }
          }else{
           echo 
    "<select name=\"make\" disabled>";
           echo 
    " <option value=\"\">Make</option>";
          }
          
    ?>
         </select>
         <input name="year" type="hidden" value="<?php echo $session_year?>">
         <input name="model" type="hidden" value="">
        <noscript>
        <input type="submit" name="Submit" value="Submit">
        </noscript>
        </form>
       </td>
      </tr>
      <tr>
       <td>
        <form action="<?php echo $PHP_SELF?>" method="post" enctype="multipart/form-data" name="rail_vehicle_selector_model">
         <?php
         
    if((isset($session_year) && $session_year != '') && (isset($session_make) && $session_make != '')){//if cookies IS set and populated
          
    $model_sql=" (SELECT manufacturer.name AS manufacturer,model.name AS model 
              FROM vehicle,tire_fitment
              LEFT JOIN manufacturer ON manufacturer.manufacturer_id = vehicle.manufacturer_id
              LEFT JOIN model ON model.model_id = vehicle.model_id
              WHERE vehicle.vehicle_id=tire_fitment.vehicle_id AND vehicle.year = " 
    $session_year " AND manufacturer.name = '" $session_make "'
              ORDER BY year)
              UNION
              (SELECT manufacturer.name AS manufacturer,model.name AS model 
              FROM vehicle,wheel_fitment
              LEFT JOIN manufacturer ON manufacturer.manufacturer_id = vehicle.manufacturer_id
              LEFT JOIN model ON model.model_id = vehicle.model_id
              WHERE vehicle.vehicle_id=wheel_fitment.vehicle_id AND vehicle.year = " 
    $session_year " AND manufacturer.name = '" $session_make "'
              ORDER BY year)"
    ;
          
    $model_rs=mysql_query($model_sql,$db);
          echo 
    "<select name=\"model\" onchange=\"rail_vehicle_selector_model.submit();\">";
          echo 
    " <option value=\"\">Model</option>";
          while(
    $model=mysql_fetch_array($model_rs)){
           echo 
    "   <option value=\"" $model['model'] . "\"";
           if(
    $model['model'] == $session_model){
            echo 
    "selected";
           }
           echo 
    ">" $model['model'] . "</option>\n";
          }
         }else{
          echo 
    "<select name=\"model\" disabled>";
          echo 
    " <option value=\"\">Model</option>";
         }
          
    ?>
         </select>
         <input name="year" type="hidden" value="<?php echo $session_year?>">
         <input name="make" type="hidden" value="<?php echo $session_make?>">
        <noscript>
        <input type="submit" name="Submit" value="Submit">
        </noscript>
        </form>
       </td>
      </tr>
     </table>
     </div>
    <?php
    }
    /********************************************************
    For some reason when you first select a year the page reloads but doesn't show the selected year. You have to select it again and then it works.

    It's driving me crazy.. and the worst thing is I'm sure it's a stupid little thing but I'm not seeing it...

    Does anyone know what's happening?

    Thank you.
    Luis

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For some reason when you first select a year the page reloads but doesn't show the selected year. You have to select it again and then it works.
    My view is that this is happening due to the first initial page load, the session variable used for year is null - ie empty.

    In your index.php page you have the following:

    PHP Code:
    .
    .
    .
    <?php
    if($HTTP_SESSION_VARS['yr'] != '' && $HTTP_SESSION_VARS['mk'] != '' && $HTTP_SESSION_VARS['md'] != ''){
         
    wehicle_products($HTTP_SESSION_VARS['yr'],$HTTP_SESSION_VARS['mk'],$HTTP_SESSION_VARS['md']);
        }
    ?> 
    .
    .
    This as I see it will define your session variables you want if they're not already defined, correct ?

    Well, why not define these with some kind of 'default' value - a year in this case to see if that solves the problem ?

    As looking over your script it appears to be sound.

  3. #3
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OOPS! I'm sorry what i actually have on index.php is:
    PHP Code:
    rail_vehicle_selector($HTTP_SESSION_VARS['yr'],$HTTP_SESSION_VARS['mk'],$HTTP_SESSION_VARS['md']); 
    Luis


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
  •