2015-06-14 49 views
4

填写完整的表单字段如何填补数据库基于在下拉选择,如何从数据库

实例从下拉列表中选择的值完整的表单输入字段:在一个应用程序,通过选择它填补了一个客户端名称包含存储在数据库中的详细信息的完整表单输入字段。

Sample Code: 
<select name="client"> 
<option value="">-- Select Client Name -- </option> 
<option value="1">John</option> 
<option value="2">Smith</option>        
</select> 

<input name="phone" type="text" value=""> 
<input name="email" type="text" value=""> 
<input name="city" type="text" value=""> 
<textarea name="address"></textarea> 

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


编辑:

我试着用AJAX却苦于无法从文件中获取特定的变量...以下是我的代码:

<script> 
    $(document).ready(function() { 
     $('#client').change(function() { 
      alert(); 
      var selected = $(this).find(':selected').html(); 

      $.post('get_details.php', {'client': selected}, function(data) { 
       $('#result').html(data); 
      }); 
     }); 
    }); 
</script> 

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

+0

使用Ajax。 Jquery很容易。 – Rasclatt

+0

到目前为止,您尝试过哪些方法来实现您的目标? –

+0

试图用AJAX但该文件可能无法获得特定的变量...以下是我的代码 在get_details.php文件中iam在不同的变量中存储不同的值,但我不明白如何让它们变成主页面的个体变量 – Krish

回答

5

这是只是一个基本的jQuery例子呼叫本身(当$_POST由脚本的顶部部分是活动的),这是我命名index.php如jQuery的AJAX的url指示。如果需要,您可以使用两个单独的页面来完成此操作。刚分离出来自HTML/JavaScript中的PHP和改变url: '/index.php'

<?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"=>'[email protected]','city'=>'Medicine Hat','address'=>'556 19th Street NE')); 
    // Exit probably not required if you 
    // separate out your code into two pages 
    exit; 
} 
?> 

<form id="tester"> 
    <select name="client" id="client"> 
     <option value="">-- Select Client Name -- </option> 
     <option value="1">John</option> 
     <option value="2">Smith</option>        
    </select> 
    <input name="phone" type="text" value=""> 
    <input name="email" type="text" value=""> 
    <input name="city" type="text" value=""> 
    <textarea name="address"></textarea> 
</form> 

<!-- jQuery Library required, make sure the jQuery is latest --> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
<script> 
    $(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); 
        } 
      }); 
     }); 
    }); 
</script> 
+0

的输入字段谢谢@Rasclatt,It Works !! – Krish

+0

没问题!确保您将问题标记为已回答,以便您没有其他人试图解决已解决的问题。欢呼声 – Rasclatt

+0

你会显示你的index.php文件代码吗? – Aswathy

-3

当你取得了JSON格式AjaxCall的回报数据,如

json_encode(array("phone"=>'123-12313',"email"=>'[email protected]','city'=>'Medicine Hat','address'=>'556 19th Street NE')); 

如上图所示

然后在jQuery的解析它并将其放入不同的选择器中,如

var Vals = JSON.parse(response); 
// These are the inputs that will populate 
$("input[name='phone']").val(Vals.phone); 

如上所示。

+3

你为什么要复制我的答案的部分?那是个奇怪的人。单词复制,更糟糕! – Rasclatt

+0

:)我只写了你的代码的解释是否 –

+3

我为OP做了大量的符号,我不认为你的增加比我写的更清晰。 – Rasclatt

相关问题