如何获取PHP变量数组值?
在这里,我要求用户输入候选人的名字,从数量的文本框中,他们已经指定了在这一页之前。在我的第二页Ballot.php中,我试图将在这些创建的文本框中输入的内容写入文本文件,但调用的变量始终为空。有什么想法吗?谢谢 $_POST[“raceList”]和$_POST[“canList”]中包含的值输入到值中如何获取PHP变量数组值?,php,html,Php,Html,在这里,我要求用户输入候选人的名字,从数量的文本框中,他们已经指定了在这一页之前。在我的第二页Ballot.php中,我试图将在这些创建的文本框中输入的内容写入文本文件,但调用的变量始终为空。有什么想法吗?谢谢 $_POST[“raceList”]和$_POST[“canList”]中包含的值输入到值中 <form action="CreateBallot.php" method="post"> How many races within ballot? <input type
<form action="CreateBallot.php" method="post">
How many races within ballot?
<input type="number" name="raceList" id="raceList">
<br>
How many candidates in each race?
<input type="number" name="canList" id="canList">
<br>
选票中有多少个种族?
每场比赛有多少候选人?
第1页(CreateBallot.php):
<form action="Ballot.php" method="post">
<?php
for($x = 1; $x <= $_POST["raceList"]; $x++){
echo ("<h1>Race #" . $x . "</h1><br>");
echo ("Please fill in candidates:<br>");
for($y = 1; $y <= $_POST['canList']; $y++){
$textboxes = array("<input type='text' name='txtbox' id='txtbox' value=''>");
echo $textboxes[0];
}
}
?>
<p><a class="btn btn-info" href="Election Commissioner.php" role="button" >Go Back »</a>
<a class="btn btn-info" id="startRace" href="Ballot.php" name="startRace" role="button" onclick="butclik();">Finish Ballot »</a></p>
</form>
<form action="2nd_page.php" method="post">
<?php
for($x = 1; $x <= $_POST["raceList"]; $x++){
echo "<h1>Race #" . $x . "</h1><br>";
echo ("Please fill in candidates:<br>");
for($y = 1; $y <= $_POST['canList']; $y++){
echo "<input type='text' name='candidate[]'>";
}
}
?>
<input type="submit" name="submit" value"Submit Values">
</form>
<?php
$candidates = $_POST['candidate'];
for($i=0;$i<count($candidates);$i++)
{
$race1 = fopen("race1.txt" , "a") or die("Unable to open file!");
$name1 = $candidates[$i];
fwrite($race1,$name1 . "\r\n");
fclose($race1);
}
?>
以下是您应该如何操作的清晰代码:
注意:假设$\u POST[“raceList”]
和$\u POST[“canList”]
包含正整数值,因此下面是代码
第1页:
<form action="Ballot.php" method="post">
<?php
for($x = 1; $x <= $_POST["raceList"]; $x++){
echo ("<h1>Race #" . $x . "</h1><br>");
echo ("Please fill in candidates:<br>");
for($y = 1; $y <= $_POST['canList']; $y++){
$textboxes = array("<input type='text' name='txtbox' id='txtbox' value=''>");
echo $textboxes[0];
}
}
?>
<p><a class="btn btn-info" href="Election Commissioner.php" role="button" >Go Back »</a>
<a class="btn btn-info" id="startRace" href="Ballot.php" name="startRace" role="button" onclick="butclik();">Finish Ballot »</a></p>
</form>
<form action="2nd_page.php" method="post">
<?php
for($x = 1; $x <= $_POST["raceList"]; $x++){
echo "<h1>Race #" . $x . "</h1><br>";
echo ("Please fill in candidates:<br>");
for($y = 1; $y <= $_POST['canList']; $y++){
echo "<input type='text' name='candidate[]'>";
}
}
?>
<input type="submit" name="submit" value"Submit Values">
</form>
<?php
$candidates = $_POST['candidate'];
for($i=0;$i<count($candidates);$i++)
{
$race1 = fopen("race1.txt" , "a") or die("Unable to open file!");
$name1 = $candidates[$i];
fwrite($race1,$name1 . "\r\n");
fclose($race1);
}
?>
注意:您的代码中有很多错误。我也修复了这些错误,它现在正在工作。只需查看代码,观察更改的内容以及下次如何执行即可
最好不要再重复同样的错误了2个字=>你做错了。。!$\u POST$\u POST[“raceList”]
&$\u POST[“canList”]
中包含哪些值?@是打开您的错误报告()@Travis Whitten:你是如何提交表单的,因为我试过,如果没有提交按钮,表单根本就没有提交???@UmairShahYousafzai我添加了按钮来向你展示,我只是为了缩短代码而不使用它们。问题不在我的语法中。我一点错误都没有。太棒了,我知道我在做什么!!非常感谢@特拉维斯惠滕:没问题。。!如果您有任何需要,请毫不犹豫地寻求更多帮助,但请确保您以适当的方式提出问题,并使用适当的代码,这样我们就不需要再次询问人们在发布期间通常会离开的东西了。。!