简体   繁体   English

如何在用逗号分隔的文本框中获取多个值

[英]How to Fetch Multiple Values in Textbox separated by comma

I have one drop-down box and one textbox. 我有一个下拉框和一个文本框。 based on my drop-down selection textbox retrieve along with DB value. 基于我的下拉选择文本框以及数据库值进行检索。 actually, its working fine without any issues. 实际上,它工作正常,没有任何问题。 But my problem was database has two or more textbox values in particular drop-down selection. 但是我的问题是数据库在特定的下拉选择中具有两个或多个文本框值。 so I want to if multiple textbox values come to the same drop-down selection then it will display all values in textbox separated by the comma (,). 因此,我想如果多个文本框值进入相同的下拉选择,则它将在文本框中显示所有值,并以逗号(,)分隔。 Any help greatly appreciated. 任何帮助,不胜感激。

Here is my used code for your reference:- 这是我使用的代码供您参考:

<select name="cat" id="cat">
        <option disabled selected value> -- select an option -- </option>
        <option value="1">Stencil Team</option>
        <option value="2">Tooling Team</option>
        <option value="3">IT</option>
        <option value="4">Manager</option>
        </select>

        <input name="phoneNumber" id="pnum"  type="text">

Jquery:- jQuery:

<script type="text/javascript">
            $(document).ready(function(){
                $("#cat").change(function(){
                    var deptid = $(this).val();
                    $.ajax({
                        url: 'ajaxdropdown.php',
                        type: 'post',
                        data: {depart:deptid},
                        dataType: 'json',
                        success:function(response){
                           var len = response.length;                       
                            $("#pnum").empty();
                            for( var i = 0; i<len; i++){
                                var id1234 = response[i]['id123'];
                                var name1234 = response[i]['name123'];

                              $("#pnum").val(name1234);
                            }
                        }
                    });
                });

            });
        </script>

Ajax Page:- Ajax页面:-

<?php
include('config.php');
// Check connection
if (!$db) {
 die("Connection failed: " . mysqli_connect_error());
}

$departid = $_POST['depart']; 

$sql = "SELECT Emp_Id,Mobile_Number FROM admin_panel WHERE Category=".$departid;

$result = mysqli_query($db,$sql);

$users_arr = array();

while( $row = mysqli_fetch_array($result) ){
    $userid = $row['Emp_Id'];
    $name = $row['Mobile_Number'];
    $users_arr[] = array("id123" => $userid, "name123" => $name);
}

echo json_encode($users_arr);
?>

My Sample Data:- 我的样本数据:

Category Mobile_Number
  IT      9629292929
  IT      8888888888
  IT      5623566666

If I select the drop-down category "IT" then I want my textbox Mobile_Number output looks like this:- 如果我选择下拉类别“ IT”,那么我希望我的文本框Mobile_Number输出看起来像这样:

9629292929,8888888888,5623566666

You can simply loop over json_encode() d array. 您可以简单地遍历json_encode() d数组。

And the join/implode the array values in a string combined by comma using PHP's in built function implode() 然后使用PHP的内置函数implode()将字符串中的数组值连接/插入到一个逗号分隔的字符串中

$arr = json_decode($yourArray);
$mobiles = array();
if (! empty($arr)) {
 foreach ($arr as $elem) {
  $mobiles[] = $elem['Mobile_Number'];
 }
}
$mobileStr = ! empty($mobiles) ? implode(',', $mobiles) : '';

Now, use this $mobileStr as value="$mobileStr" in your text box. 现在,在文本框中将此$mobileStr用作value="$mobileStr"

You may use join function of javascript. 您可以使用javascript的join函数。 Please refer following post. 请参考以下帖子。 https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_join https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_join

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM