2012-08-10 186 views
0

我正在爲即將到來的網頁做準備,它包括名稱和電子郵件的文本框我正在處理asp,但現在我需要php或html5編碼,因爲我完成了我的項目。如何在沒有數據庫的情況下收集數據

我想插入一個CSV文件中的數據,併發送一個自動生成的郵件在用戶的電子郵件地址與PHP是可能的,如何能夠幫助我這個人。 有兩個文本框名稱和電子郵件。 你可以給我的代碼,因爲我是新的PHP?

我在谷歌找到的代碼:

if($_POST['formSubmit'] == "Submit") 
{ 
    $errorMessage = ""; 
    if(empty($_POST['name'])) 
    { 
     $errorMessage .= "<li>without name how can we send you invitation</li>"; 
    } 
    if(empty($_POST['email'])) 
    { 
     $errorMessage .= "<li>i think email id is required</li>"; 
    } 

    $name = $_POST['name']; 

    $email = $_POST['email']; 
     if(empty($errorMessage)) 
    { 
     $fs = fopen("mydata.csv","a"); 
     fwrite($fs,$name . ", " . $email . "\n"); 
     fclose($fs); 

     header("Location: werememberyou.html"); 
     exit; 

回答

2

,我們在您當前密碼的兩個問題

  1. 電子郵件至特定條目。

    您可以使用php的Direct Mail功能。或者您可以使用PHP Mailer(對於本地主機測試最好)

  2. 將條目添加到CSV文件。

    在這裏你可以在將來有一個問題來添加新的條目到你當前的文件。所以我添加了一些你可以使用的代碼。

    if($_POST['formSubmit'] == "Submit") 
    { 
        $errorMessage = ""; 
        if(empty($_POST['name'])) 
        { 
         $errorMessage .= "<li>without name how can we send you invitation</li>"; 
        } 
        if(empty($_POST['email'])) 
        { 
         $errorMessage .= "<li>i think email id is required</li>"; 
        } 
    
        $name = $_POST['name']; 
        $email = $_POST['email']; 
    
        function array_insert($array, $pos, $val) 
        { 
         $array2 = array_splice($array, $pos); 
         $array[] = $val; 
         $array = array_merge($array, $array2); 
         return $array; 
        } 
    
        if(empty($errorMessage)) 
        { 
    
         $DataToInsert = $name.','.$email; 
         $PositionToInsert = 1; 
    
         //Full path & Name of the CSV File 
         $FileName = 'mydata.csv'; 
    
         //Read the file and get is as a array of lines. 
         $arrLines = file($FileName); 
    
         //Insert data into this array. 
         $Result = array_insert($arrLines, $PositionToInsert, $DataToInsert); 
    
         //Convert result array to string. 
         $ResultStr = implode("\n", $Result); 
    
         //Write to the file. 
         file_put_contents($FileName, $ResultStr); 
    
         $to  = $email; 
         $subject = 'Thank You'; 
         $message = 'We will soon contact you'; 
         $headers = 'From: [email protected]' . "\r\n" . 
            'Reply-To: [email protected]' . "\r\n" . 
            'X-Mailer: PHP/' . phpversion(); 
    
         mail($to, $subject, $message, $headers); 
    
    
         header("Location: werememberyou.html"); 
         exit; 
        } 
    
+0

有@ css3schools.comSahilPopli是2的變化在此代碼 – 2012-08-10 06:14:42

+0

任何改變。我已經發布了它。 – Vinay 2012-08-10 06:59:15

+0

我正在嘗試,如果給我的結果我會接受別擔心兄弟 – 2012-08-10 07:10:48

1

你應該閱讀有關fputcsv,此功能會爲CSV文件結構準備正確的數據。只需創建一個包含收集數據的數組並將其作爲參數傳遞。我看到你已經用「a」參數打開了你的文件,所以你很好。

如果您的服務器配置正確,發送郵件很容易,請使用mail功能。

你會在php.net上找到你所需要的一切(有例子),我強烈建議它作爲第一個信息來源。

相關問題