Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

303
Visualizações
How to update the value of textbox based from the value of dropbox? - MySQL PHP

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.

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda