Jump to content

trying to dynamically create series of checkboxes in a form


simcoweb

Recommended Posts

I'm creating a 3-part search form where the fields are dynamically generated off the database entries for these fields. In the code below it shows i'm running 3 separate queries in order to obtain the required data. The first 2 work fine and create the required form field and values perfectly. The 3rd one creates just the one checkbox and no values. Need a bit of help on this one :)

 

function search_form3()  {
  echo "<form method='POST' action='search.php' onsubmit='return validateIt(this)'>
  <select id='cat' name='category' onchange=\"opt(this.value,'cat','Select A Valid Category!')\">\r";
  echo "<option selected value='ignore'>Select Category</option>";
  $sql = "SELECT category FROM categories";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      while($row = mysql_fetch_assoc($result)) {
        echo "<option value='{$row['category']}' name='{$row['category']}'>{$row['category']}</option>\n\r";
      }
    }
  }
  echo "</select>\n\r";
  
  // loan type form
  echo "<select id=\"cat2\" name='loan_type' onchange=\"opt(this.value,'cat2','Select A Valid Loan Type!')\">\r";
  echo "<option selected value='ignore'>Select Loan Type</option>";
  $sql2 = "SELECT loantype FROM loan_type";
  if ($results2 = mysql_query($sql2)) {
if (mysql_num_rows($results2)) {
	while($row2 = mysql_fetch_assoc($results2)) {
		echo "<option value='{$row2['loantype']}' name='{$row2['loantype']}'>{$row2['loantype']} </option>\n\r";
	}
}
}
  echo "</select>";
  

$sql3 = "SELECT * FROM property_types";
if($results3 = mysql_query($sql3)) {
if(mysql_num_rows($results3)) {
	echo "<p>Select property types<br/>\n";
echo "<table width='500' border='0'>";
	while($row3 = mysql_fetch_array($results3)) {

			echo "<tr><td class='bodytext'>";
			echo "".$row3['property_type']." <input type=\"checkbox\" name=\"{$row3['property_type']}\" value=\"{$row3['property_type']}\">";
			echo "</td></tr>";
		}

		}


	}
	echo "</table>";
	echo "<input type='submit' name='search' value='Search'>\n</form>";
}

The syntax is wrong on this line:

 

echo "".$row3['property_type']." <input type=\"checkbox\" name=\"{$row3['property_type']}\" value=\"{$row3['property_type']}\">";

 

Try

 

echo "$row3['property_type'] <input type=\'checkbox\' name=\'{$row3['property_type']}\' value=\'{$row3['property_type']}\'>";

Using this:

 

echo "$row3['property_type'] <input type='checkbox' name='{$row3['property_type']}' value='{$row3['property_type']}'>";

 

throws and error in my code editor:

 

T_ENCAPSED_AND_WHITESPACE expected T_VARIABLE or T_STRING or T_NUM_STRING

the beginning of the string should be {$row3['property_type']} - any hash key you access inside a string has to be inside {}. You can get around some of the messiness by using heredoc syntax (though the rule about {} I just mentioned still applies) or by breaking out of php like so

 

<?php

while($row3 = mysql_fetch_array($results3)) {
?>

<tr>
    <td class="bodytext">
        <?php echo $row3['property_type'] ?>
        <input type="checkbox" name="<?php echo $row3['property_type'] ?>" value="<?php echo $row3['property_type'] ?>" />
    </td>
</tr>
<?php
} //end while
?>

 

I use a keyboard shortcut for <?php echo

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.