如何获取PHP变量数组值?
在这里我要求用户输入他们在此页面之前指定的文本框数量的候选人名称。在我的第二页Ballot.php我想写在这些创建的文本框中输入的内容到一个文本文件,但被调用的变量总是空的。有任何想法吗?谢谢如何获取PHP变量数组值?
包含在$ _POST [“raceList”]和$ _POST [“canList”]中的值被输入到值中。
<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>
第二页(Ballot.php)
Candidates:<br>
<?php
$race1 = fopen("race1.txt" , "a") or die("Unable to open file!");
$name1 = $_POST[$textboxes[0]];
fwrite($race1,$name1 . "\r\n");
fclose($race1);
?>
下面是你应该怎么做干净的代码:
注意:假设$_POST["raceList"]
& $_POST["canList"]
包含正整数值,所以这里是代码..!
第一页:
<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>
第2页(2nd_page.php):
<?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);
}
?>
注:有很多在你code..I错误固定的错误也是,现在正在工作。只要看看代码,观察下一次发生了什么变化以及如何去做。
很高兴再次重复相同的错误..!
太棒了,我知道我在做什么!非常感谢! –
@TravisWhitten:没问题..!如果你需要的话,不要犹豫,如果你需要更多的帮助,但是要确保你以正确的方式提出问题并提供正确的代码,这样我们就不需要再次询问人们在发布时间内最常留下的事情..! –
2 words => http://php.net/manual/en/function.error-reporting.php –
你正在做的一切都错了! '$ _POST $ _POST [“raceList”]''''_POST ['canList']'中包含的值是什么? –
@是打开你的'error_reporting()',好像你应该会得到很多错误,并且你没有报告..! –