从下拉列表中选择项时自动填充表单字段的PHP\HTML脚本

从下拉列表中选择项时自动填充表单字段的PHP\HTML脚本,php,html,postgresql,auto-populate,Php,Html,Postgresql,Auto Populate,我创建了一个HTML表单,其中包含来自 postgresql数据库,我希望表单中的一些字段 从中选择项目时,从数据库自动填充 下拉菜单。表单上还有一些其他字段 在按下提交按钮之前,需要由用户填写 将整个数据推回数据库 当前我的脚本无法连接到数据库或填充 表单上的下拉列表也无法自动填充 其他指定的表单字段(这些字段是项的属性 在下拉列表中也存储在数据库中)和第二个问题 im还有一个问题,就是在将数据输入到输入字段和 单击提交。它在数据库中创建一个新记录,而不是 填充数据库中所需的列。能找个人吗 看

我创建了一个HTML表单,其中包含来自 postgresql数据库,我希望表单中的一些字段 从中选择项目时,从数据库自动填充 下拉菜单。表单上还有一些其他字段 在按下提交按钮之前,需要由用户填写 将整个数据推回数据库

当前我的脚本无法连接到数据库或填充 表单上的下拉列表也无法自动填充 其他指定的表单字段(这些字段是项的属性 在下拉列表中也存储在数据库中)和第二个问题 im还有一个问题,就是在将数据输入到输入字段和 单击提交。它在数据库中创建一个新记录,而不是 填充数据库中所需的列。能找个人吗 看看我的剧本,帮我一把

下面是使用 下拉列表


如果要填充文本字段的数据是select选项的属性,我会使用jquery。拉动要使用的选定选项属性,然后也可以使用jquery将其输出到文本框。它可以由选择菜单上的更改事件触发。例如

$('#selectid').change(function() {
    var opt = $('#selectid option:selected');
    $('#text1id').val(opt.attr('attrfortext1'));
    $('#text2id').val(opt.attr('attrfortext2'));
    $('#text3id').val(opt.attr('attrfortext3'));
});
我认为应该使用Ajax访问另一个PHP文件来解析数据库,并返回一个json编码的数组,其中包含要填充文本字段的数据。根据您筛选的数据量,如果每个选项有多个属性,并且有100个选项,则您的方法可能会很快变得丑陋


Craig

我会使用Ajax和JQuery来实现这一点。您需要做3件事-在文档的
中添加Ajax/JQuery代码,为Ajax创建要连接的php文件,并在字段中添加
onChange
id
value
属性

中,在
-

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
function check(){   
    var pipeno = $('#pipeno').val();
    if(pipeno != "pipeno"){
      jQuery.ajax({
      type: "POST",
      url: "check.php",
      data: 'pipeno='+pipeno,
      cache: false,
      success: function(response){
     var response_array = JSON.parse(response);
     $('#wallthickness').val(response_array['wallthickness']);  
     $('#jointno').val(response_array['jointno']);
     $('#measuredlength').val(response_array['measuredlength']);
     $('#serialno').val(response_array['serialno']);}
    });
    }
    else{
         $('#wallthickness').val('');   
     $('#jointno').val('');
     $('#measuredlength').val('');
     $('#serialno').val('');}
    }
</script>
最后,将
id
value
属性添加到表单字段中(不要更改文件,只需更新这些行)

check1.php-

<?php
//Php Code to connect to postgresqldatabase
$PGHOST = "localhost:25376";
$PGDATABASE = "Pipeline";
$PGUSER = "postgres";
$PGPASSWORD = "Casa2009";
$PGPORT = 5432;
$db_handle = pg_connect("dbname=$PGDATABASE user=$PGUSER password=$PGPASSWORD");

// Code to pull data from the database and load onto the form  
$pipeno = pg_escape_string($_POST['pipeno']);
$query = "SELECT * FROM fieldtally1 WHERE pipeno = $pipeno ";
$result = pg_query($db_handle,$query); 
$row = pg_fetch_row($result);
$row_info = array('heatno1'=>$row[1],'pipeno2'=>$row[2],'heatno2'=>$row[3],'jointno'=>$row[4]);
$row_info = json_encode($row_info);
print_r($row_info); 
?>

以下是问题-

1-将所有
脚本移到页面顶部。这将:(1)清理代码,(2)能够同时处理所有数据库查询

2-在从数据库中获取下拉列表之前,您要更新
fieldtally1
的查询现在已经完成

3-在输入字段中输入数据并单击提交后,要修复第二个问题
。它在数据库中创建新记录,而不是填充数据库中所需的列
use
INSERT-INTO。。。价值观在重复密钥更新时…
。如果
pipeno
已经在数据库中(因为它是
主键
),它将
更新
,而不是
插入

