所以我要做的就是把这个调查问卷放在我主页的下半部分,这样人们可以点击与他们所在学校相对应的复选框。一旦他们检查了,我希望他们提交,当它提交时,将它们带回同一页面,但是“回声”你会去....“
出于某种原因,当我在文档中有此代码时,页面甚至不会加载。但是,如果我关闭该部分,页面加载,复选框就在那里,但它没有按照我的意愿行事。我将列出下面的整个页面代码,希望是一个简单的解决方案。我只想在他们选择之后,回应他们的选择。
提前致谢!
<? php
$school = $_POST["school"];
$school_count = count( $school );
print( “You attend $school : “ );
foreach($school as $school) {
print("$school, ");
}
?>
代码
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="../styles/main.css">
<meta charset="utf-8">
<title>My Personal Website</title>
</head>
<body>
<!--Header used in all pages -->
<?php include 'header.php' ?>
<div class="body">
<h2><u>My Personal Website...About ME!</u></h2>
<!-- Picture, nicely aligned -->
<img id="picture" src="../images/richard.jpg" alt="richard">
<!--Summary of what my website is about -->
<p id="summary">Here is a little about myself....</p>
<p>My name is Terrence Horan, I love in Montour Falls, NY, not to far from Ithaca, NY. I attend Cornell University and am majoring in Information Science, and hopefully will get a minor in Computer Science. I love anything that involves technology,however if you would like to read more, dont be shy! Come in a browse my website!</p>
</div>
<!--Questionaire near the bottom of the page -->
<div id="schoolform">
<form action=“myaction.php” method=“post”>
What school are you enrolled in at Cornell?
<input type="checkbox" name="school[]" value= "CE" />College of Engineering
<input type="checkbox" name="school[]" value= "CAS" /> College of Arts and Sciences
<input type="checkbox" name="school[]" value= "CAAP" />College of Architecture, Arts, and Planning.
<input type="checkbox" name="school[]" value= “CALS" />College of Agriculture and Life Sciences
<input type="checkbox" name="school[]" value= "ILR" />School of Industrial and Labor Relations (ILR)
<input type="checkbox" name="school[]" value= "CHE" />College of Human Ecology
<input type="checkbox" name="school[]" value= "Hotel" />School of Hotel Administration
</form>
</div>
<?php var_dump($_POST);?>
<!--Footer.php used in all my pages -->
<?php include 'footer.php' ?>
</div>
</body>
</html>
答案 0 :(得分:1)
2件事;
我宁愿这样写:
<?php
if(isset($_POST['school']) && is_array($_POST['school']) && count($_POST['school']) > 0) {
echo 'You attend the following schools: ', implode(', ', $_POST['school']);
} else {
echo 'no schools selected yet.';
}
?>
答案 1 :(得分:1)
你的php有一些问题:
1)<? php
需要<?php
。
2)您似乎在使用特殊字符在此处声明字符串:
print( “You attend $school : “ );
相反,它应该是:
print( "You attend $school : " );
3)您不能使用与foreach
数组相同的变量,因为重复foreach循环时将提供无效参数。
答案 2 :(得分:0)
对您的代码进行一些小改进可能会对您有所帮助。
// initialize variables
$school = false;
$school_count = false;
// check if you are getting something from $_POST
if ( isset( $_POST["school"] ) !== false ) {
$school = $_POST["school"];
}
// work only if there is something to do
if ( $school !== false ) {
$school_count = count( $school );
print( "You attend ".$school." : " );
foreach( $school as $values ) { // don't use the same variable name
print( $values.", ");
}
}
显然,你仍然应该在每一步都有适当的数据。
答案 3 :(得分:-1)
你的foreach循环中有错误将其更改为foreach(school as somothervariable){echo 'anything';}
目前你正在使用相同的变量学校。