2017-11-13 1 views
0

이 질문이 전에 제기되었지만 발견 된 해결책이 도움이되지 않았습니다.PHP 폼에서 CSV 파일을 생성하는 방법은 무엇입니까?

현재 사용자가 내 웹 사이트에서 양식을 작성하여 브로셔를 다운로드하면 이메일이 전송됩니다. 나는이 세부 사항이 있고 싶다; 이름, 전화 번호, 이메일 주소 및 도시 상점을 CSV 파일에 저장 한 다음 Mailchimp 또는 다른 사이트에 직접 추가 할 수 있습니다.

PHP :

<?php 
    $name = $_POST['fname']; 
    $email = $_POST['email_from']; 
    $phone = $_POST['phone']; 
    $city = $_POST['city']; 
    $content = $_POST['comments']; 
    $to  = '[email protected]'; 
    $subject = 'Enquiry'; 

    $headers = "From: " . strip_tags($email) . "\r\n"; 
    $headers .= "MIME-Version: 1.0\r\n"; 
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 

    $message = '<html><body>'; 
    $message .= '<table rules="all" cellpadding="10">'; 
    $message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . $name . "</td></tr>"; 
    $message .= "<tr style='background: #eee;' ><td><strong>Email:</strong> </td><td>" . $email . "</td></tr>"; 
    $message .= "<tr style='background: #eee;'><td><strong>Phone:</strong> </td><td>" . $phone . "</td></tr>"; 
    $message .= "<tr style='background: #eee;'><td><strong>City:</strong> </td><td>" . $city . "</td></tr>"; 
    $message .= "<tr style='background: #eee;'><td><strong>Message:</strong> </td><td>" . $content . "</td></tr>"; 
    $message .= "</table>"; 
    $message .= "</body></html>"; 



      //Creates the csv file. 
      header('Content-Type: text/csv; charset=utf-8'); 
      header('Content-Disposition: attachment; filename=downloadcsv.csv'); 

      $dataRow = [ 
      'first_name' => $name, 
      'email' => $email, 
      'phone' => $phone, 
      'city' => $city, 
      'comments' => $comments, 
      ]; 
      $fp = fopen($downloadcsv, 'w'); 
      fputcsv($fp, $dataRow); 
      fclose($fp); 



     if (mail($to, $subject, $message, $headers)) 
     { 
     header('brochure.pdf'); 
     } 
?> 

HTML :

<form name="myForm" action="amail_brochure.php" method="post" onsubmit="return validateForm()" class="sidebar-form" accept-charset="ISO-8859-1"> 
    <div class="main-form"> 
    <div> 
     <input type="text" class="form-control" name="fname" placeholder="Vollständiger Name" required="required"/>   
    </div> 
    <div> 
     <input type="email" class="form-control" name="email_from" id="email_from" placeholder="Email" required="required" /> 
    </div> 
    <div> 
     <input type="text" class="form-control" name="phone" placeholder="Telefon" required="required"/>   
    </div> 
    <div> 
     <input type="text" class="form-control" name="city" placeholder="Deine Stadt" required="required" /> 
    </div> 
    <textarea name="comments" id="" rows="6" class="form-control" placeholder="Comments:"></textarea> 
    <div class="button-container" style="display: inline-block;"> 
     <button class="btn cta-button pull-right form-submit" type="submit" formtarget="_blank">Download</button> 
    </div> 
    </div> 
</form>  

CSV 파일에 사용자 정보를 내보내는 구현하기 위해 어떤 도움을 주시면 감사하겠습니다. 감사와

,

+0

여기서 CSV 파일을 저장 하시겠습니까? – splash58

+1

쉼표로 구분 된 데이터가 포함 된 문자열을 만들어야합니다. 그런 다음 CSV 파일을 만들고 문자열을 쓸 수 있습니다. – sobbe

답변

0

귀하의 질문에 쉽게 CSV 파일에 배열 데이터 구조를 변환 할 수 대답합니다. Export to CSV via PHP

이 경우 POST 배열을 사용할 수 있습니다 (코드의 유해한 주입을 피하려면 먼저 몇 가지 검사를해야 함).

그러나 mailchimp에는 이러한 세부 정보를 직접 추가 할 수있는 API가 있습니다. 그 사이에 수동 단계가 필요하지 않은 경우이 API를 사용하여 사용자를 메일 링리스트에 직접 추가 할 수 있습니다. http://developer.mailchimp.com/documentation/mailchimp/guides/manage-subscribers-with-the-mailchimp-api/

0

데이터를 배열에 넣고 fputcsv를 사용하십시오.

$name=$_POST['fname']; 
$email=$_POST['email_from']; 
$phone=$_POST['phone']; 
$city=$_POST['city']; 
$content=$_POST['comments']; 
$dataRow = [ 
    'first_name' => $name, 
    'email' => $email, 
    'phone' => $phone, 
    'city' => $city, 
    'comments' => $comments, 
]; 
// You need to decide where you want the CSV file stored and since you are receiving lots of them you might want to use a timestamp in the filename. If you want the CSV file as part of the email you will need to attach it to the mail() message. 
$csvFile = 'some/file/path/csvfile.csv'; 
$fp = fopen($csvFile, 'w'); 
fputcsv($fp, $dataRow); 
fclose($fp); 
+0

이것을 PHP 코드에 구현하려고하면 제출 버튼이 새 탭을 열지 만 브로셔가 나타나지 않습니다. –

+0

업데이트 된 코드를 보지 않고 무엇이 될지 말하기가 어렵습니다. – davidethell

+0

원본 게시물을 코드로 업데이트했습니다. 'if'명령 전에 PHP 코드를 입력했습니다. –

관련 문제