2014-10-21 3 views
1

양식 입력을 서버의 텍스트 파일로 저장해야합니다. 나는 서버 "email.txt"에 텍스트 파일을 만들었고 777의 권한을 주었다. 그러나 양식이 제출되면 텍스트 파일은 비어있게된다. 다음과 같이서버의 파일에 양식 정보 저장

내 HTML은 다음과 같습니다

<form action="process.php" method="post"> 

<input id="email-input" type="text" name="your-email" placeholder="[email protected]" class="cform-text" size="65" title="your email"> 

<input id="optin-button" type="submit" value="Download The Report" class="cform-submit"> 

</form> 

PHP는 다음과 같이

<?PHP 

$email = $_POST["email-input"]; 

$to = "[email protected]"; 
$subject = "New Email Address for Mailing List"; 
$headers = "From: $email\n"; 

$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n 

Email Address: $email"; 

$user = "$email"; 
$usersubject = "Thank You"; 
$userheaders = "From: [email protected]\n"; 

$usermessage = "Thank you for subscribing to our mailing list."; 

mail($to,$subject,$message,$headers); 

mail($user,$usersubject,$usermessage,$userheaders); 

$fh = fopen("email.txt", "a"); 
fwrite($fh, $email); 
fclose($fh); 

header("Location: mysite.com"); 

?> 

은 지원하시기 바랍니다. 감사합니다.

답변

0

idid="email-input"이고 아직 귀하의 POST 변수에 "name="your-email""이 지정되지 않았습니다.

변경 :

$email = $_POST["email-input"]; 

로는 :

$email = $_POST["your-email"]; 

당신은 id에 의존하지만 요소의 name 및 파일이 비어있는 이유입니다 수 없습니다.

오류보고를 사용하면 오류가 발생한 것입니다.

N.B :

는 당신이 그렇지 않으면, 당신은 하나의 연속 선에 대한 모든 축적 된 이메일 주소를 가질거야, fwrite($fh, $email);fwrite($fh, $email . "\n");로 변경하는 것이 좋습니다.


오류를 찾는 데 도움이 될 파일의 ​​상단에 error reporting을 추가하십시오.

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

(!) 참고 : 오류보고는 생산을 결코 준비에서 수행하지 않으며,해야합니다.

0

file_put_contents (파일, 데이터, 모드, 컨텍스트)을 PHP로 사용할 수 있습니다.

출처 : http://www.w3schools.com/php/func_filesystem_file_put_contents.asp

당신이, 당신이 이미 파일에있는 하나의 텍스트를 추가 모드에서 FILE_APPEND를 사용하려면, 그래서 그 결과는 다음과 같습니다

file_put_contents("email.txt",$email,FILE_APPEND);