Skip to content

How to Submit Form to Current Page on Change with PHP and JavaScript


You might want the web page to change when users change the menu. Here I introduce a PHP code snippet incorporated with Javascript to achieve the goal.

echo <<<_END
<form name='change_favorite' method='post' action='{$_SERVER['PHP_SELF']}'>
<select name='menu' onChange='this.form.submit()' >
<option value='f'>foo</option>
<option value='b'>bar</option>

In case some users turned off JavaScript and make this approach fail, you should add a submit button to make web pages robust.

Leave a Reply

Your email address will not be published. Required fields are marked *