4-我建议保存您的
错误
成功
消息(#4a)并在html(#4b)顶部回显它

5a-您使用该功能将硬编码的
pipeno
下拉列表更改为动态下拉列表。因此您缺少javascript-
onChange=check()
5b-现在您正在创建
pipeno
&
onChange=check()动态,您必须更改
check()
函数以动态获取id

6-
$PHP\u SELF
无效。我想您是在尝试
$\u服务器['PHP\u SELF']
,但这很容易被黑客攻击,所以最好只使用
action=“”
。[6.1,6.2,6.3]

7-在执行
时,您使用的是相同的
值=”“
,但每个值都需要不同,以便在发布时获得值。此外,大多数
被拼错为
。[7.1,7.2,7.3]

8-如果您在同一页面上有3个表单,则每个表单必须有不同的名称,否则您将无法分辨单击了哪个提交按钮。[8.1,8.2,8.3]

9-前两张表单缺少结束标记-
。[9.1,9.2]

10-在您的2和3个表单中,您使用的是第一个表单中使用的
id
。这是无效的,因为每个
id
必须是唯一的。[10.1,10.2]


11-第三张表格使用了表格2中使用的
id=“wallthickness”
。请参见上文关于
id
唯一性的内容

您可以使用ajax技术来实现您想要的。关于它有很多参考资料。感谢Craig的贡献,但不幸的是,我对jquery不是很了解,尽管我知道它是一个简洁的Javascript代码,并且在我过去的脚本编写经验中使用过一点Javascript。我将进行一次尝试,希望它能完成任务,但我仍然相信我需要php从服务器中提取数据,我只是想知道如何将这两个代码结合起来才能使函数正常工作。我已经厌倦了这一点,但我只是没有取得任何进展,因为我帮了我的忙,我做了修改,脚本可以使用下拉列表创建表单,但无法填充下拉列表或列出的FormFeild。在我看来,php脚本似乎没有被Javascript访问,因为它没有回显“connection Succeed”支票以确认与数据库的连接。我已经修改了上面的脚本,以反映到目前为止的情况。嗨,肖恩,我一直在编写脚本,你还记得,最初我的墙厚度是一个下拉列表,但当前的脚本使它自动填充而不是下拉列表。虽然autopopulate和下拉列表目前都没有填充,但我只是按照选项中的定义将Wallthickness设置为静态填充,它工作得非常好,但是pipeno的下拉列表填充以及为各个字段定义的autopopulate尚未完成
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
function check(){   
    var pipeno = $('#pipeno').val();
    if(pipeno != "pipeno"){
      jQuery.ajax({
      type: "POST",
      url: "check.php",
      data: 'pipeno='+pipeno,
      cache: false,
      success: function(response){
     var response_array = JSON.parse(response);
     $('#wallthickness').val(response_array['wallthickness']);  
     $('#jointno').val(response_array['jointno']);
     $('#measuredlength').val(response_array['measuredlength']);
     $('#serialno').val(response_array['serialno']);}
    });
    }
    else{
         $('#wallthickness').val('');   
     $('#jointno').val('');
     $('#measuredlength').val('');
     $('#serialno').val('');}
    }
</script>
<?php
 //Php Code to connect to postgresqldatabase
 include ("connection.php");
 // Code to pull data from the database and load onto the form  
 $pipeno = pg_escape_string($_POST['pipeno']);
 $query = "SELECT * FROM fieldtally WHERE pipeno = $pipeno ";
 $result = pg_query($db_handle,$query); 
 $row = pg_fetch_row($result))
 $row_info = array('wallthickness'=>$row[1],'jointno'=>$row[2],'measuredlength'=>$row[3],'serialno'=>$row[4]);

 $row_info = json_encode($row_info);
 print_r($row_info); 
?>
Select Pipe No:<select name="pipeno" id="pipeno" onChange="check()"><option value="pipeno"> --Select-- </option> 
...  (keep your database connection / how you create your dropdown, etc here, just edit the option below)
 echo "<option value=\"$pipeno\"> $pipeno</option>";
... (keep your database connection / how you create your dropdown, etc. here)
</select> 
...
Input Joint No: <input type="text" name="jointno" id="jointno">
Input Wall Thickness: <input type="text" name="wallthickness" id="wallthickness">
Input measured Length: <input type="text" name="measuredlength" id="measuredlength">
Input Serial No: <input type="text" name="serialno" id="serialno">
<!-- #1 -->
<?php
//Php Code to connect to postgresqldatabase
$PGHOST = "localhost:25376";
$PGDATABASE = "Pipeline";
$PGUSER = "postgres";
$PGPASSWORD = "Casa2009";
$PGPORT = 5432;
$db_handle = pg_connect("dbname=$PGDATABASE user=$PGUSER password=$PGPASSWORD");

