0

I am in need of a way to populate a drop down list (I believe it is called a Select) with a SQL Server query.

I want to populate the list with the results of Select storeName from storeinfo which would return roughly 30 results and I would want these results to be displayed in the Select at page load.

How is this achieved through php? Or since I Need this done on page load would I have to use some sort of JQuery/Ajax request to accomplish?


I see this can populate my select list, but how to do it on page load?

<select name="store">
<?php 
$sql = mysqli_query($connection, "SELECT storeName FROM stores");
while ($row = $sql->fetch_assoc()){
echo "<option value=\"store1\">" . $row['storeName'] . "</option>";
}
?>
</select>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Smith Stanley
  • 461
  • 1
  • 8
  • 25
  • 2
    This is PHP/MySQL 101 for which there exists literally hundreds of tutorials. – Jay Blanchard May 12 '17 at 17:52
  • 1
    You are expected to try to **write the code yourself**. After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard May 12 '17 at 17:52
  • Sorry but have to say this: the qs is sexist. That said, why do you need it on page load? – inarilo May 12 '17 at 17:52
  • @JayBlanchard - can you direct me to one of these tuts? I have searched and not found anything like what I am after. – Smith Stanley May 12 '17 at 18:10
  • 1
    Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – Jay Blanchard May 12 '17 at 18:12
  • @inarilo - no mean to sound sexist. I ask for forgiveness and grace on that. – Smith Stanley May 12 '17 at 18:12
  • The code you show will populate the select list on page load. – Jay Blanchard May 12 '17 at 18:16
  • accepted ;) if you haven't found anything then perhaps you are not seraching for the correct keywords. what you need to do is loop through the result set and output each option inside it. – inarilo May 12 '17 at 18:16
  • Why do you need it on page load? – inarilo May 12 '17 at 18:18
  • @inarilo - I need it on page load as I am going to use the selection from this Select to pass to a second query that will populate a html table using AJAX on button press – Smith Stanley May 12 '17 at 19:05
  • You can just use your current code, and with JS attach an onchange event handler to the select element, or an onclick handler to your button, which will then run the AJAX code using the selected value. https://api.jquery.com/change/ – inarilo May 12 '17 at 19:18
  • @inarilo - I have been using the button press and using `if(isset($_POST['submit']))` on the button press to pass the data and populate a table. (This is for other fields not the one above in question) - would it be faster/less resource intensive to accomplish such using JS on change? – Smith Stanley May 12 '17 at 19:24
  • Using AJAX would mean less data needs to be sent back (just the new data instead of the whole page) but if the page is small or most things change on the page, you can use either method. Of course, user experience is also a factor. – inarilo May 12 '17 at 19:36

1 Answers1

0

To get the value of the store name you have to use that in your output too:

<select name="store">
<?php 
    $sql = mysqli_query($connection, "SELECT storeName FROM stores");
    while ($row = $sql->fetch_assoc()){
        echo "<option value=\"{$row['storeName']}\">{$row['storeName']}</option>";
    }
?>
</select>

PHP will process this and your drop-down will be available on page load.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119