| | |
Populating Multiple Text Fields Based On A Dynamic Drop-Down List Selection
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
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:
Any help will be greatly appreciated. Been searching the web endlessly for a solution.
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)
<form name="events" method="post" action="<?= $_SERVER['PHP_SELF']?>"> <table> <tr> <td> </td> <td> <?php // open connection to MySQL server $connection = mysql_connect('localhost', 'username', 'password') or die ('Unable to connect!'); //select database mysql_select_db('test') or die ('Unable to select database!'); //create and execute query $query = 'SELECT DISTINCT heading FROM events ORDER BY heading'; $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error()); //create selection list echo "<select name='Events'>\n"; while($row = mysql_fetch_row($result)) { $heading = $row[0]; echo "<option value='$heading'>$heading\n"; } echo "</select>" ?> </td> </tr> <tr> <td align="right" valign="top"><p>Heading:</p></td> <td> <input type="text" name="eventHeading" id="eventHeading" class="heading"> </td> </tr> <tr> <td align="right" valign="top"><p>Date:</p></td> <td> <input type="text" name="eventDate" id="eventDate" class="date"> </td> </tr> <tr> <td align="right" valign="top"><p>Details:</p></td> <td> <textarea name="eventDetails" id="eventDetails" class="details"></textarea> </td> </tr> <tr> <td> </td> <td> <input name="submit" type="submit" class="submitForm" value="Submit"> </td> </tr> </table> </form>
Any help will be greatly appreciated. Been searching the web endlessly for a solution.
•
•
•
•
why does the information need to be text fields?
are going to submit it again or something
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)
if ($_REQUEST['edit']){ // open connection to MySQL server $connection = mysql_connect('localhost', 'username', 'password') or die ('Unable to connect!'); //select database mysql_select_db('test') or die ('Unable to select database!'); //define variables $event = $_POST['Events']; //create and execute query $query = "SELECT heading, date, description FROM events WHERE heading = '$event'"; $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error()); while($row = mysql_fetch_row($result)) { $heading = $row[0]; $date = $row[1]; $description = $row[2]; } // close connection mysql_close($connection); }
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.
•
•
Join Date: Oct 2007
Posts: 1
Reputation:
Solved Threads: 1
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.
Pass this array to a javascript snippet
Create javascript function selectsow(selobj) similar to this one:
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.
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)
Eventsarray[$id] = array("venue => $venue, "date" =>$date);
Pass this array to a javascript snippet
Create javascript function selectsow(selobj) similar to this one:
PHP Syntax (Toggle Plain Text)
function selectshow(selObj) { global Eventsarray(); var id = selObj.options[selObj.selectedIndex].value; document.getElementByID('venue').value = Eventsarray[id]['venue']; document.getElementByID('date').value = Eventsarray[id]['date']; } Print the select box, attach an event: <select id ="showtype" onchange ="selectshow(this)"> <option value = "flowershow">flowershow</option> ... </select> <input type ="text" name = "venue" id="venue" value = ""> <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.
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
Thanks a bunch
Last edited by Venom Rush; Oct 15th, 2007 at 12:24 pm. Reason: removing quote
Re: Populating Multiple Text Fields Based On A Dynamic Drop-Down List Selection
0
#10 Feb 14th, 2008
Hi, I was also in same problem, if anyone still looking for solution please mail me on rkarim1981@gmail.com
![]() |
Similar Threads
- php mysql drop down list (PHP)
- Populate One Drop Down List From Another (ASP.NET)
- Populating a Drop-down List (PHP)
- Dynamic dropdown list (ASP.NET)
- Data Binding to a Drop Down List? (ASP.NET)
- Blank drop list (PHP)
Other Threads in the PHP Forum
- Previous Thread: Send login information
- Next Thread: problem of rhyme game
| Thread Tools | Search this Thread |
5.2.10 action apache api array beginner beneath binary broken cakephp checkbox class classes cms code cron curl database date destroy display dynamic echo echo$_get[x]changingitintovariable... email encode error fcc file files folder form forms function functions google header howtowriteathesis href htaccess html image images include insert ip javascript joomla limit link local login mail memberships menu mlm mod_rewrite multiple multipletables mysql mysqlquery neutrality oop open passwords paypal pdf php provider query radio random record remote rss script search server sessions sockets source space sql strip_tags syntax system table template thesishelp tutorial update upload url validator variable video voteup web window.onbeforeunload=closeme; youtube






