943,986 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 13491
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 15th, 2007
0

Populating Multiple Text Fields Based On A Dynamic Drop-Down List Selection

Expand Post »
Hi there

I'm new to php and MySQL and I'm busy trying to build a page that when the administrator selects an item from a drop-down list, that has been populated from a database, it populates multiple text fields with the appropriate information from the database. For example, if the administrator selects "Flower Show" from the selection box then the text fields Heading, Date and Details get populated with "Flower Show", date of the flower show, and a description of the flower show.

Here is what I have so far:

PHP Syntax (Toggle Plain Text)
  1. <form name="events" method="post" action="<?= $_SERVER['PHP_SELF']?>">
  2. <table>
  3. <tr>
  4. <td>&nbsp;</td>
  5. <td>
  6. <?php
  7. // open connection to MySQL server
  8. $connection = mysql_connect('localhost', 'username', 'password')
  9. or die ('Unable to connect!');
  10.  
  11. //select database
  12. mysql_select_db('test') or die ('Unable to select database!');
  13.  
  14. //create and execute query
  15. $query = 'SELECT DISTINCT heading FROM events ORDER BY heading';
  16. $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
  17.  
  18. //create selection list
  19. echo "<select name='Events'>\n";
  20.  
  21. while($row = mysql_fetch_row($result))
  22. {
  23. $heading = $row[0];
  24. echo "<option value='$heading'>$heading\n";
  25. }
  26. echo "</select>"
  27. ?>
  28. </td>
  29. </tr>
  30. <tr>
  31. <td align="right" valign="top"><p>Heading:</p></td>
  32. <td>
  33. <input type="text" name="eventHeading" id="eventHeading" class="heading">
  34. </td>
  35. </tr>
  36. <tr>
  37. <td align="right" valign="top"><p>Date:</p></td>
  38. <td>
  39. <input type="text" name="eventDate" id="eventDate" class="date">
  40. </td>
  41. </tr>
  42. <tr>
  43. <td align="right" valign="top"><p>Details:</p></td>
  44. <td>
  45. <textarea name="eventDetails" id="eventDetails" class="details"></textarea>
  46. </td>
  47. </tr>
  48. <tr>
  49. <td>&nbsp;</td>
  50. <td>
  51. <input name="submit" type="submit" class="submitForm" value="Submit">
  52. </td>
  53. </tr>
  54. </table>
  55. </form>

Any help will be greatly appreciated. Been searching the web endlessly for a solution.
Similar Threads
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Oct 15th, 2007
0

Re: Populating Multiple Text Fields Based On A Dynamic Drop-Down List Selection

does it matter if the page reloads or not?
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Oct 15th, 2007
0

Re: Populating Multiple Text Fields Based On A Dynamic Drop-Down List Selection

Click to Expand / Collapse  Quote originally posted by kkeith29 ...
does it matter if the page reloads or not?
No it doesn't. But it would be nice if it didn't have to. I know this involves javascript though.

But shoot away...any solution is a good solution at this point.
Last edited by Venom Rush; Oct 15th, 2007 at 11:29 am.
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Oct 15th, 2007
0

Re: Populating Multiple Text Fields Based On A Dynamic Drop-Down List Selection

why does the information need to be text fields?
are going to submit it again or something
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Oct 15th, 2007
0

Re: Populating Multiple Text Fields Based On A Dynamic Drop-Down List Selection

Click to Expand / Collapse  Quote originally posted by kkeith29 ...
why does the information need to be text fields?
are going to submit it again or something
Yes. The form is basically an edit form.

I've been working on something while waiting for a response to this thread and managed to get something working. Might not be the cleanest way in a php purest's eyes but it works.

What I did was add a submit button next to the selection box called "Edit".
Once the administrator selects an event they click the edit button which then calls the following php code:

