Ok so i an very new to this site and am trying my luck with PHP. Got a great idee to make some cash but now to implement it is a whole new game for me.

I want to keep the selceted value of the select box after the form has been submited with dynamic data from my db(mysql).
i had a look at http://www.daniweb.com/web-development/php/threads/360284/php-keep-dropdown-box-data-after-posting-fails but can't get it to work with my db and site.

my code :

<form action="#" method="post">
                        <select id="prov" name="prov">
                        <?php    $sql = "SELECT distinct province from escorts ORDER BY province ASC";
                                $query = mysql_query( $sql );
                                while( $row = mysql_fetch_assoc($query) )
                                    {
                                    echo "<option value=\"$row[province]\""; 
                                    echo "if($_POST['prov']=='$row[province]')";
                                    echo 'selected="selected">'
                                    echo "$row[province]</option>";
                                    }
                        ?>
                        </select>
                        <input name="submit" type="submit" value="Select"/>
                    </form>

I get the folowing error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /home/m7593340/public_html/search.php on line 79

Hope this helps

For one thing you are missing quotes arround associative index names. Since the array elements are complex variables you should also enclose them in curly braces to get them parsed properly in a double quoted string. Another issue is that you echo the if condition which is not right. Then you are missing a space before selected and the angle bracket after the selected goes to the next echo statement (otherwise it might get lost). See my attempt at the correct code:

echo "<option value=\"{$row['province']\""; 
if($_POST['prov'] == $row['province']) {
    echo ' selected="selected"'
}
echo ">{$row['province']}</option>";

Thanks man will check it in the morning. Will post back.

So it works fine 1 or 2 very minor changes and......

<form action="#" method="post">
                            <select id="prov" name="prov">
                                    <?php    


                                        $sql = "SELECT distinct province from escorts ORDER BY province ASC";
                                        $query = mysql_query( $sql );
                                        while( $row = mysql_fetch_array($query) )
                                        {

                                        echo "<option value=\"$row[province]\"";
                                        if ($_POST['prov']==$row['province']) {echo "selected='selected'"; } ;
                                        echo ">$row[province]</option>";
                                        }
                                    ?>
                            </select>
                        </form>
Be a part of the DaniWeb community

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