as part of my project I have two files:
In File 1. the brands are listed in a select field and displayed in a separated field that allows to modify the brand's name. The file 2. contains the query that update the DB.
The problem consists of passing the ID of the selected brand to the UPDATE query stored in update_brand.php in order to identify the record to update.
May be I am approaching the matter in a wrong way, but if not do you know how to pass to the query the ID?
modify_brand.php
<form role="form" action='../php/update_brand.php' method='post'>
<label>BRANDS LIST</label>
<select name='brands-list'>
<?php
while ($listabrand=mysqli_fetch_array($brands)){
echo '<option value="'.$listabrand['1'].'">'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>
<label>BRAND'S NAME TO MODIFY</label>
<input type="text" name='brand-name'>
<button type="submit" name='modify-btn' class="btn btn-default">Modifica</button>
</form>
.....
<script>
$('select[name="brands-list"]').change(function(){
var selectedBrand = $(this).val();
$('input[name="brand-name"]').val(selectedBrand);
});
</script>
update_brand.php
<?php
require '../sys/conn.php';
$mod_brand=$_POST['brand-name']
if (isset($_POST['modify-btn'])){
$brand=mysqli_real_escape_string($conn, $mod_brand] );
if ($mod_brand !=''){
$update = mysqli_query($conn,"
UPDATE mg_terms SET name= $brand
WHERE term_id=......... // THIS IS WHERE THE ID OF SELECTED BRAND SHOULD BE PLACED
");
header('Location: ../pages/success.html');}
else{header('Location: ../pages/error.html');}}
mysqli_close($conn);
?>
Hope I was enoughly clear.
So what you want to do is to set the select options value to the id, So when you submit your form, the selected id is submitted to the php. But you're gonna have to change your Javascript a bit. It should set the input's value to the options text, not the options value:
<form role="form" action='../php/update_brand.php' method='post'>
<label>BRANDS LIST</label>
<select name='brands-list'>
<?php
while ($listabrand=mysqli_fetch_array($brands)){
echo '<option value="'.$listabrand['0'].'">'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>
<label>BRAND'S NAME TO MODIFY</label>
<input type="text" name='brand-name'>
<button type="submit" name='modify-btn' class="btn btn-default">Modifica</button>
</form>
.....
<script>
$('select[name="brands-list"]').change(function(){
$('input[name="brand-name"]').val($('select[name="brands-list"] option:selected').text().split(' - ')[1]);
});
</script>
And on the php side:
<?php
require '../sys/conn.php';
$mod_brand=$_POST['brand-name'];
$mod_id=$_POST['brands-list'];
if (isset($_POST['modify-btn'])){
$brand=mysqli_real_escape_string($conn, $mod_brand );
$brand_id=intval($mod_id );
if ($mod_brand !=''){
$update = mysqli_query($conn,"
UPDATE mg_terms SET name= '$brand'
WHERE term_id=$brand_id
");
header('Location: ../pages/success.html');}
else{header('Location: ../pages/error.html');}}
mysqli_close($conn);
?>