我试图创建一个由循环中的输入值组成的数组。我是PHP的新手,并查找了其他几个问题,但无济于事。我正在使用随机数“$ QuestionNoSelect”并从MySQL服务器中选择有关该问题的文本和信息。
//For loop for displaying and naming
for($i = 0; $i < 11; $i++)
{
$QuestionNoSelect = rand(1,16);
array_push($IDListing, $QuestionNoSelect);
$sql = "SELECT QuestionText FROM johnconn_sstest.tbRandomArray WHERE QuestionNo = $QuestionNoSelect";
$QuestionText = $conn->query($sql);
if ($QuestionText->num_rows > 0)
{
// output data of each row
while($row = $QuestionText->fetch_assoc())
{
//Number Question, Increment, Question then input box
echo "<br><br>".$QuestionNumberer. ". ". "<br>";
echo "Question number ID= ".$QuestionNoSelect, "<br>";
$QuestionNumberer++;
echo $row["QuestionText"];
echo '<br>'.'Answer';
echo "<input type='text' name='answerbox[]' id='answerbox[]' class='userInfo' value='".$i."'/>";
echo '<br>'.'comment(if Applicable)';
echo "<input type='text' name='commentbox[]' id='commentbox'[]' class='userInfo' value='".$i."'/>";
}
}
else
{
echo "0 results";
}
}
我试图从我创建的名称或ID数组中获取这些值,但无法弄清楚为什么我无法使其工作。我甚至无法获得要打印的值数组
//PRINT ANSWER
echo '<br>Answer List <br>';
for($i = 0; $i < 11; $i++)
{
$_POST("answerbox[$i]");
}
非常感谢任何帮助。谢谢。
答案 0 :(得分:0)
简短的回答是你将$ _POST视为一个函数($_POST()
)而不是数组($_POST[]
)。因为表单字段是一维数组,所以$ _POST数据是一个二维数组。使用以下语法访问提交的数组元素:
$_POST[formfieldname][numerickey]
我已经重写了部分代码并添加了一些改进,这些改进可以提高效率并展示一些好的做法。
$conn=new mysqli($host, $user, $pass, $dbname);
$conn->select_db('johnconn_sstest'); // designate a default database
$sql="SELECT QuestionNo,QuestionText FROM tbRandomArray ORDER BY RAND() LIMIT 10;"; // until your table gets very, very large RAND() will serve you well
if($result=$conn->query($sql)){ // declare $result and check if true/success or false/syntax failed
$i=0;
echo "<form action=\"receiving_page.php\" method=\"post\">"; // assign receiving page and data delivery method
while($row=$result->fetch_assoc()){ // loop through your db table rows
++$i;
echo "Q$i ID:{$row['QuestionNo']}<br>"; // show counter and question's row id
echo "<input type=\"hidden\" name=\"QNo[$i]\" value=\"{$row['QuestionNo']}\">"; // invisibly pass question id to receiving page
echo "{$row['QuestionText']}<br>";
echo "Answer <input type=\"text\" name=\"answerbox[$i]\" id=\"answerbox$i\" class=\"userInfo\" value=\"\"><br>"; // assign numeric key to each answer input name beginning with 1
echo "Comment(if Applicable) <input type=\"text\" name=\"commentbox[$i]\" id=\"commentbox$i\" class=\"userInfo\" value=\"\"><br><br>"; // assign numeric key to each answer input name beginning with 1
}
echo "<input type=\"submit\" value=\"Submit\">"; // submit button with no name attribute so the value is not passed to the receiving page
echo "</form>";
}else{
echo $conn->error;
}
@ receiving_page.php:
if(isset($_POST["QNo"]) && sizeof($_POST["QNo"])==10 &&
isset($_POST["answerbox"]) && sizeof($_POST["answerbox"])==10 &&
isset($_POST["commentbox"]) && sizeof($_POST["commentbox"])==10){
foreach($_POST["QNo"] as $index=>$value){
// do whatever you like with the values, use $index access other arrays' elements
// $value is each QNo in sequence.
// $_POST["answerbox"][$i] is answerbox value in the same QNo row
// $_POST["commentbox"][$i] is commentbox value in the same QNo row
}
}else{
echo "Did not receive the full batch of expected values";
}
当然,在您的表单上,字段&#39;密钥($i
中的fieldname[$i]
)可以省略如下:fieldname[]
- 这可能更合适,但请注意,然后数字键将从0开始而不是1并自动递增
答案 1 :(得分:-1)
print_r是你的朋友。你可以print_r($ _ POST),它可以让你看看你有没有你认为的。一旦你看到你应该清楚为什么你没有看到你想要的东西。
如果此代码旨在打印答案,则需要类似
echo $_POST["answerbox[{$i}]"];