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.   

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.