Jump to content
Larry Ullman's Book Forums

Sticky Foms & Html Select List Generated By Php


Recommended Posts

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

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

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

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

  • Upvote 2
Link to comment
Share on other sites

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.

  • Upvote 1
Link to comment
Share on other sites

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

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

 Share

×
×
  • Create New...