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

时间:2011-03-12 14:44:23

标签: php javascript forms

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

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

3 个答案:

答案 0 :(得分:3)

你必须使用AJAX,它现在主要用于异步JavaScript和JSON。由于你似乎对此不熟悉,我强烈建议使用一个好的AJAX库,如jQuery,YUI,Dojo,Prototype等。这将使你的代码比自己完成更容易,并且可能更加便携浏览器。搜索与jQuery,AJAX和PHP相关的教程。前段时间我看到John Resig的一个很好的演讲,他用很少的代码演示了你用jQuery和PHP做的事情。不幸的是我现在不记得标题或链接,但你应该能够毫无问题地找到它。

答案 1 :(得分: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) {}
});

答案 2 :(得分:1)

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

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

希望它有所帮助。祝你好运!

相关问题