User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,543 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,305 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 6447 | Replies: 10 | Solved
Reply
Join Date: Oct 2007
Posts: 189
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

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

  #1  
Oct 15th, 2007
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:

<form name="events" method="post"  action="<?= $_SERVER['PHP_SELF']?>">
<table>
  <tr>
    <td>&nbsp;</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>&nbsp;</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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 643
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 72
kkeith29's Avatar
kkeith29 kkeith29 is online now Online
Practically a Master Poster

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

  #2  
Oct 15th, 2007
does it matter if the page reloads or not?
Reply With Quote  
Join Date: Oct 2007
Posts: 189
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

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

  #3  
Oct 15th, 2007
Originally Posted by kkeith29 View Post
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.
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 643
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 72
kkeith29's Avatar
kkeith29 kkeith29 is online now Online
Practically a Master Poster

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

  #4  
Oct 15th, 2007
why does the information need to be text fields?
are going to submit it again or something
Reply With Quote  
Join Date: Oct 2007
Posts: 189
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

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

  #5  
Oct 15th, 2007
Originally Posted by kkeith29 View Post
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:

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.
Reply With Quote  
Join Date: Oct 2007
Posts: 1
Reputation: bgseo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
bgseo bgseo is offline Offline
Newbie Poster

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

  #6  
Oct 15th, 2007
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.
 Eventsarray[$id] = array("venue => $venue, "date" =>$date);  


Pass this array to a javascript snippet


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

 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.
Reply With Quote  
Join Date: Oct 2007
Posts: 189
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

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

  #7  
Oct 15th, 2007
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
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 643
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 72
kkeith29's Avatar
kkeith29 kkeith29 is online now Online
Practically a Master Poster

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

  #8  
Oct 15th, 2007
do you still need my code or did the other one work out for you.
Reply With Quote  
Join Date: Oct 2007
Posts: 189
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

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

  #9  
Oct 16th, 2007
Originally Posted by kkeith29 View Post
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
Reply With Quote  
Join Date: Feb 2008
Posts: 2
Reputation: rkarim1981 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rkarim1981 rkarim1981 is offline Offline
Newbie Poster

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

  #10  
Feb 14th, 2008
Hi, I was also in same problem, if anyone still looking for solution please mail me on rkarim1981@gmail.com
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 4:59 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC