lutinFou Posted January 11, 2012 Share Posted January 11, 2012 Hi, first of all....this is a great book! Ok now to my question. I have created a HTML form in which there is a selection box generated by a php foreach loop: <?php //Array containing option values $options=array('Architecture','Interior Design','Dimensional Surveys','Party Wall Surveyors','In-house visualizations','CDM'); //Foreach loop to insert values into HTML echo'<select class="formInputText"name="selService[]"id="selService"multiple="multiple">'; foreach($options as $service){ echo"<option value=\"$service\">$service</option>\n"; } //close HTML select tag echo'</select>'; ?> If I were to make a sticky form using a conventionally generated HTML select box (i.e. typr it all out myself rather than let php do it, I would do something like the following: <option value="Architect"><?php if(isset($_POST['Architect'])echo 'selected="selected"?>Architect</option> In the form I have created with php, I was thinking that the following would work: echo"<option value=\"$service\">if(isset($_POST[$service]))echo\'selected\'="\selected"\</option>\n"; but it doesn't. I have tried several variations of this (i.e. with single and double quotes, etc...) but to no avail. Would you be able to point me in the right direction? Thanks Jonathan ?> Link to comment Share on other sites More sharing options...
Jonathon Posted January 11, 2012 Share Posted January 11, 2012 What source code does it generate? 1 Link to comment Share on other sites More sharing options...
lutinFou Posted January 12, 2012 Author Share Posted January 12, 2012 Hi, by using the code: //Array containing option values $options=array('Architecture','Interior Design','Dimensional Surveys','Party Wall Surveyors','In-house visualizations','CDM'); //Foreach loop to insert values into HTML echo'<select class="formInputText"name="selService[]"id="selService"multiple="multiple">'; foreach($options as $service){ echo"<option value=\"$service\" if(isset($_POST[$service])&&($_POST[$service]==$service))echo 'selected=\"Selected\"';>$service</option>\n"; } echo'</select>'; ?> The HTML gernerated reads: <select class="formInputText"name="selService[]"id="selService"multiple="multiple"><br /> <b>Notice</b>: Undefined index: Architecture in <b>C:\xampp\htdocs\HoltArchitects\pages\contact_holt_architects.php</b> on line <b>67</b><br /> <br /> <b>Notice</b>: Undefined index: Architecture in <b>C:\xampp\htdocs\HoltArchitects\pages\contact_holt_architects.php</b> on line <b>67</b><br /> <option value="Architecture" if(isset()&&(==Architecture))echo 'selected="Selected"';>Architecture</option> <br /> <b>Notice</b>: Undefined index: Interior Design in <b>C:\xampp\htdocs\HoltArchitects\pages\contact_holt_architects.php</b> on line <b>67</b><br /> <br /> <b>Notice</b>: Undefined index: Interior Design in <b>C:\xampp\htdocs\HoltArchitects\pages\contact_holt_architects.php</b> on line <b>67</b><br /> <option value="Interior Design" if(isset()&&(==Interior Design))echo 'selected="Selected"';>Interior Design</option> <br /> <b>Notice</b>: Undefined index: Dimensional Surveys in <b>C:\xampp\htdocs\HoltArchitects\pages\contact_holt_architects.php</b> on line <b>67</b><br /> <br /> <b>Notice</b>: Undefined index: Dimensional Surveys in <b>C:\xampp\htdocs\HoltArchitects\pages\contact_holt_architects.php</b> on line <b>67</b><br /> <option value="Dimensional Surveys" if(isset()&&(==Dimensional Surveys))echo 'selected="Selected"';>Dimensional Surveys</option> <br /> <b>Notice</b>: Undefined index: Party Wall Surveyors in <b>C:\xampp\htdocs\HoltArchitects\pages\contact_holt_architects.php</b> on line <b>67</b><br /> <br /> <b>Notice</b>: Undefined index: Party Wall Surveyors in <b>C:\xampp\htdocs\HoltArchitects\pages\contact_holt_architects.php</b> on line <b>67</b><br /> <option value="Party Wall Surveyors" if(isset()&&(==Party Wall Surveyors))echo 'selected="Selected"';>Party Wall Surveyors</option> <br /> <b>Notice</b>: Undefined index: In-house visualizations in <b>C:\xampp\htdocs\HoltArchitects\pages\contact_holt_architects.php</b> on line <b>67</b><br /> <br /> <b>Notice</b>: Undefined index: In-house visualizations in <b>C:\xampp\htdocs\HoltArchitects\pages\contact_holt_architects.php</b> on line <b>67</b><br /> <option value="In-house visualizations" if(isset()&&(==In-house visualizations))echo 'selected="Selected"';>In-house visualizations</option> <br /> <b>Notice</b>: Undefined index: CDM in <b>C:\xampp\htdocs\HoltArchitects\pages\contact_holt_architects.php</b> on line <b>67</b><br /> <br /> <b>Notice</b>: Undefined index: CDM in <b>C:\xampp\htdocs\HoltArchitects\pages\contact_holt_architects.php</b> on line <b>67</b><br /> <option value="CDM" if(isset()&&(==CDM))echo 'selected="Selected"';>CDM</option> </select> The php I am using to validate the select box is: //Select box validation if(isset($_POST['selService'])){ $mySelection=count($_POST['selService']); echo'<p>'; for($i=0;$i<$mySelection;$i++){ echo $_POST['selService'][$i]."<br /> "; } echo"</p>"; } else{ $_POST['selService']=NULL; echo'<p>Please select a service</p>'; } thanks, Jonathan Link to comment Share on other sites More sharing options...
Jonathon Posted January 12, 2012 Share Posted January 12, 2012 Here: if(isset($_POST[$service])&&($_POST[$service]==$service)) Your checking for a $_POST variable that doesn't exist, you have named the select element so you need to check for the presence of the named element against the service. 1 Link to comment Share on other sites More sharing options...
Paul Swanson Posted January 12, 2012 Share Posted January 12, 2012 Judging from your output, I think you want: foreach ($options as $service) { echo "<option value=\"$service\"" . (isset ($_POST['selService']) && in_array ($service, $_POST['selService']) ? ' selected="selected" : '') . ">$service</option>\n"; } 2 Link to comment Share on other sites More sharing options...
lutinFou Posted January 12, 2012 Author Share Posted January 12, 2012 Hi Jonathon, thanks for your help. I understand what you mean, I just seem to be having troble implementing it. I have changed the script somewhat to: echo '<select class="formInputText"name="selService[]"id="selService"multiple="multiple">'; $clientOption=$_POST['selService']; foreach($options as $service){ echo"<option value=\"$service\" if(isset($clientOption)&&($clientOption==$service))echo'selected=\"Selected\"';>$service</option>\n"; } however the line if(isset($clientOption)&&($clientOption==$service))echo'selected=\"Selected\"';> is treated as HTML and not PHP on the generated HTML, ie: <select class="formInputText"name="selService[]"id="selService"multiple="multiple"><option value="Architecture" if(isset()&&(==Architecture))echo'selected="Selected"';>Architecture</option> <option value="Interior Design" if(isset()&&(==Interior Design))echo'selected="Selected"';>Interior Design</option> <option value="Dimensional Surveys" if(isset()&&(==Dimensional Surveys))echo'selected="Selected"';>Dimensional Surveys</option> <option value="Party Wall Surveyors" if(isset()&&(==Party Wall Surveyors))echo'selected="Selected"';>Party Wall Surveyors</option> <option value="In-house visualizations" if(isset()&&(==In-house visualizations))echo'selected="Selected"';>In-house visualizations</option> <option value="CDM" if(isset()&&(==CDM))echo'selected="Selected"';>CDM</option> </select> any ideas where i am going wrong? thanks Jonathan Link to comment Share on other sites More sharing options...
Antonio Conte Posted January 13, 2012 Share Posted January 13, 2012 You cannot use if/else inside an echo. Paul's code might work, but here is the way I tend to do this. foreach ($options as $service) { if(isset($_POST['selService']) && $_POST['selService'] == $service) { echo '<option value="'.$service.'" selected="selected">'.$service.'</option>\n'; } else { echo '<option value="'.$service.'">'.$service.'</option>\n'; } } Hope it works. Btw: Use the code formating tags when posting code... 2 Link to comment Share on other sites More sharing options...
lutinFou Posted January 13, 2012 Author Share Posted January 13, 2012 Hi, thanks for all of your help.I would never have figured out those bits of code myself. For some reason, the form is still not sticky. Guess I can live with out it for now ) thanks again! Jonathan Link to comment Share on other sites More sharing options...
Paul Swanson Posted January 13, 2012 Share Posted January 13, 2012 I think because your <select> element is multiple, $_POST['selService'] will be an array, and you'll have to use in_array rather than ==, so whether you use my code or Antonio's, try change your conditional to: isset ($_POST['selService']) && in_array ($service, $_POST['selService']) ...and see if that doesn't make it sticky for you. 1 Link to comment Share on other sites More sharing options...
Antonio Conte Posted January 13, 2012 Share Posted January 13, 2012 Did not notice that, Paul. I didn't know select had a multiple tag. You learn something everyday, huh? Your code also makes sense now. Good solution. Is there any reason for using isset(), though? in_array() will do that job on it's own, or am I miss something here? Just curiosity. ----------- To TS: How about checkboxes? It just makes more sense if you first give the user an opportunity to select several values. This is done exactly the same way as with multiple option tags: <input type="checkbox" name="checkbox[]" value="a"> <input type="checkbox" name="checkbox[]" value="b"> <input type="checkbox" name="checkbox[]" value="c"> <input type="checkbox" name="checkbox[]" value="d"> Link to comment Share on other sites More sharing options...
Paul Swanson Posted January 14, 2012 Share Posted January 14, 2012 Using isset() might be overkill, since any <select> element should be present in $_POST. It's just a habit from validating text inputs, which won't be present in $_POST if no value has been entered, and will give the undefined index error we all know so well. Link to comment Share on other sites More sharing options...
Josee Posted January 14, 2012 Share Posted January 14, 2012 By the way, this: echo"<option value=\"$service\" if(isset($clientOption)&&($clientOption==$service))echo'selected=\"Selected\"';>$service</option>\n"; couldn't work since you had twice echo. Link to comment Share on other sites More sharing options...
lutinFou Posted January 14, 2012 Author Share Posted January 14, 2012 Hi all, many many thanks to all of you for your help. the "in_array" suggestion form Paul does the trick. In fact after re-reading the section on arrays last night, I realized that the $_POST['selService'] was an array value. I am just was not advanced enough in PHP to know how to to call (I do this all the time - get too far ahead of myself, although I guess that is the way to learn!) Now just got to wait for all the hair I have pulled out over this little problem to grow back hehehehe! Thanks again to you all Jonathan Link to comment Share on other sites More sharing options...
Recommended Posts