0

我有一个 php 邮件表单。如果电子邮件地址未通过验证,我的 jquery 会使用成功消息更新 emailform div。如果表单成功提交(发布),我只希望出现成功消息。

jQuery:

  $('#submit').submit(function() { // catch the form's submit event
      $.ajax({ // create an AJAX call...
          data: $(this).serialize(), // get the form data
          type: $(this).attr('method'), // GET or POST
          url: $(this).attr('action'), // the file to call
          success: function(response) { // on success..
            console.log(response);
              if(response != 'error; you need to submit the form!'){
              $('#emailform').html("<h2 style='text-align:center;'>Thank you!</h2><hr><p style='text-align:center;'>Thank you for submitting your purchase information.<br>We will send your free gifts soon!</p>"); // update the DIV
            }
          }
      });
      return false; // cancel original event to prevent form submitting
  });

PHP:

<?php
ini_set('display_errors',1);
 error_reporting(E_ALL);

if(!isset($_POST['submit']))
{
  //This page should not be accessed directly. Need to submit the form.
  echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$email = $_POST['email'];
$purchasecode = $_POST['purchasecode'];
$vendor = $_POST['vendor'];


//Validate first
if(empty($_POST['name'])  ||
   empty($_POST['email']) ||
   empty($_POST['purchasecode']) ||
   empty($_POST['vendor']))
{
    echo "All fields are required.";
exit;
}

if(IsInjected($email))
{
    echo "Bad email value!";
    exit();
}

$email_from = $email;
$email_subject = "GDFY Purchase Confirmation";
$email_body = "New purchase confirmation from $name.\n".
    "Here are the details:\n\n Name: $name \n\n Email: $email \n\n Purchase Code: $purchasecode \n\n Vendor: $vendor";

$to = "idc615@gmail.com";//<== update the email address

$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email_from \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: index.html');

// echo "success";


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?>
4

1 回答 1

0

好的,我有一些对你有用的东西,但你需要先下载两个文件,这是我从中获得 JS/demo/tutorial 的地方。

来源:http ://webdesign.torn.be/tutorials/javascript/prototype/forms-with-prototype/

1) http://webdesign.torn.be/tutorials/assets/images/loading_indicator.gif

2) http://webdesign.torn.be/tutorials/assets/js/prototype.js

加上这个:

<button type="submit" name="submit"  id="submitButton" value="Submit" class="mainButton">SUBMIT</button>

应该改用这个:

<input type="submit" name="submit"  id="submitButton" value="Submit" class="mainButton">

jQuery冲突

如果您必须使用 jQuery,请尝试以下操作:

<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script>
jQuery.noConflict();

或者

<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script>
// Give $ back to prototype.js; create new alias to jQuery.
var $jq = jQuery.noConflict();
</script>

HTML 表单(已测试)

<!DOCTYPE html>

<head>

<script type="text/javascript" src="prototype.js"></script>

<script type="text/javascript">
// <![CDATA[
document.observe('dom:loaded', function() {

    function sendForm(event){
        // we stop the default submit behaviour
        Event.stop(event);
        var oOptions = {
            method: "POST",
            parameters: Form.serialize("contactForm"),
            asynchronous: true,
            onFailure: function (oXHR) {
                $('feedback').update(oXHR.statusText);
            },
            onLoading: function (oXHR) {
                $('feedback').update('Sending data ... <img src="loading_indicator.gif" title="Loading..." alt="Loading..." border="0" />');
            },                          
            onSuccess: function(oXHR) {
               $('feedback').update(oXHR.responseText);
            }               
        };
        var oRequest = new Ajax.Updater({success: oOptions.onSuccess.bindAsEventListener(oOptions)}, "mail_form.php", oOptions);             
    }
    Event.observe('submitButton', 'click', sendForm, false);

}); 
// ]]>
</script>

</head>

<body>
<div id="feedback"> </div>
<div id="contact">

            <h2>Confirm your purchase information</h2>
            <hr>
            <form method="post" id="contactForm" name="contactform" action="">
            <p>
            <label for='name'>Your Name:</label> <br>
            <input type="text" name="name">
            </p>
            <p>
            <label for='email'>Email Address:</label> <br>
            <input type="text" name="email">
            </p>
            <p>
            <label for='purchasecode'>Purchase Code:</label> <br>
            <input type="text" name="purchasecode">
            </p>
            <p>
            <label for='vendor'>Vendor Name:</label> <br>
            <select name="vendor">
              <option value="1" selected="selected"></option>
              <option value="2" >Amazon</option>
              <option value="3" >Barnes &amp; Noble</option>
              <option value="4" >Family Christian</option>
              <option value="5" >Christianbook.com</option>
              <option value="6" >LifeWay</option>
              <option value="7" >Books-A-Million</option>
              <option value="8" >Mardel</option>
            </select>
            </p>

<input type="submit" name="submit"  id="submitButton" value="Submit" class="mainButton"><br>

</form>
</div>

</body>
</html>

PHP 处理程序 (mail_form.php) 已编辑

注意:更改$to = "email@example.com";为您的电子邮件地址

<?php
ini_set('display_errors',1);
 error_reporting(E_ALL);

if(!isset($_POST['submit']))
{
  //This page should not be accessed directly. Need to submit the form.
  echo "Error, you need to submit the form!";
}

$name = $_POST['name'];
$email = $_POST['email'];
$purchasecode = $_POST['purchasecode'];
$vendor = $_POST['vendor'];

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

die("The email <b>$email</b> you entered, is not valid.");
exit;
}

//Validate first
if(empty($_POST['name'])  ||
   empty($_POST['email']) ||
   empty($_POST['purchasecode']) ||
   empty($_POST['vendor']))
{
    echo "All fields are required.";
exit;
}

if(IsInjected($email))
{

    echo "Bad email value!";
    exit();
}

$email_from = $email;
$email_subject = "GDFY Purchase Confirmation";
$email_body = "New purchase confirmation from $name.\n".
    "Here are the details:\n\n Name: $name \n\n Email: $email \n\n Purchase Code: $purchasecode \n\n Vendor: $vendor";

$to = "email@example.com"; //<== update the email address

$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email_from \r\n";
//Send the email!

mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
// header('Location: index.html');

echo "Success";

// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?>
于 2013-09-04T18:07:20.057 回答