Page 1 of 1

Dropdown selection without button [SOLVED]

Posted: Mon Feb 21, 2005 3:46 pm
by pthomas
Here's yet another item I've run into and am just stuck. I have a dropdown box thats built from PHP and I want to be able to git rid of the submit button and just be able to select an item from the dropdown and upon selection, have an action happen.

Here's my dropdown box:

Code: Select all

echo "<TABLE ALIGN=CENTER>\n";
        echo "<TR>\n";
        echo "<TD><P ALIGN=CENTER>Items to show per page</P></TD>\n";
        echo "<TD><FORM NAME="items_per_page" ACTION="".$_SERVER&#1111;'PHP_SELF']."" METHOD=POST>\n";
        echo "<SELECT NAME="items_to_show">\n";

        $list = array(5, 10, 25, 50, 100);
        foreach ($list as $inc)
        &#123;
                echo "\t\t\t<OPTION VALUE="$inc"";
                if ($inc == $_POST&#1111;'items_to_show'])            //Set user's preference as the one displayed
                        echo "SELECTED  >$inc\n";
                else
                        echo "  >$inc\n";
        &#125;
        echo "</SELECT></TD>\n";

        echo "<INPUT TYPE=HIDDEN NAME="category" VALUE="$category">\n";
        echo "<INPUT TYPE=HIDDEN NAME="mode" VALUE="".$_POST&#1111;'mode']."">\n";

        echo "<TD><INPUT TYPE=SUBMIT VALUE="Change"></TD>";
        echo "</FORM></TR></TABLE>\n";
I've been looking over the dropdown box tutorial here http://www.htmlcodetutorial.com/forms/_ ... hange.html but I couldn't put it all together.

I want to basically refresh the current page and pass the needed variables back to it:
items_to_show = <user selection via dropdown>
category = $category
mode = $_POST['mode']

This is what I had before I threw my hands up:

Code: Select all

echo "<TABLE ALIGN=CENTER>\n";
        echo "<TR>\n";
        echo "<TD><P ALIGN=CENTER>Items to show per page</P></TD>\n";
        echo "<TD><FORM NAME="items_per_page" ACTION="".$_SERVER&#1111;'PHP_SELF']."" METHOD=POST>\n";
        echo "<SELECT NAME="items_to_show" ONCHANGE="location = this.options&#1111;this.selectedIndex].value;">\n";

        $list = array(5, 10, 25, 50, 100);
        foreach ($list as $inc)
        &#123;
                echo "\t\t\t<OPTION VALUE="$inc"";
                if ($inc == $_POST&#1111;'items_to_show'])            //Set user's preference as the one displayed
                        echo "SELECTED  >$inc\n";
                else
                        echo "  >$inc\n";
        &#125;
        echo "</SELECT></TD>\n";
        echo "<INPUT TYPE=HIDDEN NAME="category" VALUE="$category">\n";
        echo "<INPUT TYPE=HIDDEN NAME="mode" VALUE="".$_POST&#1111;'mode']."">\n";
        echo "</FORM></TR></TABLE>\n";
It would just take me back to the main site. For instance, if this page is at http://www.something.com/admin.php then selecting the dropdown would redirect me to http://www.something.com and not back to the admin.php page.

From what I read that uses javascript. Is there a way to do it in PHP?

Any ideas?
Paul

Posted: Mon Feb 21, 2005 3:51 pm
by markl999
You should just be able to do a post using:
echo '<select name="items_to_show" onChange="submit();">';

That should just do anormal post and whatever selections were made should be posted as normal.

Posted: Mon Feb 21, 2005 3:57 pm
by pthomas
Worked like a charm!

Thanks,
Paul

Posted: Mon Feb 21, 2005 4:01 pm
by feyd
Moved to Client-side

Posted: Tue Feb 22, 2005 3:04 am
by CoderGoblin
You may also wish to use an HTML <noscript> tag to include a submit button next to the dropdown list. This would also allow those people you do not have javascript enabled to still process the information, whilst keeping it hidden for the majority of users.