jkkenzie Posted April 14, 2008 Share Posted April 14, 2008 on my combo box or select box below(name="strcategory") qs in code below, am able to populate select items from my database but when i echo from database to this select box , nothing happens e.g ( value="<?php echo $name; ?>" ). Other words i want to populate my input boxes on my page on my select input box but the problem is that the select box only populates the options user selects but not what i echo to it as a previous input by user ... <td width="100">Category Name:</td><td><select name="strcategory" type="text" value=""><?php $con = mysql_connect("localhost","root","superman"); if (!$con) { die("Could not connect: " . mysql_error()); } mysql_select_db("emobase", $con); $result = mysql_query("SELECT strCategory FROM tblcategories ORDER BY id"); while($row = mysql_fetch_array($result)) { print '<option value="'. $row['strCategory'] . '">' . $row['strCategory'] . '</option>'; } mysql_close($con); ?></select> Link to comment https://forums.phpfreaks.com/topic/101015-select-box/ Share on other sites More sharing options...
haku Posted April 14, 2008 Share Posted April 14, 2008 Add this to the option tag that you want to be selected on page load: selected="selected" For example: <select name="select_name"> <option value="1">1</option> <option value="2" selected="selected">2</option> </select> When this code is executed, the option number 2 will be already selected. Link to comment https://forums.phpfreaks.com/topic/101015-select-box/#findComment-516556 Share on other sites More sharing options...
jkkenzie Posted April 14, 2008 Author Share Posted April 14, 2008 that means i have to include code to check which value is available for that record in the database first and then set the selected item on the page as the one in record? Thanks Link to comment https://forums.phpfreaks.com/topic/101015-select-box/#findComment-516606 Share on other sites More sharing options...
jkkenzie Posted April 15, 2008 Author Share Posted April 15, 2008 How do i include database values to : <option value="1">1</option> <option value="2" selected="selected">2</option> i have tried several attempts but am only getting the select box populated and none from the database record is select, you have an example , ? thanks, Joe Link to comment https://forums.phpfreaks.com/topic/101015-select-box/#findComment-517558 Share on other sites More sharing options...
zenag Posted April 15, 2008 Share Posted April 15, 2008 <? $con = mysql_connect("localhost","root",""); if (!$con) { die("Could not connect: " . mysql_error()); } mysql_select_db("emobase", $con); $result = mysql_query("SELECT strCategory FROM tblcategories ORDER BY id"); ?> <select> <? while($row = mysql_fetch_array($result)) { print "<option value=". $row['strCategory'] . ">" . $row['strCategory'] . "</option>"; } mysql_close($con); ?></select> Link to comment https://forums.phpfreaks.com/topic/101015-select-box/#findComment-517566 Share on other sites More sharing options...
jkkenzie Posted April 16, 2008 Author Share Posted April 16, 2008 Thanks, that populates the drop down list. Now, there is a form, on a page that user edits the records, when the page loads, it populates the input boxes e.g <form> ///// <input name="text1" value="<?php echo $text1; ?>"> <? $con = mysql_connect("localhost","root",""); if (!$con) { die("Could not connect: " . mysql_error()); } mysql_select_db("emobase", $con); $result = mysql_query("SELECT strCategory FROM tblcategories ORDER BY id"); ?> <select> <? while($row = mysql_fetch_array($result)) { print "<option value=". $row['strCategory'] . ">" . $row['strCategory'] . "</option>"; } mysql_close($con); ?></select> ////// </form> From above you can see that i have used your code to populate the select box, but on my input text1, i have used a value from the database based on a search criteria, I WANT THE SELECT BOX *ALSO* TO SHOW A VALUE FROM DATABASE BASED ON MY SEARCH CRITERIA AS THE INPUT BOX NAME TEXT1 ABOVE. that is what i have been asking all along, i van populate the textbox as you have given me in your code, but how do i make one selected in respect to the value already in database on a particular record. Thanks for your code, Joe Link to comment https://forums.phpfreaks.com/topic/101015-select-box/#findComment-518234 Share on other sites More sharing options...
zenag Posted April 16, 2008 Share Posted April 16, 2008 can u show ur full code...... Link to comment https://forums.phpfreaks.com/topic/101015-select-box/#findComment-518341 Share on other sites More sharing options...
haku Posted April 16, 2008 Share Posted April 16, 2008 that is what i have been asking all along Your English hasn't been clear enough to understand all along. I still dont fully get what you are asking. Are you saying you want to populate the value of the drop down menu based on the text you enter into text1? If so, you will have to either do a page reload to do this, or you will need to use ajax (javascript). You cant enter a value into an input (by the way, you need to add a type to your input) and have something else on the same page reflect that value without reloading the page or using javascript. Link to comment https://forums.phpfreaks.com/topic/101015-select-box/#findComment-518346 Share on other sites More sharing options...
jkkenzie Posted April 22, 2008 Author Share Posted April 22, 2008 I have managed to do it, thanks for your help..... here are the pseudocodes for the page for those interested. The most important part i wanted was this part: This is what i was asking you: echo "<select name='strDivision'>"; while($nt=mysql_fetch_array($result2)){ echo "<option value='$nt[strDivision]'"; if($nt[strDivision] == $strDivision) { echo " selected"; } echo ">$nt[strDivision]</option>"; } echo "</select>"; The full code is here: <html> <head> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>EMO SOCIETY DEVELOPEMENT FOUNDATION </title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link rel="stylesheet" href="table.css" type="text/css"> </head> <body bgcolor="#007F00" background="/Themes/default/images/main_bg.gif" style="margin: 0 0 0 0;"> <table> <tr> <td align="left"><img src="../images/logo.JPG" align="left" width="100" height="80" ></img></td> <td width="80%"></td> <td align="right"><img src="../images/main.JPG" align="left" width="110" height="80"></img></td> </tr> </table><hr> <?php $search = $_GET['search']; @$start = $_GET["start"]; if($start =='') $start =0; $host = "localhost"; $username = "root"; $password = "xx"; $db = "emobase"; $table ="tblmembers"; $link = mysql_connect($host,$username,$password); if (!$link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db($db, $link); if (!$db_selected) { die ("Can't use $db : " . mysql_error()); } $res = mysql_query("SELECT tblmembers.ID as 'Emo Number', tblmembers.strFN as 'First Name', tblmembers.strMN as 'Middle Name',tblmembers.strLN as 'Surname', tblmembers.strIdNumber as 'ID / Passport' FROM `$table` ORDER BY ID"); @$rows = mysql_num_rows ($res); $sort = $_GET['sort']; $order = $_GET['order']; /////////ADD SHARES TO MEMBERS////////// if(isset($_POST['save'])) { $con=mysql_connect("localhost","root","superman"); if(!$con) { die('Could not connect:'.mysql_error()); } mysql_select_db("emobase",$con); // check if the Member Exists $check = "select ID from tblmembers where ID = '".$_POST['emono']."';"; $qry = mysql_query($check) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); $id=$_POST['emono']; $strshares = $_POST['strsharesnm']; $intnoofshares = $_POST['intnoofshares']; $curtotalamount = $_POST['curtamount']; $dtbought = $_POST['dtbought']; $curshareprice = $_POST['curshareprice']; $strcategory = $_POST['strcategory']; $cdsc = $_POST['cdsc']; $query = "INSERT INTO tblshares (M_ID, strShares, intNoOfShares, curTAmount, dtBought, curSharePrice, strCategory, cdsc) VALUES ('$id', '$strshares', '$intnoofshares', '$curtotalamount' , '$dtbought', '$curshareprice', '$strcategory', '$cdsc')"; mysql_query($query) or die(mysql_error()); $query = "FLUSH PRIVILEGES"; mysql_query($query) or die(mysql_error()); } else { } ///////// Test////////// if( is_numeric($_GET['get'])) { $resulttest = $_GET['get']; $result3=mysql_query("SELECT*FROM `$table` WHERE ID='$resulttest' OR strIdNumber='$resulttest' "); while($row2=mysql_fetch_array($result3)) { $id=$row2['ID']; $strIdNumber=$row2['strIdNumber']; $strTitle=$row2['strTitle']; $strFN=$row2['strFN']; $strMN=$row2['strMN']; $strLN=$row2['strLN']; $CurRegFees=$row2['CurRegFees']; $strBank = $row2['strBank']; $dtDateRegistered=$row2['dtDateRegistered']; $dtDOB=$row2['dtDOB']; $strTel=$row2['strTel']; $strCellPhone=$row2['strCellPhone']; $strAddress=$row2['strAddress']; $strEmail=$row2['strEmail']; $ckReceiptIssued=$row2['CkReceiptIssued']; $strReceiptNo=$row2['strReceiptNo']; $strSex=$row2['strSex']; $strOccupation=$row2['strOccupation']; $strEmployer=$row2['strEmployer']; $strHomeDistrict=$row2['strHomeDistrict']; $strDivision=$row2['strDivision']; $strLocation=$row2['strLocation']; $strSublocation=$row2['strSubLocation']; $strCountry=$row2['strCountry']; $strCountryOfResidence=$row2['strCountryOfResidence']; $strPlaceOfResidence=$row2['strPlaceOfResidence']; $strFormOfPayment=$row2['strFormOfPayment']; } } //*************** POPULATE THE FORM ************* if($order ==''){ $result = mysql_query("SELECT tblmembers.ID as 'EmoNumber', tblmembers.strFN as 'FirstName', tblmembers.strMN as 'MiddleName',tblmembers.strLN as 'Surname', tblmembers.strIdNumber as 'IDorPassport' FROM `$table` WHERE ID LIKE \"%$search%\" OR (strFN LIKE \"%$search%\" or strMN LIKE \"%$search%\" or strLN LIKE \"%$search%\") ORDER BY ID ASC, strFN DESC LIMIT $start,10");} else{ $result = mysql_query("SELECT tblmembers.ID as 'EmoNumber', tblmembers.strFN as 'FirstName', tblmembers.strMN as 'MiddleName',tblmembers.strLN as 'Surname', tblmembers.strIdNumber as 'IDorPassport' FROM `$table` WHERE ID LIKE \"%$search%\" OR (strFN LIKE \"%$search%\" or strMN LIKE \"%$search%\" or strLN LIKE \"%$search%\") ORDER BY $sort $order LIMIT $start,10");} if (!$result) { die('Invalid query: ' . mysql_error()); } echo "<table align='center' width='750'><td><form> <input type='hidden' name='sort' value='ID asc,'> <input type='hidden' name='order' value='strFN desc'> <input name='search' type='text' value='$search'> <input type='submit' value='search'></td></table>"; $cols = mysql_num_fields($result); $records = mysql_num_rows ($result); echo "<table align='center' bgcolor='FFFFFF' width='750' cellspacing='1' cellpadding='0' border='0' >"; echo "<tr bgcolor='D49F00' class='menu'>"; if($order =='ASC'){ $vorder = 'DESC'; } else{ $vorder = 'ASC'; } for ($i = 0; $i < $cols;$i++) { echo "<td align='center'><a href='?sort=".mysql_field_name($result,$i)."&order=$vorder'>".mysql_field_name($result,$i)."</a></td>"; } echo "</tr>"; while ($row = mysql_fetch_array($result, MYSQL_NUM)) { echo "<tr bgcolor='F6F6F6' class='normal'>"; foreach ($row as $value) { echo "<td class='table'><a href='?get=".$value."&val='''>".$value ."</a></td>"; } echo "</tr>"; } $end = $start + $records; echo " <tr> <td width='30'></td> <td width='80'></td> </tr> <tr align = 'center' bgcolor = 'D49F00' class='menu'><td colspan=$cols>"; if($start != 0) { $prev = $start - 10; echo "<font size='3'>|</font> <a href='editmember.php?search=$search&sort=$sort&order=$order&start=$prev'>Previous</a> <font size='3'>|</font>"; } echo " Records $start to $end of $rows "; if($start<$rows-10) { $next = $start + 10; echo "<font size='3'>|</font> <a href='editmember.php?search=$search&sort=$sort&order=$order&start=$next'>Next</a> <font size='3'>|</font>"; } echo "</td></tr></form></table>"; ?><br> <table align='center' width='500' bgcolor="#AAFF55"> <tr> <td><a href="index.php"><b>Home</b></a> </td> <td align="left"><a href="editmember.php"><b>Refresh</b></a> </td> <td></td> </tr> </table><br> <table align='center' width='1000' cellspacing="10"><td> <form method='post'> <tr> <td width="43%">EMO No.:<td><input name="id" type="text" value="<?php echo $id; ?>"> <td width="43%">ID/Passport:<td><input name="strIdNumber" type="text" value="<?php echo $strIdNumber; ?>"> <td width="43%">Title:<td><?php $result2 = mysql_query("SELECT TITLE FROM tbltitles ORDER BY TITLE"); echo "<select name='strTitle'>"; while($nt=mysql_fetch_array($result2)){ echo "<option value='$nt[TITLE]'"; if($nt[TITLE] == $strTitle) { echo " selected"; } echo ">$nt[TITLE]</option>"; } echo "</select>"; ?> </tr> <tr> <td width="43%">First Name:<td><input name="strFN" type="text" value="<?php echo $strFN; ?>"> <td width="43%">Middle Name:<td><input name="strMN" type="text" value="<?php echo $strMN; ?>"> <td width="43%">Surname:<td><input name="strLN" type="text" value="<?php echo $strLN; ?>"> </tr> <tr> <td width="120">Registration Fees Kshs:</td><td><input name="CurRegFees" type="text" value="<?php echo $CurRegFees; ?>"></td> <td width="100">Bank:</td><td><?php $result2 = mysql_query("SELECT strBanks FROM tblbanks ORDER BY strBanks"); echo "<select name='strBank'>"; while($nt=mysql_fetch_array($result2)){ echo "<option value='$nt[strBanks]'"; if($nt[strBanks] == $strBank) { echo " selected"; } echo ">$nt[strBanks]</option>"; } echo "</select>"; ?> </td> <td width="120">Receipt Issued:</td><td><input name="ckReceiptIssued" type="checkbox" <?php if( $ckReceiptIssued == 1){ $selected = 'checked="checked"'; }else{ $selected = ''; } echo '<input name="ckReceiptIssued" type="checkbox" '.$selected.''; ?> /></td> </tr> <tr> <td width="105">Receipt No.:</td><td><input name="strReceiptNo" type="text" value="<?php echo $strReceiptNo; ?>"></td> <td width="115">Date Registered:</td><td><input name="dtDateRegistered" type="text" value="<?php echo $dtDateRegistered; ?>"></td> <td width="100">Y.O.B:</td><td><input name="dtDOB" type="text" value="<?php echo $dtDOB; ?>"></td> </tr> <tr> <td width="100">Telephone:</td><td><input name="strTel" type="text" value="<?php echo $strTel; ?>"></td> <td width="100">Cell Phone:</td><td><input name="strCellPhone" type="text" value="<?php echo $strCellPhone; ?>"></td> <td width="100">Address:</td><td><input name="strAddress" type="text" value="<?php echo $strAddress; ?>"></td> </tr> <tr> <td width="100">E-mail:</td><td><input name="strEmail" type="text" value="<?php echo $strEmail; ?>"></td> <td width="100">Gender:</td><td><?php $result2 = mysql_query("SELECT SEX FROM tblgender ORDER BY SEX"); echo "<select name='strSex'>"; while($nt=mysql_fetch_array($result2)){ echo "<option value='$nt[sEX]'"; if($nt[sEX] == $strSex) { echo " selected"; } echo ">$nt[sEX]</option>"; } echo "</select>"; ?></td> <td width="100">Occupation:</td><td><?php $result2 = mysql_query("SELECT DISTINCT strOccupation FROM tblmembers ORDER BY strOccupation"); echo "<select name='strOccupation'>"; while($nt=mysql_fetch_array($result2)){ echo "<option value='$nt[strOccupation]'"; if($nt[strOccupation] == $strOccupation) { echo " selected"; } echo ">$nt[strOccupation]</option>"; } echo "</select>"; ?></td> </tr> <tr> <td width="100">Employer:</td><td><input name="strEmployer" type="text" value="<?php echo $strEmployer; ?>"></td> <td width="100">Home District:</td><td><?php $result2 = mysql_query("SELECT DISTINCT strHomeDistrict FROM tblhomedistricts ORDER BY strHomeDistrict"); echo "<select name='strHomeDistrict'>"; while($nt=mysql_fetch_array($result2)){ echo "<option value='$nt[strHomeDistrict]'"; if($nt[strHomeDistrict] == $strHomeDistrict) { echo " selected"; } echo ">$nt[strHomeDistrict]</option>"; } echo "</select>"; ?></td> <td width="100">Division:</td><td><?php $result2 = mysql_query("SELECT DISTINCT strDivision FROM tbldivisions ORDER BY strDivision"); echo "<select name='strDivision'>"; while($nt=mysql_fetch_array($result2)){ echo "<option value='$nt[strDivision]'"; if($nt[strDivision] == $strDivision) { echo " selected"; } echo ">$nt[strDivision]</option>"; } echo "</select>"; ?></td> </tr> <tr> <td width="100">Location:</td><td><input name="strLocation" type="text" value="<?php echo $strLocation; ?>"></td> <td width="100">SubLocation:</td><td><input name="strSubLocation" type="text" value="<?php echo $strSublocation; ?>"></td> <td width="100">Country:</td><td><input name="strCountry" type="text" value="<?php echo $strCountry; ?>"></td> </tr> <tr> <td width="120">Country Of Residence:</td><td><input name="strCountryOfResidence" type="text" value="<?php echo $strCountryOfResidence; ?>"></td> <td width="120">Current Residence:</td><td><input name="strPlaceOfResidence" type="text" value="<?php echo $strPlaceOfResidence; ?>"></td> <td width="120">Form Of Paymenet:</td><td><input name="strFormOfPayment" type="text" value="<?php echo $strFormOfPayment; ?>"></td> </tr> </table> <table align="center" width="750"><td> <tr> <td bgcolor="A0A0A4" width="10"><input type="submit" name="save" id="save" value="Save New"></td> <td bgcolor="A0A0A4" width="10"><input type="submit" name="edit" id="edit" value="Save Edit"></td> </tr> </form></table> <hr> <table> <tr><br> <td align="left"><img src="../images/below.JPG" align="left" width="50" height="50"></img></td> </tr> </table> </body> </html> Link to comment https://forums.phpfreaks.com/topic/101015-select-box/#findComment-524088 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.