spenceddd Posted December 24, 2009 Share Posted December 24, 2009 Hello, Please bear with me as I am very new to php and html. I have a form with several drop down menus, the one at the top shows a list that when an item is selected fills in the form below including the other drop downs. So when an item is selected the form gets info from a mysql database and then fills in the form. At this point the user can further modify the form and re-submit it to update the database. This means that the drop downs in the form not only need to display the relavent options from the database to reflect the existing choice but also when clicked on show a list to allow the user the change the selection from the list. This means that the dropdown needs to: show the words 'select item' when the form is first loaded and the form is empty show the current selection when the defined by a selection in the top drodown show all the available items when the list is dropped down Curently my drop down does only this: shows the nothing when the form is first loaded and the form is empty shows the current selection when defined by a selection in the top drodown show all the available items when the list is dropped down plus the one that was there before it was clicked on, therefore doubing the option up showing the same option twice The form is here if you would like to see it in action: http://www.spencercarpenter.co.uk/portfolioAppFiles/simpleForm.php And a snippet of code from one of th drop downs is here: <select name="img01" onChange="updateImg(this, 'thumbImg01')"> <option value=-"-1" ><?php echo $row_chosen_pItem['img01']; ?></option> <?php buildFileList5('uploaded');?> </select> the code inside the 'buildFileList5' looks like this: <?php function buildFileList5($theFolder) { // Execute code if the folder can be opened, or fail silently if ($contents = @ scandir($theFolder)) { // initialize an array for matching files $found = array(); // Create an array of file types $fileTypes = array('jpg','jpeg','gif','png'); // Traverse the folder, and add filename to $found array if type matches $found = array(); foreach ($contents as $item) { $fileInfo = pathinfo($item); if (array_key_exists('extension', $fileInfo) && in_array($fileInfo['extension'],$fileTypes)) { $found[] = $item; } } // Check the $found array is not empty if ($found) { // Sort in natural, case-insensitive order, and populate menu natcasesort($found); $selectedImage = ""; //default image foreach ($found as $filename) { if(!empty($_POST['img01']) && $_POST['img01'] == $filename) { $sel = "SELECTED"; $selectedImage = $filename; } echo "<option value='$filename' $sel>$filename</option>\n"; } } } } ?> I hope that is enough info to explain it. If anyone could tell how to get the dropdown working properly I would be very gratefull. Thanks a lot. Spencer Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/ Share on other sites More sharing options...
Buddski Posted December 24, 2009 Share Posted December 24, 2009 Can you attach your file here.. Ill have a read and see what i can do for ya Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983586 Share on other sites More sharing options...
spenceddd Posted December 24, 2009 Author Share Posted December 24, 2009 yeah sure! I will insert all the files here (except the conection info of course). This is the file called simpleForm.php: <?php include('connectToDB.php'); include('populateDropDownList.php'); include('buildFileList5.php'); ?> <html> <head> <script type="text/javascript"> var existingPortfolioItems = new Array(); function chkFrm(el){ if(el.selectedIndex == 0){ alert("Please choose an option"); return false } else{ el.form.submit(); } } function updateImg(ele, img) { // using arguments makes this function reusable for multiple items // ele = dropdown object // img = image object document.getElementById(img).src = "uploaded/thumbs/" + ele.value; //document[img].alt = ele.value; } function checkForDuplicateNames(nameToCheck) { Array.prototype.chkforDuplicates = function (value) // Returns true if the passed value is found in the // array. Returns false if it is not. { var i; var ctr = 0; for (i=0; i < this.length; i++) { // use === to check for Matches. ie., identical (===), ; if (this[i] == value) { return true; } } return false; }; } </script> </head> <body> <h1>Select Existing Item: </h1> <form action"" method="POST" >Existing Item: <select name="name" onChange="chkFrm(this)"> <option value="Choose" <?php if (!(strcmp("Choose", $_POST['name']))) {echo "SELECTED";} ?>>Choose Portfolio Items</option> <?php do { ?> <option value="<?php echo $row_PortfolioItems['id']?>"<?php if (!(strcmp($row_PortfolioItems['id'], $_POST['name']))) {echo "SELECTED";} ?>><?php echo $row_PortfolioItems['name']?></option> <?php } while ($row_PortfolioItems = mysql_fetch_assoc($PortfolioItems)); $rows = mysql_num_rows($PortfolioItems); if($rows > 0) { mysql_data_seek($PortfolioItems, 0); $row_PortfolioItems = mysql_fetch_assoc($PortfolioItems); } ?> </select> </form> <form action="addToPortfolioItems.php" onSubmit="return checkForDuplicateNames("name")" method="post"> <table width="979" height="481" border="1"> <tr> <td width="417" rowspan="3">Name: <input name="name" type="text" value="<?php echo $row_chosen_pItem['name']; ?>" size="25"> <p>Short Description:</p> <p> <textarea name="shortDesc" cols="55" rows="4"><?php echo $row_chosen_pItem['shortDesc']; ?></textarea> </p> <p>Long Description:</p> <p> <textarea name="longDesc" cols="55" rows="10"><?php echo $row_chosen_pItem['longDesc']; ?></textarea> </p> <p> </p></td> <td width="257"><p>Image01: <select name="img01" onChange="updateImg(this, 'thumbImg01')"> <option value=-"-1" ><?php echo $row_chosen_pItem['img01']; ?></option> <?php buildFileList5('uploaded');?> </select> </p> <table width="24" border="1"> <tr> <td><img src="uploaded/thumbs/<?php echo $row_chosen_pItem['img01'];?>" alt="hoho" name="thumbImg01" width="100" height="100" id="thumbImg01"></td> <td><?php echo $row_chosen_pItem['img01']; ?> </td> </tr> </table></td> <td width="283" height="109"><p>Image04: <select name="img04" onChange="updateImg(this, 'thumbImg04')"> <option value=-"-1" ><?php echo $row_chosen_pItem['img02']; ?> </option> <?php buildFileList5('uploaded'); ?> </select> </p> <table width="24" border="1"> <tr> <td><img src="uploaded/thumbs/<?php echo $row_chosen_pItem['img04'];?>" alt="hh" name="thumbImg04" width="100" height="100" id="thumbImg04"></td> <td> </td> </tr> </table></td> </tr> <tr> <td height="150"><p>Image02: <select name="img02" onChange="updateImg(this, 'thumbImg02')"> <option value=-"-1" ><?php echo $row_chosen_pItem['img02']; ?> </option> <?php buildFileList5('uploaded'); ?> </select> </p> <table width="24" border="1"> <tr> <td><img src="uploaded/thumbs/<?php echo $row_chosen_pItem['img04'];?>" alt="hh" name="thumbImg02" width="100" height="100" id="thumbImg02"></td> <td> </td> </tr> </table></td> <td><p>Image05: <select name="img05" onChange="updateImg(this, 'thumbImg05')"> <option value=-"-1" >Choose Image </option> <?php buildFileList5('uploaded'); ?> </select> </p> <table width="24" border="1"> <tr> <td><img src="uploaded/thumbs/<?php echo $row_chosen_pItem['img05'];?>" alt="hh" name="thumbImg05" width="100" height="100" id="thumbImg05"></td> <td> </td> </tr> </table></td> </tr> <tr> <td height="157"><p>Image03: <select name="img03" onChange="updateImg(this, 'thumbImg03')"> <option value=-"-1" ><?php echo $row_chosen_pItem['img03']; ?> </option> <?php buildFileList5('uploaded'); ?> </select> </p> <table width="24" border="1"> <tr> <td><img src="uploaded/thumbs/<?php echo $row_chosen_pItem['img03'];?>" alt="hh" name="thumbImg03" width="100" height="100" id="thumbImg03"></td> <td> </td> </tr> </table></td> <td> </td> </tr> </table> <p>Related Item01 <select name="relatedItem01" > <option value="" <?php if (!(strcmp("Choose", $_POST['relateItem01']))) {echo "SELECTED";} ?>>Choose Item</option> <?php do { ?> <option value="<?php echo $row_PortfolioItems['id']?>"<?php if (!(strcmp($row_PortfolioItems['id'], $_POST['relateItem01']))) {echo "name";} ?>><?php echo $row_PortfolioItems['name']?></option> <?php } while ($row_PortfolioItems = mysql_fetch_assoc($PortfolioItems)); $rows = mysql_num_rows($PortfolioItems); if($rows > 0) { mysql_data_seek($PortfolioItems, 0); $row_PortfolioItems = mysql_fetch_assoc($PortfolioItems); } ?> </select> </p> <p>Related Item02 <select name="relateItem02" > <option value="Choose" <?php if (!(strcmp("Choose", $_POST['relateItem01']))) {echo "SELECTED";} ?>>Choose Item</option> <?php do { ?> <option value="<?php echo $row_PortfolioItems['id']?>"<?php if (!(strcmp($row_PortfolioItems['id'], $_POST['relateItem02']))) {echo "name";} ?>><?php echo $row_PortfolioItems['name']?></option> <?php } while ($row_PortfolioItems = mysql_fetch_assoc($PortfolioItems)); $rows = mysql_num_rows($PortfolioItems); if($rows > 0) { mysql_data_seek($PortfolioItems, 0); $row_PortfolioItems = mysql_fetch_assoc($PortfolioItems); } ?> </select> <p>Related Item03 <select name="relateItem03" > <option value="Choose" <?php if (!(strcmp("Choose", $_POST['relateItem03']))) {echo "SELECTED";} ?>>Choose Item</option> <?php do { ?> <option value="<?php echo $row_PortfolioItems['id']?>"<?php if (!(strcmp($row_PortfolioItems['id'], $_POST['relateItem01']))) {echo "name";} ?>><?php echo $row_PortfolioItems['name']?></option> <?php } while ($row_PortfolioItems = mysql_fetch_assoc($PortfolioItems)); $rows = mysql_num_rows($PortfolioItems); if($rows > 0) { mysql_data_seek($PortfolioItems, 0); $row_PortfolioItems = mysql_fetch_assoc($PortfolioItems); } ?> </select> <p>Keywords: <input name="keywords" type="text" value="<?php echo $row_chosen_pItem['keywords']; ?>" size="50"> <p>Skills: <input name="skills" type="text" value="<?php echo $row_chosen_pItem['skills']; ?>" size="50"> <p><input type="submit"/> </form> <h1>Upload an image file</h1> <form enctype="multipart/form-data" action="uploaderAndResize.php" method="POST"><input type="file" name="myfile"> <p> <input name="Submit" type='submit' value="upload" > <?echo $heldVariable?> </form> <h1>Upload a movie file</h1> <form enctype="multipart/form-data" action="uploaderAndResize.php" method="POST"><input type="file" name="myfile"> <p> <input name="Submit" type='submit' value="upload" > <?echo $heldVariable?> </form> <h1>Upload a download file</h1> <form enctype="multipart/form-data" action="uploaderAndResize.php" method="POST"><input type="file" name="myfile"> <p> <input name="Submit" type='submit' value="upload" > <?echo $heldVariable?> </form> </body> </html> <?php ?> This is the file called populateDropDownList.php: <?php /* 1. Query the database and get the ID and name FROM the table PortfolioItems */ mysql_select_db($database_db, $dbc); $query_PortfolioItems = "SELECT id, name FROM PortfolioItems"; $PortfolioItems = mysql_query($query_PortfolioItems, $dbc) or die(mysql_error()); $row_PortfolioItems = mysql_fetch_assoc($PortfolioItems); $totalRows_PortfolioItems = mysql_num_rows($PortfolioItems); /* 2. If the form was submitted then get the ID from the name text box * This is used for the form location */ $colname_chosen_pItem = "-1"; if (isset($_POST['name'])) { $colname_chosen_pItem = (get_magic_quotes_gpc()) ? $_POST['name'] : addslashes($_POST['name']); } mysql_select_db($database_db, $dbc); $query_chosen_pItem = sprintf("SELECT * FROM PortfolioItems WHERE id = %s", $colname_chosen_pItem); $chosen_pItem = mysql_query($query_chosen_pItem, $dbc) or die(mysql_error()); $row_chosen_pItem = mysql_fetch_assoc($chosen_pItem); $totalRows_chosen_pItem = mysql_num_rows($chosen_pItem); ?> This is the file called addToPortfolioItems.php: <?php include('connectToDB.php'); ?> <html> <head> <form action="" method="POST"> <input name="name" type="text" value="<?php echo $_POST[name]; ?>" size="25"> <?php $nameField= $_POST[name]; //testForDuplicateNames $query="SELECT * FROM PortfolioItems WHERE name='$_POST[name]'"; $result=mysql_query($query); $num=mysql_num_rows($result); if ($num == 0){ $query="INSERT INTO PortfolioItems (name, shortDesc, longDesc, keywords, skills, img01, img02, img03, img04, img05) VALUES ('$_POST[name]','$_POST[shortDesc]','$_POST[longDesc]', '$_POST[keywords]', '$_POST[skills]', '$_POST[img01]', '$_POST[img02]', '$_POST[img03]', '$_POST[img04]', '$_POST[img05]')"; $result=mysql_query($query); header("Location: simpleForm.php"); //* } else{ echo ("<script> <!-- location.replace(\"InvalidName.html\"); --> </script>"); } //*/ ?> </form> </head> </html> and finally here is the buildFileList5.php again: <?php function buildFileList5($theFolder) { // Execute code if the folder can be opened, or fail silently if ($contents = @ scandir($theFolder)) { // initialize an array for matching files $found = array(); // Create an array of file types $fileTypes = array('jpg','jpeg','gif','png'); // Traverse the folder, and add filename to $found array if type matches $found = array(); foreach ($contents as $item) { $fileInfo = pathinfo($item); if (array_key_exists('extension', $fileInfo) && in_array($fileInfo['extension'],$fileTypes)) { $found[] = $item; } } // Check the $found array is not empty if ($found) { // Sort in natural, case-insensitive order, and populate menu natcasesort($found); $selectedImage = ""; //default image foreach ($found as $filename) { if(!empty($_POST['img01']) && $_POST['img01'] == $filename) { $sel = "SELECTED"; $selectedImage = $filename; } echo "<option value='$filename' $sel>$filename</option>\n"; } } } } ?> Thanks a lot Buddski and sorry if some of the code is a bit messed up, like I said I'm very new to this stuff. Spencer Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983591 Share on other sites More sharing options...
Buddski Posted December 24, 2009 Share Posted December 24, 2009 Firstly.. I would change the image dropdowns to <select name="img01" onChange="updateImg(this, 'thumbImg01')"> <?php echo buildFileList5('uploaded',$row_chosen_pItem['img01']);?> </select> and for the builtFileList5() ive made a few tweaks for ya.. Dont delete your old one incase this isnt what you are after but here it is <?php function buildFileList5($theFolder, $value) { // Execute code if the folder can be opened, or fail silently if ($contents = @scandir($theFolder)) { // initialize an array for matching files $found = $output = array(); // Create an array of file types $fileTypes = array('jpg','jpeg','gif','png'); // Traverse the folder, and add filename to $found array if type matches $found = array(); foreach ($contents as $item) { $fileInfo = pathinfo($item); if (array_key_exists('extension', $fileInfo) && in_array($fileInfo['extension'],$fileTypes)) { $found[] = $item; } } // Check the $found array is not empty if (!empty($found)) { // Sort in natural, case-insensitive order, and populate menu natcasesort($found); $selectedImage = ""; //default image foreach ($found as $filename) { if($value == $filename) { $sel = "selected=\"selected\""; $selectedImage = $filename; } $output[] = "<option value=\"$filename\" $sel>$filename</option>\n"; } } return (empty($output) ? '<option value="-1">No Items Found in `'.$theFolder.'`</option>' : '<option value="-1">Please select an Item</option'."\n".join("\n",$output)); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983596 Share on other sites More sharing options...
ignace Posted December 24, 2009 Share Posted December 24, 2009 Please bear with me as I am very new to php and html. Well looking at your code you had a good start. Only one recommendation: create functions that do one thing only thus not read from a database and then output html instead create a function for both like so: define('URI_IMAGES', 'path/to/images/directory'); .. print htmlSelect(getImagesFromDirectory(URI_IMAGES, array('jpg', 'gif')), array('id' => 'img01', 'name' => 'img01', 'onchange' => 'updateImg(this, \'img01\')')); Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983597 Share on other sites More sharing options...
spenceddd Posted December 24, 2009 Author Share Posted December 24, 2009 Thanks guys! I will try to implement your suggestions. I might take a while but I will let you know how it goes. Spencer Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983606 Share on other sites More sharing options...
spenceddd Posted December 24, 2009 Author Share Posted December 24, 2009 Hi Buddski, Thanks so much for your help I have tried to get that working but have a couple of issues: All the dropdown that have an image assigned to them always seem to show the same image name each time, 'Winter Leaves' Only the first image box seems to show an image - this may be my fault but I haven't been able to get to the bottom of it yet... Spencer Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983658 Share on other sites More sharing options...
Buddski Posted December 24, 2009 Share Posted December 24, 2009 Did you change $row_chosen_pItem['img01'] in the buildFileList5 function to match the values? so image 2 would use $row_chosen_pItem['img02'] etc? Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983659 Share on other sites More sharing options...
spenceddd Posted December 24, 2009 Author Share Posted December 24, 2009 Yes and i double checked that too, here is the code from the file: <?php include('connectToDB.php'); include('populateDropDownList.php'); include('buildFileList5.php'); ?> <html> <head> <script type="text/javascript"> var existingPortfolioItems = new Array(); function chkFrm(el){ if(el.selectedIndex == 0){ alert("Please choose an option"); return false } else{ el.form.submit(); } } function updateImg(ele, img) { // using arguments makes this function reusable for multiple items // ele = dropdown object // img = image object document.getElementById(img).src = "uploaded/thumbs/" + ele.value; //document[img].alt = ele.value; } function checkForDuplicateNames(nameToCheck) { Array.prototype.chkforDuplicates = function (value) // Returns true if the passed value is found in the // array. Returns false if it is not. { var i; var ctr = 0; for (i=0; i < this.length; i++) { // use === to check for Matches. ie., identical (===), ; if (this[i] == value) { return true; } } return false; }; } </script> </head> <body> <h1>Select Existing Item: </h1> <form action"" method="POST" >Existing Item: <select name="name" onChange="chkFrm(this)"> <option value="Choose" <?php if (!(strcmp("Choose", $_POST['name']))) {echo "SELECTED";} ?>>Choose Portfolio Items</option> <?php do { ?> <option value="<?php echo $row_PortfolioItems['id']?>"<?php if (!(strcmp($row_PortfolioItems['id'], $_POST['name']))) {echo "SELECTED";} ?>><?php echo $row_PortfolioItems['name']?></option> <?php } while ($row_PortfolioItems = mysql_fetch_assoc($PortfolioItems)); $rows = mysql_num_rows($PortfolioItems); if($rows > 0) { mysql_data_seek($PortfolioItems, 0); $row_PortfolioItems = mysql_fetch_assoc($PortfolioItems); } ?> </select> </form> <form action="addToPortfolioItems.php" onSubmit="return checkForDuplicateNames("name")" method="post"> <table width="979" height="481" border="1"> <tr> <td width="417" rowspan="3">Name: <input name="name" type="text" value="<?php echo $row_chosen_pItem['name']; ?>" size="25"> <p>Short Description:</p> <p> <textarea name="shortDesc" cols="55" rows="4"><?php echo $row_chosen_pItem['shortDesc']; ?></textarea> </p> <p>Long Description:</p> <p> <textarea name="longDesc" cols="55" rows="10"><?php echo $row_chosen_pItem['longDesc']; ?></textarea> </p> <p> </p></td> <td width="257"><p>Image01: <select name="img01" onChange="updateImg(this, 'thumbImg01')"> <?php echo buildFileList5('uploaded',$row_chosen_pItem['img01']); ?> </select> </p> <table width="24" border="1"> <tr> <td><img src="uploaded/thumbs/<?php echo $row_chosen_pItem['img01'];?>" alt="hoho" name="thumbImg01" width="100" height="100" id="thumbImg01"></td> <td><?php echo $row_chosen_pItem['img01']; ?> </td> </tr> </table></td> <td width="283" height="109"><p>Image04: <select name="img04" onChange="updateImg(this, 'thumbImg04')"> <?php echo buildFileList5('uploaded',$row_chosen_pItem['img04']); ?> </select> </p> <table width="24" border="1"> <tr> <td><img src="uploaded/thumbs/<?php echo $row_chosen_pItem['img04'];?>" alt="hh" name="thumbImg04" width="100" height="100" id="thumbImg04"></td> <td> </td> </tr> </table></td> </tr> <tr> <td height="150"><p>Image02: <select name="img02" onChange="updateImg(this, 'thumbImg02')"> <?php echo buildFileList5('uploaded',$row_chosen_pItem['img02']); ?> </select> </p> <table width="24" border="1"> <tr> <td><img src="uploaded/thumbs/<?php echo $row_chosen_pItem['img04'];?>" alt="hh" name="thumbImg02" width="100" height="100" id="thumbImg02"></td> <td> </td> </tr> </table></td> <td><p>Image05: <select name="img05" onChange="updateImg(this, 'thumbImg05')"> <?php echo buildFileList5('uploaded',$row_chosen_pItem['img05']); ?> </select> </p> <table width="24" border="1"> <tr> <td><img src="uploaded/thumbs/<?php echo $row_chosen_pItem['img05'];?>" alt="hh" name="thumbImg05" width="100" height="100" id="thumbImg05"></td> <td> </td> </tr> </table></td> </tr> <tr> <td height="157"><p>Image03: <select name="img03" onChange="updateImg(this, 'thumbImg03')"> <?php echo buildFileList5('uploaded',$row_chosen_pItem['img03']); ?> </select> </p> <table width="24" border="1"> <tr> <td><img src="uploaded/thumbs/<?php echo $row_chosen_pItem['img03'];?>" alt="hh" name="thumbImg03" width="100" height="100" id="thumbImg03"></td> <td> </td> </tr> </table></td> <td> </td> </tr> </table> <p>Related Item01 <select name="relatedItem01" > <option value="" <?php if (!(strcmp("Choose", $_POST['relateItem01']))) {echo "SELECTED";} ?>>Choose Item</option> <?php do { ?> <option value="<?php echo $row_PortfolioItems['id']?>"<?php if (!(strcmp($row_PortfolioItems['id'], $_POST['relateItem01']))) {echo "name";} ?>><?php echo $row_PortfolioItems['name']?></option> <?php } while ($row_PortfolioItems = mysql_fetch_assoc($PortfolioItems)); $rows = mysql_num_rows($PortfolioItems); if($rows > 0) { mysql_data_seek($PortfolioItems, 0); $row_PortfolioItems = mysql_fetch_assoc($PortfolioItems); } ?> </select> </p> <p>Related Item02 <select name="relateItem02" > <option value="Choose" <?php if (!(strcmp("Choose", $_POST['relateItem01']))) {echo "SELECTED";} ?>>Choose Item</option> <?php do { ?> <option value="<?php echo $row_PortfolioItems['id']?>"<?php if (!(strcmp($row_PortfolioItems['id'], $_POST['relateItem02']))) {echo "name";} ?>><?php echo $row_PortfolioItems['name']?></option> <?php } while ($row_PortfolioItems = mysql_fetch_assoc($PortfolioItems)); $rows = mysql_num_rows($PortfolioItems); if($rows > 0) { mysql_data_seek($PortfolioItems, 0); $row_PortfolioItems = mysql_fetch_assoc($PortfolioItems); } ?> </select> <p>Related Item03 <select name="relateItem03" > <option value="Choose" <?php if (!(strcmp("Choose", $_POST['relateItem03']))) {echo "SELECTED";} ?>>Choose Item</option> <?php do { ?> <option value="<?php echo $row_PortfolioItems['id']?>"<?php if (!(strcmp($row_PortfolioItems['id'], $_POST['relateItem01']))) {echo "name";} ?>><?php echo $row_PortfolioItems['name']?></option> <?php } while ($row_PortfolioItems = mysql_fetch_assoc($PortfolioItems)); $rows = mysql_num_rows($PortfolioItems); if($rows > 0) { mysql_data_seek($PortfolioItems, 0); $row_PortfolioItems = mysql_fetch_assoc($PortfolioItems); } ?> </select> <p>Keywords: <input name="keywords" type="text" value="<?php echo $row_chosen_pItem['keywords']; ?>" size="50"> <p>Skills: <input name="skills" type="text" value="<?php echo $row_chosen_pItem['skills']; ?>" size="50"> <p><input type="submit"/> </form> <h1>Upload an image file</h1> <form enctype="multipart/form-data" action="uploaderAndResize.php" method="POST"><input type="file" name="myfile"> <p> <input name="Submit" type='submit' value="upload" > <?echo $heldVariable?> </form> <h1>Upload a movie file</h1> <form enctype="multipart/form-data" action="uploaderAndResize.php" method="POST"><input type="file" name="myfile"> <p> <input name="Submit" type='submit' value="upload" > <?echo $heldVariable?> </form> <h1>Upload a download file</h1> <form enctype="multipart/form-data" action="uploaderAndResize.php" method="POST"><input type="file" name="myfile"> <p> <input name="Submit" type='submit' value="upload" > <?echo $heldVariable?> </form> </body> </html> <?php ?> Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983666 Share on other sites More sharing options...
Buddski Posted December 24, 2009 Share Posted December 24, 2009 It would appear that the values for the rest of the images arent there.. put this at the bottom of populateDropDownList.php: echo '<pre>'; print_r($row_chosen_pItem); echo '</pre>'; to see what $row_choses_pItem contains. Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983676 Share on other sites More sharing options...
spenceddd Posted December 24, 2009 Author Share Posted December 24, 2009 Ok I have just done that. There appears to be values there but for some reason it is changing them all to Winter leaves and still not displaying them....strange Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983701 Share on other sites More sharing options...
spenceddd Posted December 24, 2009 Author Share Posted December 24, 2009 Ah, i have just noticed that if i create a new item (form submission to database) from scratch it seems to work ok apart from the fact it keeps loading the Winter Leaves title as the default drop down item. I'm going to remove Winter leaves from my webspace and see if it helps. Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983706 Share on other sites More sharing options...
spenceddd Posted December 24, 2009 Author Share Posted December 24, 2009 Yes it all seems to be working now apart from the default value in the drop down, I think it might be always showing the final file name in the list. Ill try and figure out why. Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983708 Share on other sites More sharing options...
spenceddd Posted December 24, 2009 Author Share Posted December 24, 2009 Got it working now, great news. Thanks for your help again. Im now attempting to re-apply some more code, could anyone explain how this line works please?: <option value="<?php echo $row_PortfolioItems['id']?>"<?php if (!(strcmp($row_PortfolioItems['id'], $_POST['name']))) {echo "SELECTED";} ?>><?php echo $row_PortfolioItems['name']?></option> I get the basic gist but the if function is baffling me, is it using shorthand code? Thanks Spencer Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983755 Share on other sites More sharing options...
Buddski Posted December 24, 2009 Share Posted December 24, 2009 Its not short hand code it just had all its spaces removed.. If you were to indent it, you would get something like this.. <option value="<?php echo $row_PortfolioItems['id']?>" <?php if (!(strcmp($row_PortfolioItems['id'], $_POST['name']))) { echo "SELECTED"; } ?> > <?php echo $row_PortfolioItems['name']?></option> Which looks VERY messy.. I would do something like this.. [code] <?php echo '<option value="'.$row_PortfolioItems['id'].'" '.(!strcmp($row_PortfolioItems['id'], $_POST['name']) ? 'selected="selected"' : '').'>'.$row_PortfolioItems['name'].'</option>'; ?> This is using a ternary operator which is good for basic PHP inline. If you want to read more on it check this out. http://php.net/manual/en/language.operators.comparison.php Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-983834 Share on other sites More sharing options...
spenceddd Posted January 3, 2010 Author Share Posted January 3, 2010 Only just seen this, thanks for the advice Budski. Quote Link to comment https://forums.phpfreaks.com/topic/186244-how-to-display-value-in-drop-down-list-after-form-has-been-reloaded/#findComment-987678 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.