Jump to content

How to execute a javascript function from a php generated selection menu


icue

Recommended Posts

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.   

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.