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

标签 php javascript forms

我需要知道如何在不刷新字段的情况下从数据库中添加数据?我的意思是就像在电子邮件中添加联系人的工作一样。如果单击“添加”按钮,我需要打开一个小窗口并在其中添加联系人。如果我选中一个或两个联系人并按插入,它应该被插入到“收件人”字段中而无需刷新父页面..!!

我如何在 php 或 JavaScript 中做到这一点?请帮助我:)

最佳答案

您需要使用 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) {}
});

关于php - 如何在不刷新的情况下将数据插入字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5283165/

相关文章:

php - Google Chrome 无需主动请求即可刷新

php - 大整数乘积差分算法

php - 选择查询中的MySQL计算差异

javascript - 有没有办法像JQuery中的链接方法一样高效地表达Javascript代码?

html - 在不同的行上显示 radio 输入,但将文本与 radio 选择器保持在同一行

php - 通过服务器路径访问文件

javascript - 从 id 中删除整数

javascript数组转换为特定格式

python - Django 表单提交单选按钮值显示为 None

python - 属性错误 : 'EditForm' object has no attribute 'validate_on_submit'