Jump to content

[SOLVED] Help with function to display form from database!


doucie

Recommended Posts

Hello,

 

I'm new to all this php business, so I hope you can help?  I'm trying to write a function which gets all the client names from a table and displays them in a drop down list.  My function is displaying a form with nothing in!  Help!

 

function get_clients()

{

$db=mysqli_connect('localhost','root','','debtnet');

if(mysqli_connect_errno())

{

echo 'Could not connect to the database';

exit;

}

$query="SELECT client_name FROM groups";

$result=mysqli_query($db,$query); ?>

<form action="group.php" method="POST" />

<select name="Client" id="Client" />

<?php while($list=mysqli_fetch_array($result)){

echo "<option value='$list'</option>

</form>";

}

}

<?php
function get_clients()
{
   $db=mysqli_connect('localhost','root','','debtnet');
   if(mysqli_connect_errno())
   {
      echo 'Could not connect to the database';
      exit;
   }
   $query="SELECT client_name FROM groups";
   $result=mysqli_query($db,$query); ?>
   <form action="group.php" method="POST" />
   <select name="Client" id="Client" />
   <?php while($list=mysqli_fetch_array($result)){
      echo "<option value='".$list['client_name']."'>".$list['client_name']."</option>";

   }
   echo  "</form>";
}
?>

 

try that.

 

EDIT: fixed some syntax I noticed was wrong.

try this

 

<?php
function get_clients()
{
   $db=mysqli_connect('localhost','root','','debtnet');
   if(mysqli_connect_errno())
   {
      echo 'Could not connect to the database';
      exit;
   }
   $query="SELECT client_name FROM groups";
   $result=mysqli_query($db,$query);

   $results = '<form action="group.php" method="POST" />';
   $results .= '<select name="Client" id="Client" />';
     while($list=mysqli_fetch_array($result)){
      $results .= "<option value='".$list['client_name']."'>".$list['client_name']."</option>";
      }
   $results .= "</form>";
return $results;
}

echo get_clients();
?>

 

leaving it the way you have it you are create a form for every row of data in the table

 

Ray

yes you can have the value you want "value="blah blah blah"" but show friendly text in the dropdown menu

 

<option value="1">ON</option>

 

will display ON in the dropdown but will pass "1" as the value.

 

If you do not use a value it will pass what is used in between the option tags

 

<option>BlahBlahBlah</option>

 

Usually don't need a value parameter if the actual value is the same as what is being displayed

 

Ray

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.