2016-03-03 62 views
-2

中獲取價值與<input type="text" name="' . $r['0'] . '" value="' . $r['0'] . '"如何從輸入的數據與來自我有一個表數據庫

從數據填充,我從數據庫中獲取這樣的:

echo '<form id="actions" name="nonValidMainForm" method="post"><table border="2" width="100%">'; 
    echo "<tr><td><b>Index</b></td><td><b>Email </b></td> <td><b>Date</b></td> <td><b>Name</b></td> <td><b>Surname</b></td> <td><b>Telephone Number</b></td> <td><b>Street Adress</b></td><br/>"; 
    while($r = mysql_fetch_array($result)) { 
     $my[] = $r['0']; 
     echo '<tr>'; 
     echo '<td>'.$roww++.'</td>'; 
     echo '<td> 
       <input size="50%" type="text" name="' . $r['0'] . '" value="'.$r['0'].'"> 
       <input type="submit" name="unsubscribe" value="Unsubscribe"> 
      </td>'; 
     echo '<td>'.$r['1'].'</td>'; 
     echo '<td>'.$r['2'].'</td>'; 
     echo '<td>'.$r['3'].'</td>'; 
     echo '<td>'.$r['4'].'</td>'; 
     echo '<td>'.$r['5'].'</td>'; 
     echo '</tr>'; 
    } 
    echo "<pre>"; 
    print_r($my); 
    echo "</pre>"; 
    if(isset($_POST['unsubscribe'])){ 
     foreach($my as $key=>$value){ 
      $email = $value; 
     } 
     echo "<script>console.log('Value is: " . $email . "');</script>"; 

    } 

    echo '<button style="position:fixed;bottom:5;left:5;">Change</button>'; 
    echo '</table></form>'; 

表看起來是這樣的:

enter image description here

我已經試過這樣:

if(isset($_POST['unsubscribe'])){ 
      $email = $POST['email']; 
      echo "<script>console.log('Value is: " . $email . "');</script>"; 
     } 

但值爲空

所以每次我按下按鈕退訂要刪除相應的電子郵件。這怎麼可能?

+0

你有沒有試過任何代碼? –

+0

是的,我已經嘗試過,但它不起作用 – BRG

+0

你有一個實際的形式?如何將值發佈到服務器? – David

回答

2

您的表單有許多元素與name相同。表單發佈時,瀏覽器如何確定將哪個元素的值發送到服務器?通常最後一個優先,但我懷疑這種行爲可能是未定義的,並且是特定於瀏覽器的。

如果各錶行必須單獨能後的形式,則每行需要自己的形式:

echo '<td> 
     <form method="POST" action="somePage.php"> 
      <input size="50%" type="text" name="email" value="'.$r['0'].'"> 
      <input type="submit" name="unsubscribe" value="Unsubscribe"> 
     </form> 
     </td>'; 

這樣,當瀏覽器的帖子的形式向服務器,它專門知道哪些emailunsubscribe使用的元素。因爲這種形式只有一種。

+0

請參閱我編輯的問題 – BRG

+0

@BRG:編輯確認了此答案。 – David

+0

那麼現在我怎麼才能得到每個輸入的值,當我按下旁邊的按鈕? – BRG

1

您必須將輸入包裝在<form>標記中。基於您的代碼

echo '<form>'; 
while($r = mysql_fetch_array($result)) { 
echo '<tr>'; 
    echo '<td>'.$roww++.'</td>'; 
    echo '<td> 
     <input size="50%" type="text" name="email" value="'.$r['0'].'"> 
     <input type="submit" name="unsubscribe" value="Unsubscribe"> 
    </td>'; 
    echo '<td>'.$r['1'].'</td>'; 
    echo '<td>'.$r['2'].'</td>'; 
    echo '<td>'.$r['3'].'</td>'; 
    echo '<td>'.$r['4'].'</td>'; 
    echo '<td>'.$r['5'].'</td>'; 
    echo '</tr>'; 
} 
echo '</form>'; 
+0

我已經有一個表格 – BRG

1
if(isset($_POST['unsubscribe'])){ 
     $email = $POST['email']; 
     echo "<script>console.log('Value is: " . $email . "');</script>"; 
    } 

上面,它看起來就像是一個語法錯誤。嘗試下面的更新

if(isset($_POST['unsubscribe'])){ 
      $email = $_POST['email']; 
      echo "<script>console.log('Value is: " . $email . "'); </script>"; 
     } 
+0

你改變了什麼? – David

+0

是的,我做了,但我仍然只得到最後的價值。他更改了$ _POST – BRG

相關問題