Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
When <option>"anyTableName"</option> is selected -> show column names of this table (PHP,MySQL=

i am new to stackoverflow, so correct me if i am talking nonsense ;)

My Problem: I´ve got a text.php where 2 are. In the first i select a table (like "accounts", "customers", and so on). The second shows the column names of the selected table. The page should not refresh when the first changes! What i´ve done so far: I managed to get a connection to the MySQL Database (xampp if this is important idk). The tables show up in the first . So howcan i achieve that the column names show up in the 2nd ? I heard of AJAX, JQuery but i dont know anything about it.

'index.php'
<form>
    <div>
    <?php
        //connection ($link is included in the <head>)
        if (!$link) {
            die("Connection failed: " . mysqli_connect_error());
            }
        //SQL-Statement and Query
        $sql1 = "SELECT * FROM tables";
        $result = mysqli_query($link, $sql1);
        ?>
        <select name="tableselect" id="tableselect" size="10">
            <?php
            if (mysqli_num_rows($result) > 0) {
                while($row = mysqli_fetch_assoc($result)) {
            ?>
            <option><?php echo $row['name']; ?></option>
                <?php }
            }
            ?>
        </select>

//second <select>
        <?PHP

            $sql2 = "SELECT COLUMN_NAME 
            FROM INFORMATION_SCHEMA.COLUMNS 
            WHERE 
                TABLE_SCHEMA = Database()
                AND TABLE_NAME = '$variable' ;";
            $result = mysqli_query($link, $sql2);
            echo $sql2
        ?>
        <select name="fieldnames" id="fieldnames" size="10">
            <option>*</option>
            <?php
                while($row=mysqli_fetch_array($resultf)) {
                ?>
                <option><?php echo $row['COLUMN_NAME']; ?></option>
                <?php }
            ?>
        </select>
        </div>
    </form>

I thought that $variable means that the name of the selected table (from 1st ) must be placed in this variable.

Any help would be nice :) If anything should be styled better let me know :D

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Yes, you achieve this easily by using JQuery Ajax or VueJS/Axios. Check out this tutorial Dynamic Dependent Select Box using jQuery, Ajax and PHP

This is also possible with alpinejs fetch if you don't want to use heavy frameworks.

Check out the answer here. AlpineJS for dynamic select menu

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!