1

Hi I'm performing the following query on a database which is getting me a dropdown menu. I want this to query the database onchange so that I can use that information in another form.

$dropdown_sql="SELECT product_id, product_name, unit_price, unit_quantity, in_stock FROM products";
$dropdown_result=mysql_query($dropdown_sql);

$options="";

while ($row=mysql_fetch_array($dropdown_result)) {
    $id=$row["product_id"];
    $product_name=$row["product_name"];
    $unit_price=$row["unit_price"];
    $unit_quantity=$row["unit_quantity"];
    $in_stock=$row["in_stock"];
    $options.="<OPTION VALUE=\"$id\">$product_name - $unit_quantity";
}
?>

The dropdown menu:

<SELECT NAME=product_name onchange="dropdown_change()">
<OPTION VALUE=0>Select a food
    <?php echo $options ?>
    </SELECT>

I can detect the change in javascript

function dropdown_change()
{
  alert("hello")
}

BUT I can't get the information from that point, and the I can't put it in the form. Please help.

1
  • 1
    To communicate between your onchange javascript and the php code which queries database, you need either post the values on change, or use Ajax calls. Commented Jul 16, 2013 at 1:15

2 Answers 2

1

Add this in select dropdown.

change the select form with:

<select name="product_name" onchange="dropdown_change(this)">
...................
</select>

<script type="text/javascript">
    function dropdown_change(el){
        var elValue = el.value;
    }
</script>

Test: JSFiddle

Sign up to request clarification or add additional context in comments.

Comments

1

If you are using jquery library,

function dropdown_change(){
    var val = $("select[name=product_name]").val();
    $.ajax({
        data : {key:val},
        url : 'your_url',
        success : function(msg){
            // do what ever you want
        }
    })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.