Jump to content

Using php to autopopulate a drop down menu


prcollin

Recommended Posts

Ok i have a drop down list that I want to update everytime a new client is added to my database so that when i want to view a client i can just clik on the drop down and choose the client.

 

Question is how do i code a drop down to autopopulate with the first and last name of the clients that are in the database in real time.

 

$client_fname

$client_lname are the column headings.  I want it to be last name, first name int he drop down menu.

 

Thanks ahead of time

 

Link to comment
Share on other sites

$member_list = "";
while($row = mysql_fetch_array( $result )) {
	if($row['name'] == $_POST['name']){
	$member_list .= "<option selected=\"selected\" value=\"".$row['name']."\">".$row['name']."</option>";
	}else{
	$member_list .= "<option value=\"".$row['name']."\">".$row['name']."</option>";		
	}
}

 

thats the php for one of my member drop downs,

and here is the form:

 <td width="175" align="center" class="cell"><select name="name" class="entryfield" id="name">
            <option value="">Select.</option>
		<?php echo $member_list; ?>	
      </select>      </td>

 

there is a lot more than that, but that is the basic's of it, im sure you will be able to fix somthing out of that.

 

~Py

Link to comment
Share on other sites

$member_list = "";
while($row = mysql_fetch_array( $result )) {
	if($row['name'] == $_POST['name']){
	$member_list .= "<option selected=\"selected\" value=\"".$row['name']."\">".$row['name']."</option>";
	}else{
	$member_list .= "<option value=\"".$row['name']."\">".$row['name']."</option>";		
	}
}

 

thats the php for one of my member drop downs,

and here is the form:

 <td width="175" align="center" class="cell"><select name="name" class="entryfield" id="name">
            <option value="">Select.</option>
		<?php echo $member_list; ?>	
      </select>      </td>

 

there is a lot more than that, but that is the basic's of it, im sure you will be able to fix somthing out of that.

 

~Py

 

ty very much

Link to comment
Share on other sites

No Problem it should work. you may have to tweek it to your spec, but i know it works on mine.

 

I have tweaked it as much as possible and even tried a new code, and I cannot get my drop down list to autopoulate.  Any other suggestions this is what I am using now. 

 

<?php

include "clientconnect.php";

  mysql_select_db('greencut_customercenter', $con);


  if ($result = mysql_query("SELECT * FROM clients")) {
    if (mysql_num_rows($result)) {
      echo "<form>";
      echo "  <select name=\"clients\">";
      while($row = mysql_fetch_array($result)) {
        echo "    <option value=\"{$row['client_lname']}\">{$row['client_fname']} {$row['client_lname']}</option>";
      }
      echo "  </select>";
      echo "</form>";
    }
  }
?>

Link to comment
Share on other sites

  • 4 years later...

I am currently working on something like this and was able to achieve the autopulation of my drop down list, the only difference here is that my database is postgresql while yours is mysql but there is not much difference in the syntax.

Here are the things I notice odd in your script.

echo " </select>";

The </select>"; should be outside the PHP tags

 

I will simply post this dropdown script for you here, if you dont mind taking a look at it and manipoulate yours to get it to function. It pretty easy

Edited by emmy
Link to comment
Share on other sites

Here are the things I notice odd in your script.

echo " </select>";

The </select>"; should be outside the PHP tags

 

That statement is incorrect. There is no reason any HTML code has to be in or outside the PHP tags as long as it is being properly formatted based on the context and the desired output. I see specifically nothing wrong with the code the OP last posted. But, since he didn't post what the output is that he is getting there's no way to know what the problem is.

Link to comment
Share on other sites

Well! I am not an expert in this but I am telling you from my own current personal experience my script did not populate until i did that. Here is a sample of my script that did the job. I think you can look at and try and try it out. It took me days to figure out that the problem had to do with the placement of my </select> tag

 

 

 

 

<html>

<head><title>Title</title></title></head>

<body>

<!--HTML Code to create the data entry form-->

<p></P>

 

<form action="fieldtally.php" method="post">

<table width="600" cellpadding= "10" cellspacing="1" border="2">

<tr align="center" valign="top">

<td align="center" colspan="1" rowspan="1" bgcolor="#64b1ff">

<h3></h3>

 

<select pipeno="pipeno" name="pipeno"><br><br>

 

<?php

 

//Php Code to connect to postgresqldatabase

include ("connection.php");

// Code to pull data from the database and load onto the form

echo "<br />\n";

$query = 'select pipeno, wallthickness from fieldtally order by pipeno asc ';

$result =mysql_query($db_handle,$query);

while ($row = mysql_fetch_row($result))

{

$pipeno = $row[0];

echo "<option>$pipeno</option>";

 

}

?>

</select>

</form>

</body>

</html>

 

Link to comment
Share on other sites

That statement is incorrect. There is no reason any HTML code has to be in or outside the PHP tags as long as it is being properly formatted based on the context and the desired output. I see specifically nothing wrong with the code the OP last posted. But, since he didn't post what the output is that he is getting there's no way to know what the problem is.

 

QFT.

Link to comment
Share on other sites

Well! I am not an expert in this but I am telling you from my own current personal experience my script did not populate until i did that. Here is a sample of my script that did the job. I think you can look at and try and try it out. It took me days to figure out that the problem had to do with the placement of my </select> tag

 

Well, I hate to tell you but your "personal experience" must be based upon a problem which you were not aware of. Whatever fixed your code had nothing to do about whether the select tag was echo'd within the PHP code or whether it was directly within HTML code. It very well may have been due to incorrectly echoing it in the wrong place. When usign code to generate other code it can be difficult to keep track of everything. But, I can categorically state that you can output the select tags within the PHP code to achieve the same results. The two blocks of code will result in the same output:

 

<select name='field_name'>

<?php

$options = array('1' => 'one', '2' => 'two', '3' => 'three');
foreach($options as $id => $value)
{
   echo "<option value='{$id}'>{$value}</option>\n";
}

?>

</select>

 

<?php

echo "<select name='field_name'>";

$options = array('1' => 'one', '2' => 'two', '3' => 'three');
foreach($options as $id => $value)
{
   echo "<option value='{$id}'>{$value}</option>\n";
}

echo "</select>";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.