如何在不干扰电子邮件通知的情况下将选项保存在文本文件中

时间:2015-03-19 16:24:10

标签: javascript php jquery forms email

我正在使用带有表单的自定义optin页面,我想稍微调整一下,

  • 我想保存服务器上txt文件中的所有optins而不用 扰乱表格的其他功能。

  • 我想要的第二件事是在optin表单后显示自定义页面 提交。

目前表单的工作原理如下 用户提交>他/她的页面刷新>我收到了提交数据的电子邮件。

这是表单代码

<div class="form fix ">
                    <p class="form-text">Fill This Out and See Your <br>Timeshare Report</p>
                    <form name="contactform" action="mail-script.php" method="POST">
                        <label for="fname">First Name:
                            <input type="text" name="fname" id="fname" />
                        </label><br>
                        <label for="lname">Last Name:
                            <input type="text" name="lname" id="lname" />
                        </label><br>
                        <label for="email">Email Address:
                            <input type="text" name="email" id="email" />
                        </label><br>
                        <label for="phone">Phone Number:
                            <input type="text" name="phone" id="phone" />
                        </label><br>
                        <label for="phone">Alternate Phone:
                            <input type="text" name="phone" id="aphone" />
                        </label><br>
                        <label for="resort">Resort Name:
                            <input type="text" name="resort" id="resort" />
                        </label><br>
                        <label for="amount">Amount Owed? $:
                            <input type="number" name="amount" id="amount" />
                            <p style="font-size: 12px !important;margin-top: -14px;padding-right: 30px;text-align:right;">
                            If Paid Off Leave Zero, Else Put Amount</p>
                        </label><br>
                        <div class="checkbox">
                            <div class="check-text fix">
                                <p>I'm Considering To</p>
                            </div>
                            <div class="check-one fix">
                                <input type="checkbox" name="call" id="" value="sell"/> Sell It <br>
                                <input type="checkbox" name="call" id="" value="buy"/> Buy It <br>
                                <input type="checkbox" name="call" id="" value="rent "/> Rent  It 
                            </div>
                            <div class="check-two fix">
                                <input type="checkbox" name="call" id="" value="cancel"/> Cancel Mortgage <br>
                                <input type="checkbox" name="call" id="" value="ownership"/> End Ownership <br>
                                <input type="checkbox" name="call" id="" value="give"/> Give It Back
                            </div>
                        </div>

                                                 <p class="captcha">
                            <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
                            <label for='message'>Enter the code above here :</label><br>
                            <input id="6_letters_code" name="6_letters_code" type="text"><br>
                            <small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small>
                        </p>
                        <input id="submit" type="submit" value="" />
                        <p class="submit-text">Ensure all fields are completed and correct, allowing you more benefits, while preventing abuse of our data.</p>
                    </form>
                </div>
            </div>

这是向我发送电子邮件的邮件脚本

<?php
/* Set e-mail recipient */

$myemail  = "**MYEMAIL**";

/* Check all form inputs using check_input function */
$fname    = check_input($_POST['fname'], "Enter your first name");
$lname    = check_input($_POST['lname'], "Enter your last name");
$email    = check_input($_POST['email']);
$phone    = check_input($_POST['phone']);
$resort   = check_input($_POST['resort']);
$amount   = check_input($_POST['amount']);
$call     = check_input($_POST['call']);

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
    $website = '';
}

/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:


First Name : $fname
Last Name : $lname
E-mail: $email
Phone : $phone
Resort: $resort
Amount: $amount
Call  : $call

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: index.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
if (strtolower($_POST['code']) != 'mycode') {die('Wrong access code');}
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>

以下是页面http://timesharesgroup.com/sell/index.html

的在线网址

1 个答案:

答案 0 :(得分:1)

您可以在完成所有验证后将此代码添加到您的脚本中:

$text = "$fname\n$lname\n$email\n$phone\n$resort\n$amount\n$call\n\n";
file_put_contents('file.txt', $text, FILE_APPEND);

或者更好的csv文件:

$fields = array($fname,$lname,$email,$phone,$resort,$amount,$call);

$fp = fopen('file.csv', 'a');
fputcsv($fp, $fields);
fclose($fp);