I'm beginning in PHP and I've been tasked to create a form with which the user can select a category (a row from a table in the MySQL Database) and update the content of a specific column in that row.
Here is what it looks like. Here is what the table in MySQL looks like (tva is the column in which I want to update the content)
Here is what the code looks like :
<tr>
<td>Modify the TVA of : </td>
<td>
<select name="tva_modif">
<?
$sql_tva_modif = "SELECT id_type, nom_fr,tva
FROM type
ORDER BY nom_fr
;";
$sql_tva_modif_result = mysql_query($sql_tva_modif);
while($val_tva_modif = mysql_fetch_array($sql_tva_modif_result))
{
echo "<option value='" . $val_tva_modif["id_type"] . "'>" . $val_tva_modif["nom_fr"] . "</option>";
}
?>
</select>
<input name="tva_val" id="tva_val" type="text" value=""></div>
</td>
</tr>
If possible, can someone explain me how to make sure that the input type text field display the TVA value of the selected option and how to make that field update the content of that column in my database ?
</div>
You are using id_categorie field which is not mention in select query
<?php
$sql_tva_modif = "SELECT id_type, nom_fr,tva
FROM type
ORDER BY nom_fr
;";
$sql_tva_modif_result = mysql_query($sql_tva_modif);
?>
<tr>
<td>Modify the TVA of : </td>
<td>
<select name="tva_modif" id="tva_modif">
<?php
while($val_tva_modif = mysql_fetch_array($sql_tva_modif_result))
{
echo "<option value='" . $val_tva_modif["id_type"] . "' id='" . $val_tva_modif["tva"] . "' >" . $val_tva_modif["nom_fr"] . "</option>";
}
?>
</select>
<input name="tva_val" id="tva_val" type="text" value=""></div>
</td>
</tr>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$('#tva_modif').change(function()
{
var id = $(this).children(":selected").attr("id");
if(id != ""){
$('#tva_val').val(id);
}
else
{
$('#tva_val').val('');
}
});
</script>
Please Try this
<tr>
<td>Modify the TVA of : </td>
<td>
<select name="tva_modif" id="ss1">
<?
$sql_tva_modif = "SELECT id_type, nom_fr,tva
FROM type
ORDER BY nom_fr
;";
$sql_tva_modif_result = mysql_query($sql_tva_modif);
while($val_tva_modif = mysql_fetch_array($sql_tva_modif_result))
{
echo "<option value='" . $val_tva_modif["id_categorie"] . "'>" . $val_tva_modif["nom_fr"] . "</option>";
}
?>
</select>
<input name="tva_val" id="tva_val" type="text" value=""></div>
</td>
</tr>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$('#ss1').change(function(){
var selected_item = $(this).val()
if(selected_item ){
$('#tva_val').val(selected_item).addClass('hidden');
// $('#tva_val').val("").removeClass('hidden');
}else{
$('#tva_val').val("").removeClass('hidden');
}
});
</script>
Please check this link JSDEMO
To update the content of your input you have to add a bit of Javascript (I advise you to add an ID to your <select>
such as "vat_select"),
<script>
document.getElementById("vat_select").onchange = function(){
document.getElementById("tva_val").value = this.options[this.selectedIndex].value;
}
</script>
Be careful, I believe you have an error in your SELECT, you need to select the "id_categorie"
too, otherwise your option values will be empty.
To update data in your table (whichever it is) I advise you to take a look at this. You should modify your instructions starting with "mysql" by "mysqli" (while having mysqli activated in PHP), because they are deprecated.
PS : VAT is the English for TVA.
I think this is what you need:
<?php
if(isset($_POST["submit_tva"])){
$id_type_and_tva_array = explode("_", $_POST["tva_modif"]);
$sql_query = "UPDATE table_name SET tva='".$_POST["tva_val"]."' WHERE id_type='".$id_type_and_tva_array[0]."'";
mysql_query($sql_query);
}
?>
<form method="post">
<table>
<tr>
<td>Modify the TVA of : </td>
<td>
<select name="tva_modif" id="tva_modif">
<?
$sql_tva_modif = "SELECT id_type, nom_fr,tva
FROM type
ORDER BY nom_fr
;";
$sql_tva_modif_result = mysql_query($sql_tva_modif);
while($val_tva_modif = mysql_fetch_array($sql_tva_modif_result))
{
echo "<option value='" . $val_tva_modif["id_type"] . "_".$val_tva_modif["tva"]."'>" . $val_tva_modif["nom_fr"] . "</option>";
}
$val_tva_modif = mysql_fetch_array($sql_tva_modif_result)
?>
</select>
<input name="tva_val" id="tva_val" type="text" value="<?php echo $val_tva_modif["tva"]; ?>"></div>
</td>
<td><input type="submit" name="submit_tva" type="text" value="Update"></td>
</tr>
</table>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$('#tva_modif').change(function()
{
var id_type_and_tva = $("tva_modif").val();
var id_type_and_tva_array = id_type_and_tva.split("_");
var tva = id_type_and_tva_array[1];
$("#tva_val").val(tva);
});
</script>