Jump to content

Recommended Posts

It's amazing the discoveries you make when you add large amounts of data to something you've tested already. "It used to work, and now it doesn't".

I have a query that uses the value from a combo box to make a list...ie the user selects a department from the combo box and gets all items located in that department. I list 25 items per page, but when i go to my next page, I lose all my data and my combo box resets itself to the default value. I can still scroll through the pages of blank, but that's not real helpful.

I was thinking that maybe a session variable would work for this, but I'm new to it all so I'm not sure if I'm looking at the wrong kind of solution. Plus I'm not sure that my implementation of _session was correct.

Here's how I have it set now:
$currentPage = $_SERVER["PHP_SELF"];
$selectedDepartment = $_POST['cmbSelect'];

and my query just uses the variable as part of a conditional:
WHERE inv_inventory.departmentID = '$selectedDepartment'

Any guidance would be appreciated.
Link to comment
https://forums.phpfreaks.com/topic/10437-retain-combp-value-for-query/
Share on other sites

you might want to check for the following:
1. FORM method is POST and not GET
2. select box is named "cmbSelect" (I know, but just in case ;])
3. say you have this links to move to the next page: Page 1 2 3 4 .....
do you link them as <a href='<?echo $_SERVER["PHP_SELF"]."?page=$ctr"?>'>
if yes, then your passing your next page in a query string, so test for it using GET
something like
[code]

$selectedDepartment = 1; // default value

if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['cmbSelect']))
{
   $selectedDepartment = intval($_POST['cmbSelect']);
}
elseif ($_SERVER['REQUEST_METHOD']=='GET' && isset($_GET['page']))
{
   $selectedDepartment = intval($_GET['page']);
}
if ($selectedDepartment ==0) $selectedDepartment = 1; //just in case something went wrong above
[/code]
Thanks so much for helping! That does exactlly what I was trying to do. But something I didn't expect happens now. The combo box value still resets to default. I thought I could just use <?php echp $_POST['cmbSelect']; ?> but that killed the page and using $selectedDepartment is what reverts it to the default value. It's not a huge deal, but it could be confusing if you don't remember what item you had done the search on...say I searched in Billing, but when I got to page 4 of the Billing Department, it would have been reset to Accounting...so mindgames so say the least.

Your help would be MUCH appreciated again.

THANKS!

A little premature happiness I'm afraid. When I went and check it, the value in the combo box changing alters the results of the query. So if I search for a department, then the values do query corerectly, but if I move to the next page, the combo box resets. Now if I go back to page one, the values displayed are for that default combo value instead of what I serached for.
do this to your select box

[code]
<select name='cmbSelect'>
<?php
     $sel_options = "";
$result = mysql_query("SELECT dept_id, department FROM tbl_departments ORDER BY department ASC ");
while ($row = mysql_fetch_assoc($result))
{
    $selected = "";
    if ($dept_ds[$ctr]==$selectedDepartment) $selected = " selected ";
    $sel_options .= "<option value='".$row['dept_id']."' $selected>".$row['department']."</option>";    
}
echo $sel_options;
?>
</select>
[/code]

just made up the fields on the SQL above, what it does is it selects the previously chosen department
Here's what I have right now. Maybe you could guide me from that code?

[code]
<select name="cmbSelect" id="cmbSelect">
<?php
    do{  
?>
<option value="<?php echo $row_rsCategory['categoryID']?>"<?php if (!(strcmp($row_rsCategory['categoryID'], "$selectedCategory"))) {echo "SELECTED";} ?>><?php echo $row_rsCategory['categoryDesc']?></option><?php
}
    while ($row_rsCategory = mysql_fetch_assoc($rsCategory));
    $rows = mysql_num_rows($rsCategory);
    if($rows > 0)
    {
        mysql_data_seek($rsCategory, 0);
        $row_rsCategory = mysql_fetch_assoc($rsCategory);
    }
?>
</select>
[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.