Javascript 在从“提交”按钮提交之前,使用ajax发布一些字段值

Javascript 在从“提交”按钮提交之前,使用ajax发布一些字段值,javascript,php,jquery,ajax,forms,Javascript,Php,Jquery,Ajax,Forms,在从提交按钮提交字段之前,我可以使用ajax发布一些值吗 我正在使用以下代码。它取得了成功,但我无法获得发布的数据。 有线索吗? 非常感谢您的帮助/评论。谢谢 function auto_fill(){ var that = $(this), url = './auto_fill.php', type = that.attr('method'), data = {}; that.fin

在从提交按钮提交字段之前,我可以使用ajax发布一些值吗

我正在使用以下代码。它取得了成功,但我无法获得发布的数据。 有线索吗? 非常感谢您的帮助/评论。谢谢

function auto_fill(){  

        var that = $(this),
            url = './auto_fill.php',
            type = that.attr('method'),
            data = {};

        that.find('[name]').each(function(index,value){
            var that =$(this),
                name =that.attr('name'),
                value = that.val();
                data[name] = value;
        }); 

        $.ajax({
            url: url,
            type: type,
            data: data,
            success: function(response) {
                        document.getElementById("f_name").value="<?php echo $name ?>";
                        document.getElementById("phn_no").value="<?php echo $phn_num ?>";

            }
        });

        return false;
    }
函数自动填充(){
var that=$(此),
url='./auto_fill.php',
type=that.attr('method'),
数据={};
that.find('[name]')。每个(函数(索引,值){
var that=$(此),
name=that.attr('name'),
value=that.val();
数据[名称]=值;
}); 
$.ajax({
url:url,
类型:类型,
数据:数据,
成功:功能(响应){
document.getElementById(“f_name”).value=“”;
文件.getElementById(“phn_编号”).value=“”;
}
});
返回false;
}
试试这个

function auto_fill(){
  $.getJSON("./auto_fill.php?id_num=" + $("#id_num").val(),
        function(data){
          $.each(data, function(i,item){
            if (item.field == "first_name") {
              $("#f_name").val(item.value);
            } else if (item.field == "phonenum") {
              $("#phn_no").val(item.value); 
            } 
          });
        });
}
auto_fill.php
您可以通过此JSON数组将任何变量传递给此值。

echo$name将仅在第一页加载时起作用,您应该使用您得到的响应来设置值什么响应返回服务器脚本?如何调用
auto_fill
?@juvian I used
if(isset($\u POST['id\u num']){$name=Sam
在auto_fill.php中,然后将其包含在我的form.php中,该表单包含在其中。这就是您要做的吗?
$id = $_GET['id_num'];

//build the JSON array for return
$json = array(array('field' => 'first_name', 
                    'value' => 'Your name'),

              array('field' => 'phonenumber', 
                    'value' => $phn),
              array('field' => 'somethinglikeDiscription', 
                    'value' => 'You entered ID as '.$id));
echo json_encode($json );