我们如何在PHP中通过AJAX填充selectbox值更改中的多个文本框

how can we fill the multiple textboxes from the selectbox value change through AJAX in PHP

本文关键字:文本 selectbox 填充 PHP AJAX 我们      更新时间:2023-09-26

我想通过PHP和HTML中的AJAX,通过selectbox的onchange事件来填充我的所有文本框和动态表。我已经做了一些工作,但是这个AJAX函数只返回数据库文件中的一个值。这是Script

<script type="text/javascript">
     function fetch_select(val)
     {
         $.ajax({
                 type: 'post',
                 url: 'dbassign.php',
                 data: {
                           get_option:val
                       },
                 success: function (response) {
              $('#cust').val(response);
         }
   });
}

这是用于选择框onchange事件

<select name='cminvoice' onchange='fetch_select(this.value)' class='form-control' autofocus>

这是我的数据库文件

if(isset($_POST['get_option']))
{
     $state = $_POST['get_option'];
     $find=mysql_query("select custn from tbbill where invoice=$state");
     while($row=mysql_fetch_array($find))
     {
       echo "$row[custn]";
     }
}

我对多个值应用了相同的函数,但它在相同的文本框中显示所有数据。

不要忘记包含jquery.js试试这个代码按照创建表格

CREATE TABLE IF NOT EXISTS `tbbill` (
  `id` int(11) NOT NULL,
  `custn` varchar(25) NOT NULL,
  `invoice` varchar(25) NOT NULL
  ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
  --
  -- Dumping data for table `tbbill`
  --
INSERT INTO `tbbill` (`id`, `custn`, `invoice`) VALUES
(1, 'abc', 'Value1'),
(2, 'xyz', 'Value1'),
(3, 'pqr', 'Value1'),
(4, 'qwe', 'Value1');

page1.php

<html>
<head>
<script src="jquery.js" type="text/javascript"></script>  
<script type="text/javascript">
function fetch_select(val)
 { alert(val);
     $.ajax({
             type: 'post',
             url: 'dbassign.php',
             data: {
                       get_option:val
                   },
             success: function (response) {
            response=response.replace(/,'s*$/, "");
            console.log(response);
            response=response.split(","); 
            var i=0;
            $.each(response, function(key, val) {
                i++;
                if(val!=""){
                    $(".response_elements").append("<input type='text' 
name='custn"+i+"' value='"+val+"'/><br/>");     
                }
            });
       }
   });
}
</script>
</head>
<body>
<select name='cminvoice' onchange='fetch_select(this.value)' class='form-
control' autofocus>
<option value="">-Select-</option>
<option value="Value1">Value1</option>
<option value="Value2">Value2</option>
</select>
<div class="response_elements">
</div>
</body>
</html>

dbassign.php

<?php 
if(isset($_POST['get_option']))
{
     $state = $_POST['get_option'];
     $con=mysql_connect('localhost','root','');
     mysql_select_db('db_wtg');
     $find=mysql_query("select custn from tbbill where     
     invoice='".$state."'");
     $data="";
     while($row=mysql_fetch_array($find))
     {
       $data.=$row['custn'].",";
     }
    echo $data;
}
?>

使用JSON_encode执行此任务,使请求易于处理。

在php文件中从服务器获得结果后,尝试做一些简单的事情,比如这个

 $data = array("for_box_one"=>'value1',"for_box_two"=>'value2');
 echo json_encode($data);
 die;

也更改

    success: function (response) {
          $('#cust').val(response);
     }

     success: function (response) {
          $('#cust').val(response.for_box_one);
          // rest will be same 
          $('#cust2').val(response.for_box_two);
          // as so on whatever you want 
     }