2016-04-26 184 views
-2

在這裏我要求用戶輸入他們在此頁面之前指定的文本框數量的候選人名稱。在我的第二頁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 &raquo;</a> 
<a class="btn btn-info" id="startRace" href="Ballot.php" name="startRace" role="button" onclick="butclik();">Finish Ballot &raquo;</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); 

     ?> 
+0

2 words => http://php.net/manual/en/function.error-reporting.php –

+0

你正在做的一切都錯了! '$ _POST $ _POST [「raceList」]''''_POST ['canList']'中包含的值是什麼? –

+0

@是打開你的'error_reporting()',好像你應該會得到很多錯誤,並且你沒有報告..! –

回答

0

下面是你應該怎麼做乾淨的代碼:

注意:假設$_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錯誤固定的錯誤也是,現在正在工作。只要看看代碼,觀察下一次發生了什麼變化以及如何去做。

很高興再次重複相同的錯誤..!

+0

太棒了,我知道我在做什麼!非常感謝! –

+0

@TravisWhitten:沒問題..!如果你需要的話,不要猶豫,如果你需要更多的幫助,但是要確保你以正確的方式提出問題並提供正確的代碼,這樣我們就不需要再次詢問人們在發佈時間內最常留下的事情..! –