【发布时间】:2015-08-14 02:49:41
【问题描述】:
我最近发现了一个问题(stackoverflow.com/questions/30556100/how-to-uncheck-all-checkboxes-if-one-checkbox-checked)。现在,我想为 HTML 生成创建一个模板,因为避免代码重复似乎是个好主意。这就是我的 html 的生成方式:
// ---------------------------------------
// --- Populate "Body Type" dropdown list:
// ---------------------------------------
// for populate body types:
$bodytype = array();
// Select exsiting body type from database:
$prep_stmt = " SELECT id, name FROM body_type";
$stmt = $mysqli->prepare($prep_stmt);
if ($stmt) {
// Bind "$userId" to parameter.
//$stmt->bind_param('i', $userId);
// Execute the prepared query.
$stmt->execute();
$stmt->store_result();
// get variables from result.
$stmt->bind_result($id, $name);
// Fetch all the records:
while ($stmt->fetch()) {
// XSS protection as we might print these values
$name = preg_replace("/[^a-zA-Z0-9_\-]+/", "", $name);
$id = filter_var($id,FILTER_SANITIZE_NUMBER_INT);
$type = "<label class='checkbox-inline'>\n";
if ($name == 'Any'){
$type .= "<input type='checkbox' id='bodyTypeAny' name='bodyTypeCheck[]' value='{$id}'> {$name}\n";
} else {
$type .= "<input type='checkbox' id='' name='bodyTypeCheck[]' value='{$id}'> {$name}\n";
}
$type .= "</label>\n";
//Add body types to array
$bodytype[] = $type;
}
} else {
echo 'Database error';
}
// Close the statement:
$stmt->close();
unset($stmt);
复选框组的所有 PHP 代码:http://pastebin.com/NXN6ZzcK
如何实现会生成复选框组的函数,所以我将其简单地称为函数。
希望有人可以帮助我。 谢谢你。
【问题讨论】: