Sylvain
Sylvain

Reputation: 95

Get value from select with jquery

On my homepage (home.php) I have a first script who take some result of php page (test.php) and display on div #details-menu. It's the list of "product" table from database. After when result is selected, I would like to validate it and display on alert. But it doesn't work... Some idea to help me ?

Here my code :

HOME.PHP (jquery code)

// First display the list of idcat
$(".displaymenu").on('click', function()
{
            var idcat=1;
            $.ajax({                                                            
            type: "GET",
            url: "test.php",
            data: "idcat="+idcat+"",
            success: function(msg){
            $("#details-menu").html(msg);
                } 
            });
}); 

// Second validate the choice after selected one product
$('#details-menu').on('click', '.validproduct', function() {
var idproduct=$(this).attr("choice_idproduct");
alert(idproduct);
}); 

HOME.PHP (html code) :

<div id="details-menu"></div>

TEST.PHP :

<?php
$idcat=$_GET['idcat'];
echo '<select id="choice_idproduct">';
    $result = mysql_query("select * from myproduct where idcat='$idcat'");
    while ($r_prod = mysql_fetch_array($result_prod)) 
    {
        echo '<option value="'.$r_prod['idproduct'].'">'.$r_prod['nameproduct'].'</option>';
    }
echo '</select>';
echo '<div class="validproduct">VALIDATE</div>';
?>

Upvotes: 0

Views: 47

Answers (1)

vaso123
vaso123

Reputation: 12391

You are trying to get an attribute of your div, what is not exists. #choice_idproduct is the child of the div, not an attribute.

Get the value of the select instead.

Try this:

var idproduct=$("#choice_idproduct").val();

Upvotes: 1

Related Questions