tags:

views:

54

answers:

2

Alright so I have this drop down populated by a Mysql DB. On change of the drop down the whole page populates information from a the DB. Well what I want when I submit this form (there are two forms on this page) I want it to return to the same client that I just had selected in the drop down. Right now I'm using a session to bring it back and select that same client as before but the on change event is not kicking in and selecting the information out of my data base for the person selected. Thanks

 session_start();

$current = isset($_SESSION['ClientNamefour']) ? $_SESSION['ClientNamefour'] : 0;

$options4=""; 

while ($row = mysql_fetch_array($result)) { 

    $id=$row["Client_Code"]; 
    $thing=$row["Client_Full_Name"];
    $value="$id, $thing";
    // insert SELECTED="SELECTED" if the current $id matches $current
    $options4.="<OPTION VALUE=\"$value\" ".($id == $current ? ' SELECTED="SELECTED"' : '').">".$thing; 
} 


?>

<FORM name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

<SELECT NAME="ClientNamefour" OnChange="this.form.submit()">

    <OPTION VALUE=0>Client
    <?php echo $options4?> 

  </SELECT>
</FORM>


   session_start();
// Do the redirect
    $_SESSION['ClientNamefour'] = $_POST['txtclientcode'];



header("Location: http://endeavor/php/financialoasistest.php");
A: 

Ok, there's quite a few issues in there, but general code structure aside:

  1. You're setting the session variable from a POST value that won't exist. (It should be $_POST['ClientNamefour'] according to your code.)

  2. You're calling session_start() twice for some reason. (Just call it once at the very beginning.)

middaparka
The redirect is also after HTML content is output the user agent.
John Conde
The reason for the session_start() being twice is because the code resides on two different pages. One process page and one is the actual website. The POST does exist later on the page. How do I make the onchange work with a session coming back?
Eric
If that's the case, you need to re-format the code above into two segments and add the (relevant) missing bits - it's hard to tell what's what at the moment and it's impossible to debug code we can't see. :-)
middaparka
A: 

If you set $_SESSON['ClientNamefour'] = $_POST['ClientNamefour'] on the process page, something like this should do when constructing the options for the <select>.

while ($row = mysql_fetch_array($result)) 
{
    $id = $row["Client_Code"]; 
    $key = "$id, $thing";
    $value = $row["Client_Full_Name"];

    $selected = ($id == @$_SESSION['ClientNamefour']) ? ' selected' : '';

    echo "<option value=\"{$key}\"{$selected}>{$value}</option>";
}

Edit: Seeing your comment on middaparka's answer, I updated this. I too assumed all your code was on one page.

Atli