如何在不刷新的情况下将数据插入字段?

5 javascript php forms

我需要知道如何在不刷新字段的情况下从数据库中添加数据?我的意思就像在电子邮件中添加联系人一样.如果我点击"添加"按钮,我需要在其中打开一个小窗口和联系人.如果我检查一个或两个联系人并按插入,则应将其插入"收件人"字段而不刷新父页面.!!

我怎么能用PHP或JavaScript做到这一点?请帮我 :)

Tre*_*vor 3

您需要使用 ajax 来执行此操作。Ajaxform 是一个很棒的插件,用于将数据从表单动态添加到页面。您也可以使用 jquery 中的 $.ajax。http://jquery.malsup.com/form/#ajaxForm

$(document).ready(function() { 
    var options = { 
        target:        '#output1',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 

        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 

        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 

    // bind form using 'ajaxForm' 
    $('#myForm1').ajaxForm(options); 
}); 
Run Code Online (Sandbox Code Playgroud)

或者常规的ajax

$.ajax({
  url : url,
  data : {name : name}
  dataType : 'json',
  success : function(data) {}
});
Run Code Online (Sandbox Code Playgroud)