Alex Alex - 11 months ago 57
SQL Question

Display value in a dedicated field selected from a list

in a php form I have the list of brands scrollable from a select field and I would modify the selected brand displaying it in a field beside.

The php form is this:




<?php
include '../sys/conn.php';
$brands = mysqli_query ($conn, "
*(query to select brands name and id)*
") or die ("Query not valid: " . mysqli_error($conn));
mysqli_close($conn);
?>
...
<form role="form" >
<label>Brands List</label>
<select class="form-control" name='brands list'>
<?php while ($listabrand=mysqli_fetch_array($brands)){
echo '<option>'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>
...
<label>Modify Brand</label>
<input type="text" name='brand-name' class="form-control" required placeholder="Brand Name to modify">
</form></html>


Basically I need to select the brand's name from the named "brands list" field and display it into the 'brand-name' in order to modify and save it.

Any help?

Answer Source

This is the right code.

<select class="form-control" name='brands-list'>
    <?php 
        while ($listabrand=mysqli_fetch_array($brands)){
        echo '<option value="'.$listabrand['1'].'">'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
    }?>
</select>

<div class="col-lg-8">
    <form role="form">
        <div class="form-group">
            <label>Marca</label>
            <input type="text" name='brand-name' class="form-control">
        </div>
    </form>
</div>

<script>
    $('select[name="brands-list"]').change(function(){  
        var selectedBrand = $(this).val();
        $('input[name="brand-name"]').val(selectedBrand);
    });
</script>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download