dongliehuan3925 2014-04-01 04:04
浏览 125
已采纳

使用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 2014-04-02 07:19
    关注

    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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办