What i have done till now was to store the dropdown selected data into the database , what i now would like is based on the username typed in the textbox and clicked on search button , the dropdown item to be selected is to be the data stored in the database along with a list of values which user should be able to choose if he wishes .
I just want to know how to replace the text Select from here with the selected data coming from the database as well as allow the user to edit the data as he wants in the dropdown and save it again in the database once he clicks the submit button .
Code While storing data is as follows
<?php
if( $_POST['registerbtn']){
$getuser= mysql_real_escape_string(trim($_POST['user']));
$selected_value = $_POST['selectID'];
if($selected_value != 0){
require("./connect.php");
$query=mysql_query("SELECT * FROM user_details WHERE username='$getuser'");
$numrows= mysql_num_rows($query);
if($numrows == 0) {
mysql_query("INSERT INTO user_details VALUES ( '','$getuser','$selected_value')");
}
else
$errormsg = "There is already a user with that username ." ;
mysql_close();
}
else
$errormsg = "You must select a linecard";
}
//code to fill dropdown
require("./connect.php");
$sql = "SELECT select_id, linecard_name FROM selection ". "ORDER BY linecard_name";
$rs = mysql_query($sql);
$options = "<option value = '0'> Select from here </option>";
while($rownw = mysql_fetch_array($rs)){
$options .= "<option value = ".$rownw['select_id']." > ".$rownw['linecard_name']. " </option> ";
}
mysql_close() ;
$form = "<form action='./reg.php' method='post'>
<table>
<tr>
<td> </td>
<td> <font color='red'> $errormsg </font> </td>
</tr>
<tr>
<td> Username </td>
<td> <input type='text' name='user' value='$getuser' /> </td>
</tr>
<tr>
<td> Select Linecard </td>
<td> <Select name='selectID' > $options </select> </td>
</tr>
<tr>
<td> </td>
<td> <input type='submit' name='registerbtn' value='Register' /> </td>
</tr>
</form>";
echo $form;
?>
Code while retrieving the data from the database to the dropdown based on username written in textbox is as follows
<?php
if( $_POST['savebtn']){
$search = $_POST['search'];
if($search == '' )
{
$errormsg1 = "Enter something in the search box";
}
else
{
require("./connect.php");
$query = mysql_query("SELECT * FROM user_details WHERE username = '$search'");
$numrows= mysql_num_rows($query);
$rows=mysql_fetch_assoc($query);
if($numrows == 1)
{
$getuser = $rows['username'];
$select_id= $rows['linecard_id'];
//selection table where linecard_name is stored along with select_id for the linecard_name to be displayed
$query1 = mysql_query("SELECT * FROM selection WHERE select_id = '$select_id' ") ;
$rows1=mysql_fetch_assoc($query1);
$linecard_name= $rows1['linecard_name'];
echo $linecard_name;
$sql = "SELECT select_id, linecard_name FROM selection ". "ORDER BY linecard_name";
$rs = mysql_query($sql);
while($rownw = mysql_fetch_array($rs)){
if( $select_id == $rownw['select_id']) {
echo $select_id;
$options .= "<option value = ". $select_id ." > " .$linecard_name. " </option> ";
}
}
}
else
$errormsg1 = "Username was Not found";
mysql_close() ;
}
}
?>
<?php
$form ="<form action='ad.php' method='post' id='search' >
<div class='place'>
<tr>
<td> </td>
<td> <font color='red' > $errormsg1 </font> </td>
</tr>
<div class='frm2'> <input type='text' size='15' value='$search' name='search' /> </div>
<div class='frm3'> <input type='submit' name='savebtn' /> </div>
<div class='clear'> </div>
</div>
</form>";
echo $form;
?>
<?php
require("./connect.php");
$sql = "SELECT select_id, linecard_name FROM selection "."ORDER BY linecard_name";
$rs = mysql_query($sql);
$options = "<option value = '0'> Select from here </option>";
while($rownw = mysql_fetch_array($rs)){
$options .= "<option value = ".$rownw['select_id']." > ".$rownw['linecard_name']. " </option> ";
}
mysql_close() ;
$form = "<form action='./reg.php' method='post'>
<table>
<tr>
<td> </td>
<td> <font color='red'> $errormsg </font> </td>
</tr>
<tr>
<td> Username </td>
<td> <input type='text' name='user' value='$getuser' /> </td>
</tr>
<tr>
<td> Select Linecard </td>
<td> <Select name='selectID' > $options </select> </td>
</tr>
<tr>
<td> </td>
<td> <input type='submit' name='savebtn' value='Save' /> </td>
</tr>
</form>";
echo $form;
?>
You could store the options into a vairable, let’s call it $options and then insert it into your $form variable. Finally you can echo it wherever you want.