2014-02-14 86 views
0

我想弄清楚如何發送檢查選項從一系列複選框到一個文本文件,以跟蹤已選擇的內容。我現在最多的是這個。提交從PHP表格檢查選項

<html> 
<?php 
//Get contents from zmzonSongs.txt file, put into array. 
$songList = explode("\n", file_get_contents('zmzonSongs.txt')); 

print "Welcome to Zmzon. Select songs below to add to your library."; 
//Print contents as checklist. 
foreach($songList as $songs){ 
    echo "<br/><input type='checkbox' name='songList[]' value='$songs' />"$songs<br>"; 
} 

?> 

</html> 

我需要的用戶點擊提交按鈕後,發送已檢查像myLibrary.txt文件一切的價值觀,但每次我嘗試添加使用echo命令提交按鈕,我的整個頁面停止工作並出現空白。我完全失去了。

+1

打開錯誤報告 –

+0

我不知道該怎麼做。我對PHP幾乎一無所知,自從我開始這個任務以來,我幾乎沒有任何意義。 – user3308219

+0

@ user3308219另一種方法是檢查error_log或查看您的頁面的源代碼。 –

回答

0

你有一個額外的報價:

echo "<br/><input type='checkbox' name='songList[]' value='$songs' />"$songs<br>"; 

應該

echo "<br/><input type='checkbox' name='songList[]' value='$songs' />$songs<br>"; 

echo "<br/><input type='checkbox' name='songList[]' value='$songs' />".$songs."<br>"; 
+0

我解決了這個問題,但這不是我想要做的。我需要將所有已被選中的歌曲寫入文件,如「藝術家 - 曲目」。 – user3308219

+1

這是一個完全的其他問題,我相信你可以找到使用Google的教程。 –

1

你的複選框和提交按鈕應該是一個形式裏面,你的提交按鈕是HTML,而不是PHP。

<?php 
    /* Lets set the destination to the same file as the form. (see form action) 
    * That means, if someone has submitted the form, there will be data inside 
    * $_POST right now! 
    */ 
    // var_dump($_POST); <-- Can be used to quickly peek inside $_POST 

    if (!empty($_POST)) // Check if there is any data 
    { 
     save_data_to_file($_POST); // pseudo-code (should be replaced by you) 
     echo "success!"; // A success message perhaps? 
     exit(); // Stop here to not display the form below. 
    } 

    // If we come this far, it means the form has not been submitted. 
    // So lets display the form! 

    // You can have this line first in the file, or later, 
    // as long as you do this before you try to use it in your checkboxes. 
    $songList = explode("\n", file_get_contents('zmzonSongs.txt')); 
?> 

<h1>Welcome to Zmzon. Select songs below to add to your library</h1> 
<form action="thisfile.php" method="POST"> 
    <?php 
     // See, you can jump between PHP and HTML whenever you want. 
     foreach($songList as $songs){ 
      echo "<input type='checkbox' ... ><br>"; 
     } 
    ?> 
    <input type="submit"> 
</form> 

您可以將數據發送到另一個頁面或同一頁面,由「action」決定。 數據將位於變量$ _POST或$ _GET中(取決於您選擇的發送方法)。

查看這些全局變量的簡單方法是使用var_dump()。例如:

<pre> 
<?php var_dump($_POST); ?> 

再次更新

+0

我不明白我應該將文本文件添加到數組中的位置。它應該在你創建的PHP塊還是外部?最重要的是,我不明白我是否正確使用東西。我需要使用回聲嗎?運行我現在的版本給了我複選框以及每當我運行它時添加的歌曲的名稱,但我不知道它是否執行了任何操作。 – user3308219

+0

您可以將它添加到幾乎任何地方,只要它位於PHP標籤內部並且在使用之前。 – ippi

+0

感謝您的協助。這是迄今爲止我收到的最有用的建議。 – user3308219

0
<html> 
<form method='post'> 
<?php 
//Get contents from zmzonSongs.txt file, put into array. 
if (!isset ($_POST['songList'])) 
{ 
    $songList = explode("\n", file_get_contents('zmzonSongs.txt')); 
    print "Welcome to Zmzon. Select songs below to add to your library."; 
    //Print contents as checklist. 
    foreach($songList as $songs){ 
     echo "<br/><input type='checkbox' name='songList[]' value='$songs' />"$songs<br>"; 
    } 
} 
else 
{ 
$songlist = $_POST['songList']; // which is array 
// next you write code to update in text file. 

} 

?> 
<input type="Submit" value="Submit"> 

</form> 
</html>