dongliehuan3925
dongliehuan3925
2014-04-01 04:04

使用ajax发送html数据和表单并保留在当前页面上

I'm trying to send an email the contains html table data as well as the input from the user. This is what the email is displaying at the moment.

Device - iPad $14000

Your App Will Cost Approximately : $14000

FromEmail

The input from user is not being displayed for some odd reason, I've changed the function to .click & .form but this sends only the input from the user. I really want both of these things to send.

here is the html

<div class="total-inner">
    <table class="total-table" id="table">
        <tbody>
            <tr>
                <td>Device - iPad</td>
                <td>$14000</td>
            </tr>
        </tbody>    
    </table>

    <hr><br>

    <div class="total-text-wrap">
        <p>Your App Will Cost Approximately  :  $<span class="total">0</span></p>   
    </div>

    <br><hr>
</div>

<div class="form-wrap">
    <form method="post" class="quote-form" action="server.php">
        <span>Company:<input type="text" name="name"></span>
        <span>Email:<input type="text" name="email"></span>

        <br>

        <button class="total-button" id="send-quote" type="submit">SEND</button>
    </form>
 </div>

 <div class="button-wrap">
    <a href="index1.html">
        <button class="total-button" input-type="submit">RESTART</button>
    </a>
    <button class="total-button" id="quote-advance">GET IN TOUCH</button>
    <button class="total-button">SAVE PDF</button>
 </div>

My Javascript/jquery

  Var   contactNode    = $('#quote-advance'),
        quoteNode      = $('.quote-form'),
        tableData      = [],
        totalData      = [];

 contactNode.click(function(){
            $(".form-wrap").slideDown(750);
            $(function(){
                    totalTableNode.each(function(){
                        tableData.push($(this).html());
                    });

                    totalText.each(function(){
                        totalData.push($(this).html());
                    });
            quoteNode.submit(function(e){   
                $.ajax({
                  type : "POST",
                  url : 'server.php',
                  data : "content=" + tableData + totalData,
                  success: function(data) {
                      alert('Email Sent');// alert the data from the server
                  },
                  error : function() {
                  }

            });
                e.preventDefault();
            });

        });
    });

and the php

<?php 
$to  = 'test@gmail.com';
$subject = 'Testing';
$message = $_REQUEST['content'];
$message .= 'From' . $_POST["name"];
$message .= 'Email' . $_POST["email"];
$headers  = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
mail($to, $subject, $message, $headers);
?>

To reiterate, the email that I'm receiving at the moment contains ONLY the html table data. & if i change the quoteNode function to .click or .form. The email sends the input data, but not the html table data & redirects to blank server.php file (I want it to alert & stay on the current page as well as send the user input & the html table data). Please help! I'm stumped!!! Cheers!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douxiapi4381 douxiapi4381 7年前

    looks like the data isnt filled correctly. It could be submitted before it filled. Can you try this:

    EDIT (works at my server):

    HTML/ JS

    <div class="total-inner">
        <table class="total-table" id="table">
            <tbody>
                <tr>
                    <td>Device - iPad</td>
                    <td>$14000</td>
                </tr>
            </tbody>    
        </table>
    
        <hr><br>
    
        <div class="total-text-wrap">
            <p>Your App Will Cost Approximately  :  $<span class="total">0</span></p>   
        </div>
    
        <br><hr>
    </div>
    
    <div class="form-wrap">
        <form method="post" class="quote-form" action="#">
            <span>Company:<input id="name" type="text" name="name"></span>
            <span>Email:<input id="email" type="text" name="email"></span>
    
            <br>
    
            <input class="total-button" id="send-quote" type="submit" value="submit">
        </form>
     </div>
    
     <div class="button-wrap">
        <a href="index1.html">
            <button class="total-button" input-type="submit">RESTART</button>
        </a>
        <button class="total-button" id="quote-advance">GET IN TOUCH</button>
        <button class="total-button">SAVE PDF</button>
     </div>
    <script type="text/javascript" src="jQuery.js"></script>    
     <script>
    var contactNode    = $('#quote-advance'),
            quoteNode      = $('.quote-form');
    
    
    
    quoteNode.submit(function( event ) { 
      $(".form-wrap").slideDown(750);
    
    var dataTable = "";
    
    $('.total-table td').each(function() {
        dataTable += $(this).html();    
    });
    
    var name =$("#name").val(); 
    var email = $("#email").val();
    data= 'name=' + name + '&email=' + email + '&content=' + dataTable ;        
      alert(dataTable);
        $.ajax({
          type : "POST",
          url : 'server.php',
          data : data,
          success: function(data) {
              alert('Email Sent');// alert the data from the server
          },
          error : function() {
          }
        });
    event.preventDefault();
    });
    </script>
    

    PHP

    <?php 
    $to  = 'mail@gmail.com';
    $subject = 'Testing';
    $message = $_POST['content'];
    $message .= 'From' . $_POST["name"];
    $message .= 'Email' . $_POST["email"];
    $headers  = 'MIME-Version: 1.0' . "
    ";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
    ";
    mail($to, $subject, $message, $headers);
    ?>
    
    点赞 评论 复制链接分享