0

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

目前我的脚本无法连接到数据库或填充表单上的下拉列表,也无法自动填充其他指定的表单字段(下拉列表中项目的属性也存储在数据库中)我还遇到的第二个问题是,在输入字段中输入数据并单击提交后。它在数据库中创建一条新记录,而不是在数据库中填充所需的列。有人可以看看我的脚本并帮助我。

这是使用下拉菜单创建数据输入表单的脚本

 <?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 fieldtally data to the database
 $pipeno = pg_escape_string( $_POST['pipeno']);
 $heatno1 = pg_escape_string( $_POST['heatno1']);
 $pipeno2 = pg_escape_string( $_POST['pipeno2']);
 $heatno2 = pg_escape_string($_POST['heatno2']);
 $Djointno = pg_escape_string($_POST['Djointno']);
 $measuredlength = pg_escape_string($_POST['measuredlength']);
 $serialno = pg_escape_string($_POST['serialno']);
 $wthick= pg_escape_string($_POST['wthick']);
 $remarks= pg_escape_string($_POST['remarks']); 
 //<!-- #3 -->
$query = "Update fieldtally set  wthick = $wthick, pipeno=$pipeno,heatno1=$heatno1,pipeno2=$pipeno2,heatno2=$heatno2,Djointno=$Djointno,measuredlength=$measuredlength,serialno=$serialno,remarks=$remarks where pipeno = $pipeno;
          INSERT INTO fieldtally(wthick, pipeno,heatno1,pipeno2,heatno2,Djointno,measuredlength,serialno,remarks), 
                Select $wthick, $pipeno,$heatno1,$pipeno2,$heatno2,$Djointno,$measuredlength,$serialno,$remark,
                WHERE NOT EXISTS (select pipeno from fieldtally where pipeno = $pipeno)";

//<!-- #4a -->
 $result = pg_query($query);
 if (!$result) {
 $errormessage = pg_last_error();
 $message = "Error with query: " . $errormessage;
 }
 $message = sprintf ("These values were inserted into the pipeline database - %s %s %s %s %s %s %s %s %s",$wthick,$pipeno,$heatno1,$pipeno2,$heatno2,$Djointno,$measuredlength,$serialno,$remarks);
 }
 //<!-- #2 -->
 if(isset($_POST['submit_2'])){
 //Code to post fieldbend data under suto the database
 $pipeno = pg_escape_string( $_POST['pipeno']);
 $wthick1= pg_escape_string($_POST['wthick1']);
 $heatno1 = pg_escape_string( $_POST['heatno1']);
 $pipeno2 = pg_escape_string( $_POST['pipeno2']);
 $heatno2 = pg_escape_string($_POST['heatno2']);
 $Djointno = pg_escape_string($_POST['Djointno']);
 $measureddistance = pg_escape_string($_POST['measureddistance']);
 $benddegree = pg_escape_string($_POST['benddegree']);
 $bendtype= pg_escape_string($_POST['bendtype']); 
 $remarks= pg_escape_string($_POST['remarks']);
 //<!-- #3 -->
$query1 = "INSERT INTO fieldbend(pipeno,wthick1,heatno1,pipeno2,heatno2,Djointno,measureddistance,benddegree,bendtype,remarks)VALUES ('$pipeno','$wthick1','$heatno1','$pipeno2','$heatno2','$Djointno','$measureddistance','$benddegree','$bendtype','$remarks') ON DUPLICATE KEY UPDATE wthick1='$wthick1',heatno1='$heatno1',pipeno2='$pipeno2',heatno2='$heatno2',Djointno='$Djointno''";

//<!-- #4a -->
 $result1 = pg_query($query1);
 if (!$result1) {
 $errormessage = pg_last_error();
 $message1 = "Error with query: " . $errormessage;
 }
 $message1 = sprintf ("These values were inserted into the pipeline database - %s %s %s %s %s %s %s %s %s %s",$pipeno,$wthick1,$heatno1,$pipeno2,$heatno2,$Djointno,$measureddistance,$benddegree, $bendtype,$remarks);
 }
 //<!-- #2 -->
 if(isset($_POST['submit_3'])){
 //Code to post apptally data under suto the database
 $pipeno = pg_escape_string( $_POST['pipeno']);
 $wthick2= pg_escape_string($_POST['wthick2']);
 $tallytype = pg_escape_string( $_POST['tallytype']);
 $qty = pg_escape_string( $_POST['qty']);
 $serialno = pg_escape_string($_POST['serialno']);
 $referenceid = pg_escape_string($_POST['referenceid']);
 //<!-- #3 -->
$query2 = "INSERT INTO apptally(pipeno,wthick2,tallytype,qty,serialno,referenceid)VALUES ('$pipeno','$wthick2','$tallytype','$qty','$serialno','$referenceid') ON DUPLICATE KEY UPDATE wthick2='$wthick2', pipeno='$pipeno'";

//<!-- #4a -->
 $result2 = pg_query($query2);
 if (!$result2) {
 $errormessage = pg_last_error();
 $message2 = "Error with query: " . $errormessage;
 }
 $message2 = sprintf ("These values were inserted into the Pipeline database - %s %s %s %s %s %s",$pipeno,$wthick2,$tallytype,$qty, $serialno,$referenceid);
 }
 // Code to pull data from the database and load onto the form
 $query = 'select pipeno from fieldtally 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];

 } 
 // This function creates dropdowns that will be used in the 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;
     $name_array = ($field_name == 'pipeno') ? $pipeno_array : $wallthick;
     // 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>
<meta charset="utf-8">
<title>UG Pipeline Field Data Capture</title>
</head>
<body>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     </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: "check.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']);
             $('#Djointno').val(response_array['Djointno']);
             }           
            });
           }
           else{
           $('#heatno1').val('');
           $('#pipeno2').val('');
           $('#heatno2').val('');
           $('#Djointno').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="#00CC00">
 <h3>Input Field Tally Information</h3>
      Select Wall Thickness:<select name="wthick" id="wthick">
 <!-- #7.1 -->
  <option value=""> --Select-- </option> 
  <option value="9.80">  9.80  </option>
  <option value="13.50"> 13.50 </option>
  <option value="15.90"> 15.90 </option>
  </Select>           
 Select Pipe No:<?php dropdown('pipeno', 1); ?> HeatNo1: <input type="text" name="heatno1" id="heatno1"><br /><br />  
 PipeNo2: <input type="text" name="pipeno2" id="pipeno2"> HeatNo2: <input type="text" name="heatno2" id="heatno2">Joint No: <input type="text" name="Djointno"><br /><br /> 
  Input measured Length: <input type="text" name="measuredlength"> Input Serial No: <input type="text" name="serialno"><br><br> Remarks: <input type="text" name="remarks"><br><br> 
 <!-- #8.1 -->
 <input type="Submit" name="submit_1" value="Submit">
 <!-- #9.1 -->
</td></tr></table></form>
<!-- #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="#FF99FF">
 <h3>Input Field Bend Information</h3>
  Select Wall Thickness:<select name="wthick1" id="wthick1">
 <!-- #7.2 -->
     <option value=""> --Select-- </option> 
     <option value="9.80">  9.80  </option>
     <option value="13.50">13.50 </option>
     <option value="15.90"> 15.90 </option>
  </select>
 <!-- #10.1 -->           
 Select Pipe No:<?php dropdown('pipeno', 2); ?>   HeatNo1: <input type="text" name="heatno1" id="heatno1_2"> <br><br>
 PipeNo2: <input type="text" name="pipeno2" id="pipeno2_2"> HeatNo2: <input type="text" name="heatno2" id="heatno2_2"> Joint No: <input type="text" name="Djointno"> <br><br>
 Input Measured Distance: <input type="text" name="measureddistance"> Input Bend Angle: <input type="text" name="benddegree"> <br><br>
Select Bend Type:<select name="bendtype" id="bendtype">
    <option value=""> --Select-- </option> 
    <option value="Combo">Combo</option>
    <option value="SAG">SAG</option>
    <option value="OB">OB</option>
    <option value="SBRT">SBRT</option>
    <option value="SBLT">SBLT</option>
    <option value="HBLT">HBLT</option>
    <option value="HBRT">HBRT</option><p></p> 
  Remarks: <input type="text" name="remarks"><br></br>

  <input type="Submit" name="submit_2" value="Submit"> 
 <!-- #9.2 -->
</td></tr></table></form>
<!-- #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="#99FF33">
 <h3>Input App. Tally Information</h3>
 <!-- #11 -->
       Select Wall Thickness:<select name="wthick2" id="wthick2">
 <!-- #7.3 -->
     <option value=""> --Select-- </option> 
     <option value="9.80">  9.80  </option>
     <option value="13.50"> 13.50 </option>
     <option value="15.90"> 15.90 </option>
  </select>
 <!-- #10.2 -->             
 Select Pipe No:<?php dropdown('pipeno', 3); ?>  <br><br> Input Tally Type: <input type="text" name="tallytype">
 Input Tally Qty: <input type="text" name="qty"><br></br>  Input Serial No: <input type="text" name="serialno"> 
 RefID: <input type="text" name="referenceid"><br></br>
 <!-- #8.3 -->
 <input type="Submit" name="submit_3" value="Submit">
 </td></tr></table>
</form>

</body>
</html>

将数据加载到表单字段并从数据库中推回数据的 PHP 脚本 - 文件名 = 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); 
?>

