-2

I am trying to populate a Bootstrap form with content from a database. Basically the user flow is, a user selects a record from a dropdown that has been filled with information from a database. Whenever they select an option from that dropdown it fills the form with the relevant data. Here is some sample code:

<?php
      mysql_connect('server', 'name', 'password');
      mysql_select_db('database');
      $sql = "SELECT * FROM table";
      $result = mysql_query($sql);
      echo "<select name='select' class='form-control'";
      while ($row = mysql_fetch_array($result)) {
            echo "<option value='" . $row['details_id'] . "'>" . $row['name'] . "</option>";
      }
      echo "</select>";
      ?>
      </div>
       <script>

       </script>
       </div>
            <legend>Edit details and submit request</legend>
            <!-- Text input-->
            <div class="form-group">
                 <label class="col-md-4 control-label" for="edit">Name:</label>
                 <div class="col-md-8">
                      <input id="edit" name="edit" type="text" class="form-control input-md">
                 </div>
            </div>

The dropdown is being filled properly, I just don't know how to fill the form with the data now!

Euan Cantley
  • 21
  • 2
  • 8
  • "This is rather URGENT can someone please help!" - tbh, we (I) don't care if you have 2h or 2 years to solve your problem. We will help you as fast as we can provide an appropriate solution. So please just focus on posing a question, leave the rest to the community. – Philipp Jul 28 '16 at 13:05
  • Possible duplicate of [Want to fetch data from database based on dropdown list selection using php](http://stackoverflow.com/questions/38614551/want-to-fetch-data-from-database-based-on-dropdown-list-selection-using-php) – Philipp Jul 28 '16 at 13:06
  • Alrighty pal I wasn't to know! Just been a bit desperate wanted to get some responses for myself don't exactly think a valid question is worth some downvotes! Just cause someone else can easily solve this doesn't mean it's a bad thing I can't – Euan Cantley Jul 28 '16 at 13:11
  • Agreed, I think the question itself is legit, I didn't downvote. My guess would be, you got the downvotes because of the "urgent"-stuff, or because your indentation was bad before your question got edited. – Philipp Jul 28 '16 at 13:17
  • Not sure if you know that writing something like URGENT in uppercase is more of a demand to attention. We can't really make demands on volunteers knowledge. I suggets that bit is removed or expressed in a more etiquette friendly manner. – Andrew Truckle Jul 28 '16 at 13:41

1 Answers1

1

Use AJAX. I wrote about it in a previous post. Add an event listener to the select box, so when an item is picked, you can make an AJAX request to the PHP site to get the data you need. json_encode the data (make it into an array first), and then with jQuery, you can put the data into the form.

Bringing data from html to php and posting it onto database

Community
  • 1
  • 1
Janno
  • 542
  • 4
  • 15
  • 1
    You can also check this answer for a solution without jQuery http://stackoverflow.com/a/38614744/3595565 it is pretty much the same scenario – Philipp Jul 28 '16 at 13:02