2 Replies - 3961 Views - Last Post: 22 March 2011 - 02:24 PM Rate Topic: -----

#1 Jersey928   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 22-March 11

Pre-selected Value in dropdown Menu with data from Database

Posted 22 March 2011 - 12:44 PM

i am new to PHP, but i have experience in other coding.

I have an update form that retrieves the data from a data base and i am trying to get the default selected value to be the value from the database.

here is the code that i have
print '  <form action="'."$_SERVER[PHP_SELF]".'" method="post">
	<table border="1" cellpadding="4" cellspacing="0">
	
	// prepopulate form with values from db
	<tr><td>Book ISBN# </td><td>'."$book[ID]".'</td></tr>
	<tr><td>Title</td><td><input type="text" name="Title" value="'."$book[Title]".'"></td></tr>
	<tr><td>Author </td>
		 <td> ';
		$query="SELECT AuthorID,FirstName,LastName FROM Author";

		/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

		$result = mysql_query ($query);
		echo "<select name=Author value=''>Author Name</option>";
		// printing the list box select command

		while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
		echo "<option value=$nt[AuthorID]>$nt[FirstName]$nt[LastName]</option>";
		/* Option values are added by looping through the array */
		}
		echo "</select>";// Closing of list box 
		


Is This A Good Question/Topic? 0
  • +

Replies To: Pre-selected Value in dropdown Menu with data from Database

#2 init.d.httpd   User is offline

  • D.I.C Head
  • member icon

Reputation: 31
  • View blog
  • Posts: 132
  • Joined: 02-February 11

Re: Pre-selected Value in dropdown Menu with data from Database

Posted 22 March 2011 - 12:57 PM

What is it doing or not doing right?

also

echo "<select name=Author value=''>Author Name</option>";

echo "<option value=$nt[AuthorID]>$nt[FirstName]$nt[LastName</option>";



should be

echo '<select name="Author" value="">Author Name</option>';

echo "<option value=\"$nt[AuthorID\">$nt[FirstName]$nt[LastName]</option>";


This post has been edited by init.d.httpd: 22 March 2011 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#3 Jersey928   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 22-March 11

Re: Pre-selected Value in dropdown Menu with data from Database

Posted 22 March 2011 - 02:24 PM

i am trying to get it so that when it pulls the recored to be updated from the database it makes the selected value in the drop down menu the value in the recored
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1