# PHP code to retrieve column data with dropdown menu

**URL:** <https://www.phphelp.com/t/php-code-to-retrieve-column-data-with-dropdown-menu/26407>\
**Category:** General PHP Help\
**Created:** [April 30, 2018, 5:49am UTC](https://www.phphelp.com/t/php-code-to-retrieve-column-data-with-dropdown-menu/26407 "2018-04-30T05:49:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mirc](https://www.phphelp.com/user_avatar/www.phphelp.com/mirc/32/1263_2.png) [@Mirc](https://www.phphelp.com/u/Mirc)\
**Post date:** [April 30, 2018, 5:49am UTC](https://www.phphelp.com/t/php-code-to-retrieve-column-data-with-dropdown-menu/26407/1 "2018-04-30T05:49:38Z")

</div>

I am seeking a PHP code to fetch and retrieve data from a single column from a mysql table and display in a tabular form on the page. So I need a dropdown option list for the column names so every time a different column is selected, the relevant data is displayed on the page. Can someone provide me a code snippet or help me out with this, please? thanks.

I inserted an example image to show how it’d look. But it my case, i just need to display one column instead of multiple columns.

* * *

 ![phpdropdown.gif](https://www.phphelp.com/uploads/default/original/2X/d/dbc9f51f733a547630dae608e741e85f9ad0ba4c.gif)

---

<div class="post-metadata">

**Author:** ![Cluster\_One](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@Cluster\_One](https://www.phphelp.com/u/Cluster_One)\
**Post date:** [May 1, 2018, 7:03pm UTC](https://www.phphelp.com/t/php-code-to-retrieve-column-data-with-dropdown-menu/26407/2 "2018-05-01T19:03:54Z")

</div>

Hi,

I would query the database table for column names - it should be fairy simple,(google ).  
Then use the column names as values in ,select, tag.

I am not sure if you can get what you want just by selecting a column name from dropdown option list, perhaps a submit button need adding.

Upon clicking on ,submit, the code then should grab $\_POST for that dropdown list (select tag), feed the value from $\_POST to query which should be built to retrieve data only for that column.

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [May 1, 2018, 7:27pm UTC](https://www.phphelp.com/t/php-code-to-retrieve-column-data-with-dropdown-menu/26407/3 "2018-05-01T19:27:43Z")

</div>

> [@Cluster\_One](#):
>
> I am not sure if you can get what you want just by selecting a column name from dropdown option list, perhaps a submit button need adding.

Need an AJAX call to a page that will process and return the new options, using the onchange event.

---

<div class="post-metadata">

**Author:** ![Mirc](https://www.phphelp.com/user_avatar/www.phphelp.com/mirc/32/1263_2.png) [@Mirc](https://www.phphelp.com/u/Mirc)\
**Post date:** [May 2, 2018, 8:08am UTC](https://www.phphelp.com/t/php-code-to-retrieve-column-data-with-dropdown-menu/26407/4 "2018-05-02T08:08:40Z")

</div>

thanks, guys. I tried your suggestions already actually. but I couldn’t make it work, so trashed it. So I was hoping to get a code snippet here. I googled a lot but couldn’t find something like that. so decided to ask experts. thanks for your suggestions.

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [May 2, 2018, 12:06pm UTC](https://www.phphelp.com/t/php-code-to-retrieve-column-data-with-dropdown-menu/26407/5 "2018-05-02T12:06:00Z")

</div>

So, what did you try?

---

<div class="post-metadata">

**Author:** ![Mirc](https://www.phphelp.com/user_avatar/www.phphelp.com/mirc/32/1263_2.png) [@Mirc](https://www.phphelp.com/u/Mirc)\
**Post date:** [May 2, 2018, 1:10pm UTC](https://www.phphelp.com/t/php-code-to-retrieve-column-data-with-dropdown-menu/26407/6 "2018-05-02T13:10:08Z")

</div>

Basically I tried to modify this code here [http://www.itgeared.com/articles/1403-ajax-php-sql-database-data-tutorial/](http://www.itgeared.com/articles/1403-ajax-php-sql-database-data-tutorial/) but I couldn’t achieve it. It is no exactly what I want, but structurally similar to it.

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [May 2, 2018, 1:49pm UTC](https://www.phphelp.com/t/php-code-to-retrieve-column-data-with-dropdown-menu/26407/7 "2018-05-02T13:49:12Z")

</div>

That is bad and outdated code anyway.

I would recommend creating an API, but that is me. What you basically need is two files.

1. The actual file the actual form uses to display
2. A background file that you can for updates.

[http://test.andrewstonecipher.info/](http://test.andrewstonecipher.info/)

---

<div class="post-metadata">

**Author:** ![Mirc](https://www.phphelp.com/user_avatar/www.phphelp.com/mirc/32/1263_2.png) [@Mirc](https://www.phphelp.com/u/Mirc)\
**Post date:** [May 2, 2018, 2:10pm UTC](https://www.phphelp.com/t/php-code-to-retrieve-column-data-with-dropdown-menu/26407/8 "2018-05-02T14:10:16Z")

</div>

yes, the code is outdated, but it works. Your example is similar to it.
