icue Posted July 2, 2012 Share Posted July 2, 2012 Hi, Is it possible to call a javascript function from a php generated selection menu? I am trying to update a list of music venues based on the genre selected from a dropdown selection menu. A list member (Barand) was very kind and provided me with the following code to generate the menu from a database. <?php $sql = "SELECT idgenre, genre_name FROM genre ORDER BY genre_name"; $res = mysql_query($sql); echo "<select name='genre' id='genre'> <option value='0'>- select genre -</option>"; while (list($id, $name) = mysql_fetch_row($res)) { echo "<option value='$id'> $name</option>"; } echo "</select>\n"; ?> In order to update the list without reloading the page I need to call a javascript function: onchange="selectGenre(this.value)" I have tried various version of the following: echo "<select name='genre' id='genre' <script type='text/javascript'> onchange='selectGenre(this.value)' '</script>' and I have searched many sites but have not found a solution that works. Maybe I am not entering the correct search term, so could someone please offer some suggestions. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/265099-how-to-execute-a-javascript-function-from-a-php-generated-selection-menu/ Share on other sites More sharing options...
cpd Posted July 2, 2012 Share Posted July 2, 2012 <select onchange="selectGenre(this.value);"> Quote Link to comment https://forums.phpfreaks.com/topic/265099-how-to-execute-a-javascript-function-from-a-php-generated-selection-menu/#findComment-1358499 Share on other sites More sharing options...
icue Posted July 2, 2012 Author Share Posted July 2, 2012 Hi, Thanks for the reply. While I was waiting for a reply, I had an idea which I tried and it worked. Not sure if it is the right but it works. I put the start of the <select> outside of the php like this: <form> <select name="genre" id="genre" onchange="selectGenre(this.value)" > <?php require_once '../includes/db_connect.php'; $sql = "SELECT idgenre, genre_name FROM genre ORDER BY genre_name"; $res = mysql_query($sql); echo "<option value='0'>select a genre</option>"; while (list($id, $name) = mysql_fetch_row($res)) { echo "<option value='$id'> $name</option>"; } echo "</select>\n"; ?> </form> and everything works fine. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/265099-how-to-execute-a-javascript-function-from-a-php-generated-selection-menu/#findComment-1358502 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.