所以我是一个初学者,我正在开发一个项目来创建动态下拉表单。我写了一些代码,现在想知道我是否可以插入php& html里面有一个多行的php变量。下面这段代码就是我刚试过的[javascript不包含在这里]但是php显示错误。这根本不可能,还是我的错误?提前谢谢。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>php & html inside a multi line php variable</title>
</head>
<body>
<?php
$chooseroom=<<<HERE
<div class="select-boxes">
<?php
//Include database configuration file
include('dbConfig.php');
//Get all occupation data
$query = $db->query("SELECT * FROM occupations WHERE status = 1 ORDER BY occupation_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
?>
<select name="occupation" id="occupation">
<option value="">Select occupation</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['occupation_id'].'">'.$row['occupation_name'].'</option>';
}
}else{
echo '<option value="">occupation not available</option>';
}
?>
</select>
<select name="specification" id="specification">
<option value="">Select occupation first</option>
</select>
<select name="expertise" id="expertise">
<option value="">Select specification first</option>
</select>
</div>
HERE;
echo $chooseroom;
</body>
</html>
好的,这是你的一些请求我的JAVASCRIPT:
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#occupation').on('change',function(){
var occupationID = $(this).val();
if(occupationID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'occupation_id='+occupationID,
success:function(html){
$('#specification').html(html);
$('#expertise').html('<option value="">Select specification first</option>');
}
});
}else{
$('#specification').html('<option value="">Select occupation first</option>');
$('#expertise').html('<option value="">Select specification first</option>');
}
});
$('#specification').on('change',function(){
var specificationID = $(this).val();
if(specificationID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'specification_id='+specificationID,
success:function(html){
$('#expertise').html(html);
}
});
}else{
$('#expertise').html('<option value="">Select specification first</option>');
}
});
});
</script>
答案 0 :(得分:0)
是的,可以将多行html和php添加到变量中。您可以在选项中添加多个条目,如下所示
while($row = $query->fetch_assoc()){
/* here . operator is used to append
to the existing value of $chooseroom */
$chooseroom .= '<option value="'.$row['occupatio_id'].'">'.$row['occpation_name'].'</option>';
}
将此变量打印到
所需的位置<?php echo $chooseroom; ?>
答案 1 :(得分:0)
你不能以你尝试的方式做你正在尝试的事情;您尝试做的是在HEREDOC块中运行PHP代码。
但是,您可以放置PHP代码,并在该块之前创建一组选项,并在需要的地方插入它(您甚至不需要将HEREDOC块分配给一个变量,你可以使用echo out out:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>php & html inside a multi line php variable</title>
</head>
<body>
<?php
//Include database configuration file
include('dbConfig.php');
//Get all occupation data
$query = $db->query("SELECT * FROM occupations WHERE status = 1 ORDER BY occupation_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
// Create an HTML options string
$htmlOptions = "";
if($rowCount > 0) {
while($row = $query->fetch_assoc()){
$htmlOptions .= '<option value="'.$row['occupation_id'].'">'.$row['occupation_name'].'</option>';
}
} else {
$htmlOptions .= '<option value="">occupation not available</option>';
}
// Output
echo <<<HERE
<div class="select-boxes">
<select name="occupation" id="occupation">
<option value="">Select occupation</option>
$htmlOptions
</select>
<select name="specification" id="specification">
<option value="">Select occupation first</option>
</select>
<select name="expertise" id="expertise">
<option value="">Select specification first</option>
</select>
</div>
HERE;
?>
</body>
</html>
虽然,老实说,HEREDOC的整个过程在某种程度上是多余的,因为你可以突破PHP并在你需要的地方回应你的选项 - 这样就可以在PHP和HTML代码之间略微更好地分离:
<?php
//Include database configuration file
include('dbConfig.php');
//Get all occupation data
$query = $db->query("SELECT * FROM occupations WHERE status = 1 ORDER BY occupation_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
// Create an HTML options string
$htmlOptions = "";
if($rowCount > 0) {
while($row = $query->fetch_assoc()){
$htmlOptions .= '<option value="'.$row['occupation_id'].'">'.$row['occupation_name'].'</option>';
}
} else {
$htmlOptions .= '<option value="">occupation not available</option>';
}
// OUTPUT
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>php & html inside a multi line php variable</title>
</head>
<body>
<div class="select-boxes">
<select name="occupation" id="occupation">
<option value="">Select occupation</option>
<?= $htmlOptions; ?>
</select>
<select name="specification" id="specification">
<option value="">Select occupation first</option>
</select>
<select name="expertise" id="expertise">
<option value="">Select specification first</option>
</select>
</div>
</body>
</html>
答案 2 :(得分:0)
我自己总是喜欢这个解决方案:
<?php
include('dbConfig.php');
$query = $db->query("SELECT * FROM occupations WHERE status = 1 ORDER BY occupation_name ASC");
$rowCount = $query->num_rows;
ob_start();
if ( $rowCount > 0 ) {
while( $row = $query -> fetch_assoc() ){
echo '<option value="'.$row['occupation_id'].'">'.$row['occupation_name'].'</option>';
}
} else {
echo '<option value="">occupation not available</option>';
}
$values = ob_get_clean();
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>php & html inside a multi line php variable</title>
</head>
<body>
<div class="select-boxes">
<select name="occupation" id="occupation">
<option value="">Select occupation</option>
<?php echo $values; ?>
</select>
<select name="specification" id="specification">
<option value="">Select occupation first</option>
</select>
<select name="expertise" id="expertise">
<option value="">Select specification first</option>
</select>
</div>
</body>
</html>