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

$_SERVER['PHP_SELF']

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.

<?php
...
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>
</select>
</form>
_END;
...
?>

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 *