PHP/JS : Echoing several variables without losing their value

标签 php javascript jquery ajax

我构建的 JS/Ajax 函数无需单击按钮或刷新页面即可提交。该函数获取输入字段的值,并用 php 回显结果。但是每次回显一个变量时,下一个变量都会删除前一个变量的值。如何避免这种情况? EXAMPLE

JS

<script>
$(document).ready(function() {
  var timer = null; 
    var dataString;   
      function submitForm(){
        $.ajax({ type: "POST",
           url: "index.php",
           data: dataString,
           success: function(result){
                         $('#special').html('<p>' +  $('#resultval', result).html() + '</p>');
                                           }
                 });
                 return false; }

    $('#contact_name').on('keyup', function() {
    clearTimeout(timer);
    timer = setTimeout(submitForm, 050);
          var name = $("#contact_name").val();
    dataString = 'name='+ name;
    });

     $('#email').on('keyup', function() {
     clearTimeout(timer);
     timer = setTimeout(submitForm, 050);
     var name = $("#email").val();
     dataString = 'name='+ name;
     });

     $('#phone').on('keyup', function() {
     clearTimeout(timer);
     timer = setTimeout(submitForm, 050);
     var name = $("#phone").val();
     dataString = 'name='+ name;
     });

     $('#address').on('keyup', function() {
     clearTimeout(timer);
     timer = setTimeout(submitForm, 050);
     var name = $("#address").val();
     dataString = 'name='+ name;
     });

     $('#website').on('keyup', function() {
     clearTimeout(timer);
     timer = setTimeout(submitForm, 050);
     var name = $("#website").val();
     dataString = 'name='+ name;
     });


 }); 
</script>

HTML/PHP

<form action="" method="post" enctype="multipart/form-data" id="contact_form" name="form4"> 
     <div class="row">  
      <div class="label">Contact Name *</div> <!-- end .label --> 
        <div class="input"> 
          <input type="text" id="contact_name" class="detail" name="contact_name" value="<?php $contact_name ?>" />  
          <div id="special"><span id="resultval"></span></div>  
        </div><!-- end .input--> 
     </div><!-- end .row --> 
     <div class="row">  
      <div class="label">Email Address *</div> <!-- end .label --> 
       <div class="input"> 
        <input type="text" id="email" class="detail" name="email" value="<?php $email ?>" />  
        <div id="special"><span id="resultval"></span></div> 
       </div><!-- end .input--> 
     </div><!-- end .row --> 
    </form>

最佳答案

你可以使用append()方法:

success: function(result){
        $('#special').append('<p>' + result + '</p>');
}

因为您已经为输入设置了相似的类,您可以缩小代码:

 $('.detail').on('keyup', function() {
    clearTimeout(timer);
    var name = $(this).val();
    dataString = 'name='+ name;
    timer = setTimeout(submitForm, 050);
 });

请注意,ID必须 是唯一的,并且从服务器重复请求数据效率不高。

关于PHP/JS : Echoing several variables without losing their value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11573321/

相关文章:

jquery - 在 emberjs 中加载带有夹具数据的路线时出错

javascript - 使用 key 及其后缀从对象创建数组

javascript - 使用另一个函数的值调用一个函数

jquery - 如何传递多个变量?

Php Sum 与 PDO 函数与 mysql

php - 在无法保证 ID 匹配的情况下连接两个 MySQL 表很困难

php - Oracle ODBC unixODBC][驱动程序管理器]无法打开库

php - 显示接下来的三个星期六

javascript - 缩小 angularjs 应用程序时出现错误 : Unexpected token punc «)», 预期的 punc «,»

javascript - leaflet 使用 jquery 和选择器更改圆半径