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

5
我需要知道如何在不刷新字段的情况下从数据库中添加数据?就像电子邮件中添加联系人一样工作。如果我点击“添加”按钮,我需要打开一个小窗口并在其中查找联系人。如果我选择了一个或两个联系人并按下插入,它应该在不刷新父页面的情况下插入到“收件人”字段中..!!
我该如何在php或JavaScript中实现这个功能?请帮帮我 :)

我相信你正在寻找 Ajax。 - DOK
这个能行吗?如果可以的话,你能帮我吗? - user656521
3个回答

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); 
}); 

或者普通的ajax

$.ajax({
  url : url,
  data : {name : name}
  dataType : 'json',
  success : function(data) {}
});

3

您需要使用AJAX,今天它主要代表异步JavaScript和JSON。由于您似乎是新手,强烈建议使用好的AJAX库,如jQuery、YUI、Dojo、Prototype等。这将使您的代码比自己编写更容易,并且可能在浏览器之间也更具可移植性。搜索与jQuery、AJAX和PHP相关的教程。我曾经看过John Resig的一次很棒的演讲,他用非常少的代码演示了您正在尝试使用jQuery和PHP完成的工作。不幸的是,我现在无法记住标题或链接,但您应该能够轻松找到它。


1

我不太能理解你的问题,但是我认为dom操作可以解决这个问题。

只需看一下这个例子:http://jsfiddle.net/cyhello/Yfhrp/

希望对你有帮助。祝好运!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接