Hey Guys,

I build an array, which pulls its information from a database. Here is the code:

                             FROM TABLE
                             WHERE END_DATE = '".$daterange."'";                    
$ORIGRP = odbc_exec($db, $querygrp);                
while (odbc_fetch_row($ORIGRP))
    $filtergrp[] = (string)(trim(odbc_result($ORIGRP, "GRP")));

This works perfectly and reads in the correct values. I am using the $filtergrp array to populate a drop down box with this code:

echo "<select NAME =selectedgrp onChange='submit();'>";      
echo "<option value = ''>SELECT VALUE</option>";
echo "<option value = 'ALL' ".($filtergrp=='ALL'? "SELECTED":"").">ORIG (ALL)</option>";
    foreach($filtergrp as $g1)
        if($g1 == $filtergrp)
            echo "<option value = ".$g1." selected>".$g1."</option>";                         
            echo "<option value = ".$g1.">".$g1."</option>";
echo "</select>";
echo "</td>";        

This populates the drop down box perfectly with the correct values. The problem is when a value is selected in the drop down box it doesn't return the correct value for two word values, such as, New York. It would only return the value New. Im not really sure where the problem is, but it seems like a pretty easy fix. Im a newbie to php and programming in general so I probably left some helpful information out. If so just ask for any needed clarification.

I appreciate the help.

Recommended Answers

All 6 Replies

The problem stems from the fact you're not wrapping HTML attribute values in quotes.

Replace lines 8 and 9 with the following respectively.

echo '<option value="'.$g1.'" selected="selected">'.$g1.'</option>';
echo '<option value="'.$g1.'">'.$g1.'</option>';

Oh, and I also believe line 4 should be as follows, as you're currently comparing the value to the array containing all of your options:

if($g1 == $_POST['filtergrp'])

Thanks for the help. That fixed it!!

if($g1 == $_POST['filtergrp'])

It didn't like this portion. Changed the code around, but couldnt get it to accept it. Any ideas?

Hmmm... not sure then without seeing the full code. I'd assumed this script was run immediately after form submission.

I only raised the point because you're using the variable $filtergrp on lines 3, 4 and 6, with lines 3 and 6 being comparisons with an option, whereas line 4 is an array of values built from the query.

I figured it out. I was using $_REQUEST up above instead of $_POST. Not sure what it changed but is working fine. Is that more of a correct coding practice?

The only other problem I am having with that drop down box is that it resets after the value is selected. It reads in the value, but instead of staying on the selected value it resets to the default. Do you know why that might be?

Thanks again.

Genearally the use of $_GET and $_POST are preferred to $_REQUEST, as $_REQUEST is sometimes disabled in server configurations, hence your code will not work.

And correcting line 6, perhaps if($g1 == $_REQUEST['filtergrp']) would resolve the issue of the select field resetting after submission.

Although that's working on the basis that you're displaying the select field on the same page to which the form is originally submitted. With the source code you've posted thus far, it isn't possible to tell.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.