PHP Syntax (Toggle Plain Text)
  1. if ($_REQUEST['edit']){
  2.  
  3. // open connection to MySQL server
  4. $connection = mysql_connect('localhost', 'username', 'password')
  5. or die ('Unable to connect!');
  6.  
  7. //select database
  8. mysql_select_db('test') or die ('Unable to select database!');
  9.  
  10. //define variables
  11. $event = $_POST['Events'];
  12.  
  13. //create and execute query
  14. $query = "SELECT heading, date, description FROM events WHERE heading = '$event'";
  15. $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
  16.  
  17. while($row = mysql_fetch_row($result))
  18. {
  19. $heading = $row[0];
  20. $date = $row[1];
  21. $description = $row[2];
  22. }
  23.  
  24. // close connection
  25. mysql_close($connection);
  26. }

Please post your solution though as I don't really think mine is the best route to go.
Last edited by Venom Rush; Oct 15th, 2007 at 11:43 am.
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Oct 15th, 2007
0

Re: Populating Multiple Text Fields Based On A Dynamic Drop-Down List Selection

Hi,
I am no expert, but would you consider the following approach?


1) Drop down box with all the Shows (flower show, dance show, etc)

2) Two more text fields with data (venue, date)

3) Upon selecting an option in the event select, the two text fields must be populated.

The logic:

Get all show types in an array.
PHP Syntax (Toggle Plain Text)
  1. Eventsarray[$id] = array("venue => $venue, "date" =>$date);
  2.  


Pass this array to a javascript snippet


Create javascript function selectsow(selobj) similar to this one:

PHP Syntax (Toggle Plain Text)
  1. function selectshow(selObj) {
  2. global Eventsarray();
  3. var id = selObj.options[selObj.selectedIndex].value;
  4.  
  5. document.getElementByID('venue').value = Eventsarray[id]['venue'];
  6. document.getElementByID('date').value = Eventsarray[id]['date'];
  7.  
  8. }
  9.  
  10. Print the select box, attach an event:
  11. <select id ="showtype" onchange ="selectshow(this)">
  12. <option value = "flowershow">flowershow</option>
  13. ...
  14.  
  15. </select>
  16.  
  17. <input type ="text" name = "venue" id="venue" value = "">
  18. <input type ="text" name = "date" id="date" value = "">

Using such approach will let you populate the fields without reloading the page.

Reloading a page, on the other hand lets you track which events your visitors are interested in, and adds to your Alexa ranking, but that's another story.

Hope this helps.
Last edited by bgseo; Oct 15th, 2007 at 12:03 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
bgseo is offline Offline
1 posts
since Oct 2007
Oct 15th, 2007
0

Re: Populating Multiple Text Fields Based On A Dynamic Drop-Down List Selection

Thanks bgseo for your response. I'll try that out. The page I'm creating is purely for editing upcoming events for a small holiday lodge. Only admin will have access to it so I'm not worried about Alexa ranking.

Thanks a bunch
Last edited by Venom Rush; Oct 15th, 2007 at 12:24 pm. Reason: removing quote
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Oct 15th, 2007
0

Re: Populating Multiple Text Fields Based On A Dynamic Drop-Down List Selection

do you still need my code or did the other one work out for you.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Oct 16th, 2007
0

Re: Populating Multiple Text Fields Based On A Dynamic Drop-Down List Selection

Click to Expand / Collapse  Quote originally posted by kkeith29 ...
do you still need my code or did the other one work out for you.
I'd like to see it but I don't need it. I also think it might help any others out there that have the same question as me....so spit it out
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Feb 14th, 2008
0

Re: Populating Multiple Text Fields Based On A Dynamic Drop-Down List Selection

Hi, I was also in same problem, if anyone still looking for solution please mail me on rkarim1981@gmail.com
Reputation Points: 10
Solved Threads: 1
Newbie Poster
rkarim1981 is offline Offline
4 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Send login information
Next Thread in PHP Forum Timeline: problem of rhyme game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC