php打开并填充表单,php – 在Dropdown Selection上,如何从Database填充完整的表单字段...

如何根据从下拉列表中选择的值填充数据库中的完整表单输入字段

示例:在应用程序中,通过选择客户端名称,它将填充完整的表单输入字段,并在数据库中存储详细信息.

Sample Code:

-- Select Client Name --

John

Smith

所有关于输入字段都需要填写客户端名称选择的值.

编辑:

我尝试使用AJAX但无法从文件中获取特定变量…下面是我的代码:

$(document).ready(function() {

$('#client').change(function() {

alert();

var selected = $(this).find(':selected').html();

$.post('get_details.php', {'client': selected}, function(data) {

$('#result').html(data);

});

});

});

在get_details.php文件中,我将不同的值存储在不同的变量中,但我不明白如何将它们转换为单个变量到主页面.

解决方法:

这只是一个调用自身的基本jQuery示例(当创建$_POST时脚本的顶部是活动的),我将其命名为index.php,如jQuery AJAX的url所示.如果需要,您可以使用两个单独的页面来执行此操作.只需将PHP与HTML / Javascript分开并更改url:’/ index.php’:

// This is where you would do any database call

if(!empty($_POST)) {

// Send back a jSON array via echo

echo json_encode(array("phone"=>'123-12313',"email"=>'test@test.com','city'=>'Medicine Hat','address'=>'556 19th Street NE'));

// Exit probably not required if you

// separate out your code into two pages

exit;

}

?>

-- Select Client Name --

John

Smith

$(document).ready(function() {

// On change of the dropdown do the ajax

$("#client").change(function() {

$.ajax({

// Change the link to the file you are using

url: '/index.php',

type: 'post',

// This just sends the value of the dropdown

data: { client: $(this).val() },

success: function(response) {

// Parse the jSON that is returned

// Using conditions here would probably apply

// incase nothing is returned

var Vals = JSON.parse(response);

// These are the inputs that will populate

$("input[name='phone']").val(Vals.phone);

$("input[name='email']").val(Vals.email);

$("input[name='city']").val(Vals.city);

$("textarea[name='address']").val(Vals.address);

}

});

});

});

来源:https://www.icode9.com/content-1-292101.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值