当我在有或没有数据的情况下点击提交按钮时出现的错误是这样的

注意:未定义变量:第 27 行 C:\Users\iegbulefu\Documents\My Web Sites\Personal Site1\autopopulate.php 中的备注

警告:pg_query():查询失败:错误:“GGFF”第 1 行或附近的语法错误:...no2=67677,Djointno=8,measuredlength=80,serialno=99GGFF,remar...^ 在 C:\ Users\iegbulefu\Documents\My Web Sites\Personal Site1\autopopulate.php 在第 30 行

4

2 回答 2

2

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

在您<head></head>添加此 javascript <title></title>-

<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>

然后创建一个以check.php代码命名的文件 -

<?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); 
?>

最后,在表单字段中添加id&value属性(不要更改文件,只需更新这些行)

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">

更新 10/19 由于更改代码 -

我在你的代码中放了编号的锚点,注释在最后。您可以在 2 个文件中完成所有这些操作,但第二个文件check1.php只能有 1 个数据库查询,否则您的 ajax 会出错。

自动填充.php-

<!-- #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>

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 - 将所有<?php ?>脚本移动到页面顶部。这将:(1) 清理您的代码,(2) 能够同时处理所有数据库查询。

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

3 - 要解决您的第二个问题,请after entering data into the input fields and click submit. it creates a new record in the database instead of populating the required columns in the database使用INSERT INTO ... VALUES ... ON DUPLICATE KEY UPDATE .... 如果pipeno已经在数据库中(因为它是 a Primary Key)它将UPDATE代替INSERT.

4 - 我建议保存您的errororsuccess消息 (#4a) 并将其回显在您的 html (#4b) 的顶部。

5a - 您从硬编码pipeno下拉菜单更改为使用该函数的动态下拉菜单。所以你错过了 javascript - onChange=check(); 5b - 现在你正在创建你的pipeno&onChange=check();动态,你必须改变你的check()函数来动态获取 id。

6 -$PHP_SELF无效。我认为您正在尝试$_SERVER['PHP_SELF'],但这很容易被黑客入侵,因此最好只使用action="". [6.1,6.2,6.3]

7 - 当你做你的时候,<select><option>你使用的是相同的value="",但每一个都需要不同,你才能在发布时获得价值。此外,大多数</option>'s 拼写错误为</optio>. [7.1,7.2,7.3]

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

9 - 您的前两个表格缺少结束标签 - </td></tr></table></form>。[9.1, 9.2]

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

11 - 表格id="wallthickness"2 中使用的第 3 表格用途。有关id唯一性,请参见上文。

于 2012-10-18T03:44:18.510 回答
0

If the data you want to populate the text field(s) is an attribute of your select option, I'd use jquery. Pull the selected options attributes you want to use, you can then output that to your text box with jquery also. It can be triggered by the change event on the select menue. For example..

$('#selectid').change(function() {
    var opt = $('#selectid option:selected');
    $('#text1id').val(opt.attr('attrfortext1'));
    $('#text2id').val(opt.attr('attrfortext2'));
    $('#text3id').val(opt.attr('attrfortext3'));
});

I'd argue to use Ajax to access another PHP file to parse your database and return a json encoded array with the data you want to populate the text fields with. Depending on how much data your sifting through, your method could ugly quickly if there were several attributes per option with 100's of options.

Craig

于 2012-10-17T14:25:42.040 回答