2017-09-23 112 views
0

我的主頁設置如下。它有2個文本框和從文本文件填充的複選框列表(我沒有使用數據庫作爲其簡單的形式)。我的問題是,我不能在複選框博文的值和回聲他們在foreach循環中回顯PHP變量

<!DOCTYPE HTML> 
<html> 
<body> 
    <?php 
     $CN = $Assignee =""; 
    ?> 

<form action="GPParser1.php" method="post"> 
<table> 
    <tr> 
    <td>CName:</td> 
    <td><input type="text" name="CN"></td> 
    </tr> 

    <tr> 
    <td>Assignee:</td> 
    <td><input type="text" name="Assignee" ></td> 
    <tr> 

    <tr> 
    <td>Select Vendors:</td> 
</tr> 
    <td> 
     <?php 

     $gp = file('GP.txt'); 

     foreach ($gp as $line_num => $gp) 
     { 
      //echo '<input type="checkbox" name= $line value=$line>($line)."<br />\n"; 
     print " 
         <br/> 
         <input type='checkbox' name='" . $gp . "' value='" . $gp . "'>$gp"; 
     }; 

     ?> 
    </td> 
    </tr> 

    <tr> 
    <td></td> 
    <td> 
     <input type="submit"> 
    </td> 


<?php 

echo $CasinoName; 
echo $Assignee; 

//foreach ($gp as $ln => $gp) { 
// echo $gp; 
//}; 
?> 
</body> 
<html> 

和:

<!DOCTYPE HTML> 
<html> 
<body> 
    <?php 

    $CN = $_POST["CN"]; 
    $Assignee = $_POST["Assignee"]; 
    $GPS = $_POST ["$gp"]; 

echo $CN; 
echo $Assignee; 
foreach ($GPS as $GPss => $GPS) 
{ 
    echo "$GPS"; 
} 

//foreach ($gp as $ln => $gp) { 
// echo $gp; 
//}; 
?> 
</body> 
<html> 

因爲它是現在我周圍$ GPS =根據得到的幾個誤區$ _ POST [ 「$ GP」];

我該怎麼辦?

+0

您$ GP變量需要聲明只有當你使用它的環路鍵 - >值($ GP)是不是很好的實踐,儘量限制重新命名爲不同的名稱。 – 2017-09-23 16:14:20

+0

目前,你有複選框交替改變'name'值,這意味着沒有辦法PHP可以期望知道什麼'名稱'值進來,這可能是什麼PHP拋出的錯誤。你希望它是這樣嗎?還是你想要向PHP發送一組值? –

回答

0

我不能肯定這是否是一種解決方案,你」重新尋找如果我錯了,請讓我知道,所以我可以給出更好的答案。

我的解決方案是,你的表單將有一個複選框列表,當表單被提交時,它將成爲一個值的數組。

<!DOCTYPE HTML> 
 
<html> 
 

 
<body> 
 
    <?php 
 
     $CN = $Assignee =""; 
 
    ?> 
 

 
    <form action="GPParser1.php" method="post"> 
 
     <table> 
 
     <tr> 
 
      <td>CName:</td> 
 
      <td><input type="text" name="CN"></td> 
 
     </tr> 
 

 
     <tr> 
 
      <td>Assignee:</td> 
 
      <td><input type="text" name="Assignee"></td> 
 
      <tr> 
 

 
      <tr> 
 
       <td>Select Vendors:</td> 
 
      </tr> 
 
      <td> 
 
       <?php 
 

 
     $gp = file('GP.txt'); 
 

 
     foreach ($gp as $line_num => $gp) { ?> 
 
       <p><input type="checkbox" name="gp[]" value="<?php echo $gp; ?>"> 
 
        <?php echo $gp; ?> 
 
       </p> 
 
       <?php }; ?> 
 
      </td> 
 
      </tr> 
 

 
      <tr> 
 
      <td></td> 
 
      <td> 
 
       <input type="submit"> 
 
      </td> 
 
      </tr> 
 
</body> 
 
</html>

<!DOCTYPE HTML> 
 
<html> 
 

 
<body> 
 
    <?php 
 

 
    $CN = $_POST["CN"]; 
 
    $Assignee = $_POST["Assignee"]; 
 
    $GPS = $_POST["gp"]; 
 

 
echo $CN; 
 
echo $Assignee; 
 
foreach ($GPS as $GPss => $GPS) 
 
{ 
 
    echo "$GPS"; 
 
} 
 

 
//foreach ($gp as $ln => $gp) { 
 
// echo $gp; 
 
//}; 
 
?> 
 
</body> 
 
<html>

+1

謝謝,這正是我需要的! –

0

在這兩個腳本,你在foreach覆蓋變量循環

foreach ($gp as $line_num => $gp) 
//      this ^^^ 
//  ^^^ overwrites this 

foreach ($GPS as $GPss => $GPS) 
//     this ^^^ 
//  ^^^ overwrites this 

,因爲你正在使用的as

而是雙方相同的變量名使用另一個像這樣的varibale名稱

foreach ($gp as $line_num => $gpx) 

的一般做法是命名使用pluralised名稱,如$items和不用其他的singluar變量包含數組的數組變量,像

foreach ($gps as $line_num => $gp)