//<!-- #2 -->
 if(isset($_POST['submit_1'])){
 //Code to post data to the database
 $pipeno = pg_escape_string( $_POST['pipeno']);
 $wallthickness = pg_escape_string($_POST['wallthickness']);
 $heatno1 = pg_escape_string( $_POST['heatno1']);
 $pipeno2 = pg_escape_string( $_POST['pipeno2']);
 $heatno2 = pg_escape_string($_POST['heatno2']);
 $jointno = pg_escape_string($_POST['jointno']);
 $measuredlength = pg_escape_string($_POST['measuredlength']);
 $serialno = pg_escape_string($_POST['serialno']); 
 $wthick= pg_escape_string($_POST['wthick']);

//<!-- #3 -->
 $query = "INSERT INTO fieldtally1(pipeno,wallthickness,heatno1,pipeno2,heatno2,jointno,measuredlength,serialno,wthick)VALUES ('$pipeno','$wallthickness','$heatno1','$pipeno2','$heatno2','$jointno','$measuredlength','$serialno','$wthick') ON DUPLICATE KEY UPDATE wallthickness='$wallthickness',heatno1='$heatno1',pipeno2='$pipeno2',heatno2='$heatno2',jointno='$jointno',measuredlength='$measuredlength',serialno='$serialno',wthick='$wthick'";

//<!-- #4a -->
 $result = pg_query($query);
 if (!$result) {
 $errormessage = pg_last_error();
 $message = "Error with query: " . $errormessage;
 }
 $message = sprintf ("These values were inserted into the database - %s %s %s %s %s %s %s %s %s",$pipeno,$wallthickness,$heatno1,$pipeno2,$heatno2,$jointno,$measuredlength,$serialno,$wthick);
 }

 // Code to pull data from the database and load onto the form
 $query = 'select pipeno, wallthickness from fieldtally1 order by pipeno asc'; 
 $result = pg_query($db_handle,$query); 
 while ($row = pg_fetch_row($result))
 {
    // Creates Arrays to use in dropdowns
     $pipeno_array[] = $row[0];
     $wallthickness_array[] = $row[1];
 } 

  // This function creates dropdowns that can be used in your forms
 function dropdown($field_name, $num){
     // Creates the Dropdown
 //<!-- #5a -->
     $c = ($field_name == 'pipeno') ? ' onChange="check('.$num.');"' : '';
     echo "<select name=\"".$field_name."\" id=\"".$field_name.$num."\"$c>\n";
     echo "<option value=\"\"> --- Select --- </option>\n";
     // Chooses which array to use for Dropdown options
     global $pipeno_array, $wallthickness_array;
     $name_array = ($field_name == 'pipeno') ? $pipeno_array : $wallthickness_array;
     // Creates the Dropdown options based off the array above
     foreach($name_array as $k){
         echo "<option value=\"$k\">$k</option> \n"; }
     // Ends the Dropdown
     echo "</select>\n";
 }

 ?>
 <html>
     <head><title>UG Pipeline Field Data Capture</title></head>
      <body>
       <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
       <script type="text/javascript">
 <!-- #5b -->
        function check(num){
           var pipeno_id = '#pipeno_'+num;
           var pipeno = $(pipeno_id).val();
           if(pipeno != ""){
             jQuery.ajax({
             type: "POST",
             url: "check1.php",
             data: 'pipeno='+pipeno,
             cache: false,
             success: function(response){
             var response_array = JSON.parse(response);
             $('#heatno1').val(response_array['heatno1']);
             $('#pipeno2').val(response_array['pipeno2']); 
             $('#heatno2').val(response_array['heatno2']);
             $('#jointno').val(response_array['jointno']);
             //$('#measuredlength').val(response_array['measuredlength']); // this should be input from the user
            // $('#serialno').val(response_array['serialno']);  //This should also be input from the user
            }           
            });
           }
           else{
           $('#heatno1').val('');
           $('#pipeno2').val('');
           $('#heatno2').val('');
           $('#jointno').val('');}
            }
  </script> 

 <!-- #4b -->
 <?php printf($message);?>
 <!-- #6.1 -->
 <form action="" method="post">
 <table width="800" cellpadding= "10" cellspacing="1" border="2">
 <tr align="center" valign="top">
 <td align="center" colspan="1" rowspan="1" bgcolor="#64b1ff">
 <h3>Input Field Tally Information</h3>
      Select Pipe Thickness:<select name="wthick" id="wthick">
 <!-- #7.1 -->
  <option value=""> --Select-- </option> 
  <option value="9.8">  9.8  </option>
  <option value="13.5"> 13.5 </option>
  <option value="15.9"> 15.9 </option>
  </Select>           
 Select Pipe No:<?php dropdown('pipeno', 1); ?>  Select Wall Thickness:<?php dropdown('wallthickness', 1); ?><br /><br /> 
 HeatNo1: <input type="text" name="heatno2" id="heatno1"> PipeNo2: <input type="text" name="pipeno2" id="pipeno1"> HeatNo2: <input type="text" name="heatno2" id="heatno2"><br /><br /> 
 Joint No: <input type="text" name="jointno"> Input measured Length: <input type="text" name="measuredlength"> Input Serial No: <input type="text" name="serialno"><br><br> 
 <!-- #8.1 -->
 <input type="Submit" name="submit_1" value="Submit">
 <!-- #9.1 -->
 </td></tr></table></form>
 <p></p>

 <!-- #6.2 -->
 <form action="" method="post">
 <table width="800" cellpadding= "10" cellspacing="1" border="2">
 <tr align="center" valign="top">
 <td align="center" colspan="1" rowspan="1" bgcolor="#ff9d9d">
 <h3>Input Field Bend Information</h3>
  Select Wall Thickness:<select name="wallthickness" id="wallthickness">
 <!-- #7.2 -->
     <option value=""> --Select-- </option> 
     <option value="9.8">  9.8  </option>
     <option value="13.5">13.5 </option>
     <option value="15.9"> 15.9 </option>
  </select>
 <!-- #10.1 -->           
 Select Pipe No:<?php dropdown('pipeno', 2); ?>  Select Wall Thickness:<?php dropdown('wallthickness', 2); ?><br /><br /> 
 HeatNo1: <input type="text" name="heatno1" id="heatno1_2"> PipeNo2: <input type="text" name="pipeno2" id="pipeno2_2"> HeatNo2: <input type="text" name="heatno2" id="heatno2_2"><br /><br /> 
 Joint No: <input type="text" name="jointno"> Input Measured Distance: <input type="text" name="measureddistance"><br><br> 
 Input Bend Angle: <input type="text" name="benddegree"> Input Bend Type: <input type="text" name="bendtype"><br><br>
 <!-- #8.2 -->
 <input type="Submit" name="submit_2" value="Submit">
 <!-- #9.2 -->
 </td></tr></table></form>
 <p></p>

 <!-- #6.3 -->
 <form action="" method="post">
 <table width="800" cellpadding= "10" cellspacing="1" border="2">
 <tr align="center" valign="top">
 <td align="center" colspan="1" rowspan="1" bgcolor="#66CC66">
 <h3>Input App. Tally Information</h3>
 <!-- #11 -->
      Select Wall Thickness:<select name="wallthickness1" id="wallthickness1">
 <!-- #7.3 -->
     <option value=""> --Select-- </option> 
     <option value="9.8">  9.8  </option>
     <option value="13.5"> 13.5 </option>
     <option value="15.9"> 15.9 </option>
  </select>
 <!-- #10.2 -->             
 Select Pipe No:<?php dropdown('pipeno', 3); ?>  Select Wall Thickness:<?php dropdown('wallthickness', 3); ?><br /><br />            
 Input Tally Type: <input type="text" name="type">   Input Serial No: <input type="text" name="serialno"><br><br>
 Input Reference ID: <input type="text" name="referenceid"><br><br>
 <!-- #8.3 -->
 <input type="Submit" name="submit_3" value="Submit">
 </td></tr></table>
 </form>
 </body>
 </html>
<?php
//Php Code to connect to postgresqldatabase
$PGHOST = "localhost:25376";
$PGDATABASE = "Pipeline";
$PGUSER = "postgres";
$PGPASSWORD = "Casa2009";
$PGPORT = 5432;
$db_handle = pg_connect("dbname=$PGDATABASE user=$PGUSER password=$PGPASSWORD");

// Code to pull data from the database and load onto the form  
$pipeno = pg_escape_string($_POST['pipeno']);
$query = "SELECT * FROM fieldtally1 WHERE pipeno = $pipeno ";
$result = pg_query($db_handle,$query); 
$row = pg_fetch_row($result);
$row_info = array('heatno1'=>$row[1],'pipeno2'=>$row[2],'heatno2'=>$row[3],'jointno'=>$row[4]);
$row_info = json_encode($row_info);
print_r($row_info); 
?>