PostgreSQL
 sql >> Teknologi Basis Data >  >> RDS >> PostgreSQL

Skrip PHP\HTML untuk mengisi bidang formulir secara otomatis saat item dipilih dari daftar tarik-turun

Saya akan melakukan ini dengan menggunakan Ajax dan JQuery. Anda perlu melakukan 3 hal - Tambahkan kode Ajax/JQuery di head dokumen, buat file php untuk menghubungkan Ajax, dan tambahkan onChange , id &value atribut ke bidang.

Di <head></head> . Anda tambahkan javascript ini setelah <title></title> . Anda -

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

Kemudian buat file bernama check.php dengan kode -

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

Dan terakhir, tambahkan id &value atribut ke bidang formulir Anda (Jangan ubah file Anda, cukup perbarui baris ini)

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

DIPERBARUI 19/10 Karena kode yang diubah -

Saya telah menempatkan jangkar bernomor dalam kode Anda, dan catatannya ada di akhir. Anda dapat melakukan ini semua dalam 2 file, tetapi file kedua check1.php , hanya dapat memiliki 1 kueri basis data, atau Anda akan mengalami kesalahan di ajax Anda.

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

Inilah masalahnya-

1 - Pindahkan semua <?php ?> skrip ke bagian atas halaman. Ini akan:(1) membersihkan kode Anda, (2) dapat memproses semua kueri basis data secara bersamaan.

2 - Permintaan Anda untuk memperbarui fieldtally1 sekarang selesai sebelum Anda mendapatkan dropdown dari database.

3 - Untuk memperbaiki masalah kedua Anda 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 gunakan INSERT INTO ... VALUES ... ON DUPLICATE KEY UPDATE ... . Jika pipeno sudah ada di database (karena ini adalah Primary Key ) akan UPDATE bukannya INSERT .

4 - Saya sarankan untuk menyimpan error Anda atau success pesan (#4a) dan gema di bagian atas html Anda (#4b).

5a - Anda berubah dari kode keras pipeno dropdown, kembali ke dropdown dinamis menggunakan fungsi. Jadi Anda kehilangan javascript - onChange=check(); 5b - sekarang Anda membuat pipeno &onChange=check(); secara dinamis, Anda harus mengubah check() berfungsi untuk mendapatkan id secara dinamis juga.

6 - $PHP_SELF tidak valid. Saya pikir Anda mencoba $_SERVER['PHP_SELF'] , tapi ini mudah diretas, jadi lebih baik gunakan action="" . [6.1,6.2,6.3]

7 - Saat melakukan <select><option> Anda menggunakan value="" yang sama , tetapi masing-masing perlu berbeda agar Anda mendapatkan nilai saat memposting. Juga, sebagian besar </option> 's salah eja sebagai </optio> . [7.1,7.2,7.3]

8 - Jika Anda memiliki 3 formulir pada halaman yang sama, masing-masing harus memiliki nama yang berbeda, atau Anda tidak akan dapat membedakan tombol kirim mana yang diklik. [8.1,8.2,8.3]

9 - Dua formulir pertama Anda tidak memiliki tag penutup - </td></tr></table></form> . [9.1, 9.2]

10 - Dalam formulir 2 &3 Anda, Anda menggunakan id 's yang digunakan dalam bentuk 1. Ini tidak valid karena setiap id harus unik. [10.1,10.2]

Formulir 11 - 3 menggunakan id="wallthickness" yang digunakan dalam formulir 2. lihat di atas tentang id keunikan.



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. PG::InvalidParameterValue:ERROR:nilai tidak valid untuk parameter client_min_messages:panic

  2. buka bungkus array postgresql menjadi baris

  3. Memantau Distribusi Percona untuk PostgreSQL - Metrik Utama

  4. Bagaimana cara meningkatkan ke PostgreSQL 11 untuk Ubuntu 18.04?

  5. SQL mengeluarkan data dari BEGIN; ...; AKHIR; blokir dengan python