christopher larsen christopher larsen - 1 year ago 74
SQL Question

change input value based on <select> PHP/Mysql

Been looking for a few days for the answer and even tho I think this should be simple, I just can't seem to make it work.

First I populate a "select" with values from my database with a while loop.

<select class="admin-content-select" name="sale">
<?php try {
$stmt = $db->prepare('SELECT saleID, saleKal, saleNr FROM wcms_sale');
$stmt->execute(array(':saleKal' => $row['saleKal']));
while($sale = $stmt->fetch()){
echo '<option value="'.$sale['saleKal'].'">'.$sale['saleKal'].'</option>';
}catch(PDOException $e) {
echo $e->getMessage();

then I have an input field which I need to get populated with a value based on what is selected. The values are on the same row in the db so my initial thought was that it would be easy to say: "when X row is selected, echo Y" but since it's a while loop it will simply echo a lot of input fields.

Any work arounds or am I totally in the wrong here with the while loop?

Hope I explained this right and thanks beforehand for any answer :)

Answer Source

You can use jQuery

$('.admin-content-select').change(function () {
        var content = $(this).val();
        if(content == 'A')
            $('#id_of_input').val('A is selected');
        else if(content == 'B')
            $('#id_of_input').val('B is selected');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download