I have a dropbox and a textbox. The dropbox is populated by the value from MySQL database. What I want to do is to make the value of the textbox change dynamically based from what I select from the dropbox.
Right now, my current code is partly working. The dropbox is populated by the value from the database. But the textbox doesn't change value, it's only showing the value of the first row.
These values are in the same table.
id | person | age | by_user | up_date
-----------------------------------------------
AI | john | 10 | "byUser" | "date updated"
AI | jane | 12 | "byUser" | "date updated"
AI | tess | 15 | "byUser" | "date updated"
This is the code in the page where I have the submit button:
<?php
session_start();
include 'headers/header_on.php'; //LOGIN
include "dbh.php"; //MySQL CONN
$person = $_POST['person'];
$age = $_POST['age'];
$sql = "SELECT * FROM table ORDER BY name";
?>
<p id="opener">
<?php
if (isset($_SESSION['Fname'])) {
echo "Welcome back, " . $_SESSION['Fname'];
} else {
header("Location: ../register.php?error");
exit();
}
?>
</p>
<form class='data_form' action='data/update.inc.php' method='post'>
<?php
//DROPDOWN BOX
echo "<select name=person id=person>";
//PULLS DATA FROM DATABASE
foreach (mysqli_query($conn, $sql) as $row){
echo "<option value=$row[person]>$row[person]</option>";
}
echo "</select>";
?>
<?php
//TEXTBOX
echo "<input type=text name=age value=$row[age]>";
?>
<button type='submit'>UPDATE</button>
</form>
This is the code for the other php file: (update.inc.php)
include '../dbh.php'; //CONN TO MySQL
$by_user = $_SESSION['Fname'];
//GET CURRENT DATE YYY-MM-DD
$up_date = date('Y-m-d');
{
//INSERT VALUES TO MySQL DATABASE --> DOESN'T WORK
$sql = "UPDATE table SET age=$age, by_user=$by_user, up_date=$up_date";
$result = mysqli_query($conn, $sql);
header("Location: ../index.php");
}
?>
Basically, what I'm trying to achieve is to update the age of a person (in the textbox) based from the person selected in the dropbox when I click the UPDATE button. But I can't get pass to the textbox issue where the value won't update based from the value of the dropbox.
Hope anyone can help me with this. Thanks!
Just a heads up, I'm not familiar with AJAX and JQuery. I'm still learning how to program.