2014-02-12 74 views
-2

我試圖在PHP中進行民意調查。我試圖通過將信息寫入一個txt文件來收集數據。如何獲取將數據寫入txt文件的代碼?如何將投票數據寫入文本文件?

  1. 這是我在我的處理程序中的所有代碼,如何使它寫入我的txt文件。大部分底層的東西並不重要。嘗試查看說明if($ submit =='submit')的代碼以及之後的代碼。

    <!DOCTYPE html> 
    <html> 
    <head> 
        <meta charset="utf-8"/> 
        <title>Poll</title> 
    </head> 
    <body> 
    <?php 
        //no need for sport validation is unimportant and doesnt work 
        if (isset($_REQUEST['Soda'])) { 
         $Soda = $_REQUEST['Soda']; 
        } else { 
         $Soda = NULL; 
         echo '<p class="error">You forgot to select your favorite soda!</p>'; 
        } 
        //This is end of soda validation 
        if (!empty($_REQUEST['Book'])) { 
         $Book = $_REQUEST['Book']; 
        } else { 
         $Book = NULL; 
         echo '<p class="error">You forgot to write in your favorite book!</p>'; 
        } 
        //End of book validation 
        if (isset($_REQUEST['SOTU'])) { 
         $SOTU = $_REQUEST['SOTU']; 
        } else { 
         $SOTU = NULL; 
         echo '<p class="error">You forgot to select the two biggest issues of the     state of the union address!</p>'; 
        } 
        //End of SOTU validation 
        if (isset($_REQUEST['Soda']) && !empty($_REQUEST['Book']) &&        isset($_REQUEST['SOTU'])) { 
         echo' Thank You for filling out the survey!<br> You can see the results of the pole' . "<a href=\"poll_results.php\"> here</a>!<br><br> Your response has been recorded."; 
        } else { 
         echo '<p class="error">Please go ' . "<a href=\"poll_form.html\">back</a>" . ' and fill out the poll!<p>'; 
        } 
        //End of link responses 
        //Define variables and make sure file works 
        $submit = $_REQUEST['submit']; 
        $filename = 'poll_data.txt'; 
        $handle = fopen($filename, 'a'); 
        //next is the stuff that is to be appended 
         if ($submit == 'Submit') { 
          fopen($filename, 'w'); 
          $newdata = $Soda . PHP_EOL; 
          fwrite($handle, $newdata); 
         } else { echo 'You didn\'t click submit';} 
        //Now to sort the data and present it 
        /*explode('PHP.EOL', $filename); 
        $CC = 0; 
        $P = 0; 
        $MD = 0; 
        $SS = 0; 
        $BR = 0; 
        $DLS = 0; 
        $O = 0; 
        foreach($filename as $value) { 
         if ($value = 'Coca-Cola') { 
          $CC = $CC + 1; 
         } 
         elseif ($value = 'Pepsi') { 
          $P = $P + 1; 
         } 
         elseif ($value = 'MtnDew') { 
          $MD = $MD + 1; 
         } 
         elseif ($value ='Sprite/Sierra-Mist') { 
          $SS = $SS + 1; 
         } 
         elseif ('BigRed') { 
          $BR = $BR + 1; 
         } 
         elseif ('DontLikeSoda') { 
          $DLS = $DLS + 1; 
         } 
         elseif ('Other') { 
          $O = $O + 1; 
         } 
        }*/ 
    ?> 
    

回答

0

這應該工作:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"/> 
    <title>Poll</title> 
</head> 
<body> 
<?php 
    //no need for sport validation is unimportant and doesnt work 
    if (isset($_REQUEST['Soda'])) { 
     $Soda = $_REQUEST['Soda']; 
    } else { 
     $Soda = NULL; 
     echo '<p class="error">You forgot to select your favorite soda!</p>'; 
    } 
    //This is end of soda validation 
    if (!empty($_REQUEST['Book'])) { 
     $Book = $_REQUEST['Book']; 
    } else { 
     $Book = NULL; 
     echo '<p class="error">You forgot to write in your favorite book!</p>'; 
    } 
    //End of book validation 
    if (isset($_REQUEST['SOTU'])) { 
     $SOTU = $_REQUEST['SOTU']; 
    } else { 
     $SOTU = NULL; 
     echo '<p class="error">You forgot to select the two biggest issues of the     state of the union address!</p>'; 
    } 
    //End of SOTU validation 
    if (isset($_REQUEST['Soda']) && !empty($_REQUEST['Book']) &&        isset($_REQUEST['SOTU'])) { 
     echo' Thank You for filling out the survey!<br> You can see the results of the pole' . "<a href=\"poll_results.php\"> here</a>!<br><br> Your response has been recorded."; 
    } else { 
     echo '<p class="error">Please go ' . "<a href=\"poll_form.html\">back</a>" . ' and fill out the poll!<p>'; 
    } 
    //End of link responses 
    //Define variables and make sure file works 
    $submit = $_REQUEST['submit']; 
    $filename = 'poll_data.txt'; 
    //next is the stuff that is to be appended 
     if ($submit == 'Submit') { 
      $handle = fopen($filename, 'a'); 
      fputs($handle, $Soda.PHP_EOL); 
      fclose($handle); 
     } else { echo 'You didn\'t click submit';} 
    //Now to sort the data and present it 
    /*explode('PHP.EOL', $filename); 
    $CC = 0; 
    $P = 0; 
    $MD = 0; 
    $SS = 0; 
    $BR = 0; 
    $DLS = 0; 
    $O = 0; 
    foreach($filename as $value) { 
     if ($value = 'Coca-Cola') { 
      $CC = $CC + 1; 
     } 
     elseif ($value = 'Pepsi') { 
      $P = $P + 1; 
     } 
     elseif ($value = 'MtnDew') { 
      $MD = $MD + 1; 
     } 
     elseif ($value ='Sprite/Sierra-Mist') { 
      $SS = $SS + 1; 
     } 
     elseif ('BigRed') { 
      $BR = $BR + 1; 
     } 
     elseif ('DontLikeSoda') { 
      $DLS = $DLS + 1; 
     } 
     elseif ('Other') { 
      $O = $O + 1; 
     } 
    }*/ 
?>