Dropdown selection without button [SOLVED]

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Dropdown selection without button [SOLVED]

Post 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
Last edited by pthomas on Tue Feb 22, 2005 8:32 am, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post by pthomas »

Worked like a charm!

Thanks,
Paul
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Moved to Client-side
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Post Reply