title: PHP\HTML script to auto populate form fields when an item is selected from a dropdown list
body: <blockquote>
    <p>I created a HTML form with one drop down listing of items from
    postgresql database, I wish to have some of the fields in the form to
    populate automatically from the database when an item is selected from
    the dropdown menu. I also have some other fields on the form which
    needs to be filled-in by the users before pressing the submit button
    to push the entire data back to the database.</p>
    
    <p>Currently my script is not able to connect to the database or populate
    the drop-down listing on the form and also not able to autopopulate
    the other specified form fields (which are the attributes of the items
    in the dropdown list also stored in the database) and the second issue
    im also having is that 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. Can Someone please
    take a look at my script and help me out.</p>
    
    <p>Here is the script to create the data entry form  with a
    dropdown</p>
  </blockquote>
  
  <pre><code> &lt;?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");
  
  
  //&lt;!-- #2 --&gt;
   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']); 
   //&lt;!-- #3 --&gt;
  $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)";
  
  //&lt;!-- #4a --&gt;
   $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);
   }
   //&lt;!-- #2 --&gt;
   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']);
   //&lt;!-- #3 --&gt;
  $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''";
  
  //&lt;!-- #4a --&gt;
   $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);
   }
   //&lt;!-- #2 --&gt;
   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']);
   //&lt;!-- #3 --&gt;
  $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'";
  
  //&lt;!-- #4a --&gt;
   $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
       //&lt;!-- #5a --&gt;
       $c = ($field_name == 'pipeno') ? ' onChange="check('.$num.');"' : '';
       echo "&lt;select name=\"".$field_name."\" id=\"".$field_name.$num."\"$c&gt;\n";
       echo "&lt;option value=\"\"&gt; --- Select --- &lt;/option&gt;\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 "&lt;option value=\"$k\"&gt;$k&lt;/option&gt; \n"; }
       // Ends the Dropdown
       echo "&lt;/select&gt;\n";
   }
  ?&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;UG Pipeline Field Data Capture&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
       &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
       &lt;/head&gt;
        &lt;body&gt;
  &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  
  &lt;!-- #5b --&gt;
          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('');}
              }
    &lt;/script&gt;
            &lt;!-- #4b --&gt;
   &lt;!--&lt;?php printf($message);?&gt;--&gt;
  
   &lt;!-- #6.1 --&gt;
  &lt;form action="" method="post"&gt;
   &lt;table width="800" cellpadding= "10" cellspacing="1" border="2"&gt;
   &lt;tr align="center" valign="top"&gt;
   &lt;td align="center" colspan="1" rowspan="1" bgcolor="#00CC00"&gt;
   &lt;h3&gt;Input Field Tally Information&lt;/h3&gt;
        Select Wall Thickness:&lt;select name="wthick" id="wthick"&gt;
   &lt;!-- #7.1 --&gt;
    &lt;option value=""&gt; --Select-- &lt;/option&gt; 
    &lt;option value="9.80"&gt;  9.80  &lt;/option&gt;
    &lt;option value="13.50"&gt; 13.50 &lt;/option&gt;
    &lt;option value="15.90"&gt; 15.90 &lt;/option&gt;
    &lt;/Select&gt;           
   Select Pipe No:&lt;?php dropdown('pipeno', 1); ?&gt; HeatNo1: &lt;input type="text" name="heatno1" id="heatno1"&gt;&lt;br /&gt;&lt;br /&gt;  
   PipeNo2: &lt;input type="text" name="pipeno2" id="pipeno2"&gt; HeatNo2: &lt;input type="text" name="heatno2" id="heatno2"&gt;Joint No: &lt;input type="text" name="Djointno"&gt;&lt;br /&gt;&lt;br /&gt; 
    Input measured Length: &lt;input type="text" name="measuredlength"&gt; Input Serial No: &lt;input type="text" name="serialno"&gt;&lt;br&gt;&lt;br&gt; Remarks: &lt;input type="text" name="remarks"&gt;&lt;br&gt;&lt;br&gt; 
   &lt;!-- #8.1 --&gt;
   &lt;input type="Submit" name="submit_1" value="Submit"&gt;
   &lt;!-- #9.1 --&gt;
  &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/form&gt;
  &lt;!-- #6.2 --&gt;
   &lt;form action="" method="post"&gt;
   &lt;table width="800" cellpadding= "10" cellspacing="1" border="2"&gt;
   &lt;tr align="center" valign="top"&gt;
   &lt;td align="center" colspan="1" rowspan="1" bgcolor="#FF99FF"&gt;
   &lt;h3&gt;Input Field Bend Information&lt;/h3&gt;
    Select Wall Thickness:&lt;select name="wthick1" id="wthick1"&gt;
   &lt;!-- #7.2 --&gt;
       &lt;option value=""&gt; --Select-- &lt;/option&gt; 
       &lt;option value="9.80"&gt;  9.80  &lt;/option&gt;
       &lt;option value="13.50"&gt;13.50 &lt;/option&gt;
       &lt;option value="15.90"&gt; 15.90 &lt;/option&gt;
    &lt;/select&gt;
   &lt;!-- #10.1 --&gt;           
   Select Pipe No:&lt;?php dropdown('pipeno', 2); ?&gt;   HeatNo1: &lt;input type="text" name="heatno1" id="heatno1_2"&gt; &lt;br&gt;&lt;br&gt;
   PipeNo2: &lt;input type="text" name="pipeno2" id="pipeno2_2"&gt; HeatNo2: &lt;input type="text" name="heatno2" id="heatno2_2"&gt; Joint No: &lt;input type="text" name="Djointno"&gt; &lt;br&gt;&lt;br&gt;
   Input Measured Distance: &lt;input type="text" name="measureddistance"&gt; Input Bend Angle: &lt;input type="text" name="benddegree"&gt; &lt;br&gt;&lt;br&gt;
  Select Bend Type:&lt;select name="bendtype" id="bendtype"&gt;
      &lt;option value=""&gt; --Select-- &lt;/option&gt; 
      &lt;option value="Combo"&gt;Combo&lt;/option&gt;
      &lt;option value="SAG"&gt;SAG&lt;/option&gt;
      &lt;option value="OB"&gt;OB&lt;/option&gt;
      &lt;option value="SBRT"&gt;SBRT&lt;/option&gt;
      &lt;option value="SBLT"&gt;SBLT&lt;/option&gt;
      &lt;option value="HBLT"&gt;HBLT&lt;/option&gt;
      &lt;option value="HBRT"&gt;HBRT&lt;/option&gt;&lt;p&gt;&lt;/p&gt; 
    Remarks: &lt;input type="text" name="remarks"&gt;&lt;br&gt;&lt;/br&gt;
  
    &lt;input type="Submit" name="submit_2" value="Submit"&gt; 
   &lt;!-- #9.2 --&gt;
  &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/form&gt;
  &lt;!-- #6.3 --&gt;
  
  &lt;form action="" method="post"&gt;
   &lt;table width="800" cellpadding= "10" cellspacing="1" border="2"&gt;
   &lt;tr align="center" valign="top"&gt;
   &lt;td align="center" colspan="1" rowspan="1" bgcolor="#99FF33"&gt;
   &lt;h3&gt;Input App. Tally Information&lt;/h3&gt;
   &lt;!-- #11 --&gt;
         Select Wall Thickness:&lt;select name="wthick2" id="wthick2"&gt;
   &lt;!-- #7.3 --&gt;
       &lt;option value=""&gt; --Select-- &lt;/option&gt; 
       &lt;option value="9.80"&gt;  9.80  &lt;/option&gt;
       &lt;option value="13.50"&gt; 13.50 &lt;/option&gt;
       &lt;option value="15.90"&gt; 15.90 &lt;/option&gt;
    &lt;/select&gt;
   &lt;!-- #10.2 --&gt;             
   Select Pipe No:&lt;?php dropdown('pipeno', 3); ?&gt;  &lt;br&gt;&lt;br&gt; Input Tally Type: &lt;input type="text" name="tallytype"&gt;
   Input Tally Qty: &lt;input type="text" name="qty"&gt;&lt;br&gt;&lt;/br&gt;  Input Serial No: &lt;input type="text" name="serialno"&gt; 
   RefID: &lt;input type="text" name="referenceid"&gt;&lt;br&gt;&lt;/br&gt;
   &lt;!-- #8.3 --&gt;
   &lt;input type="Submit" name="submit_3" value="Submit"&gt;
   &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <blockquote>
    <p>Php Script to load data onto the form field and push data back from
    the database - filename = check1.php</p>
  </blockquote>
  
  <pre><code>&lt;?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'=&gt;$row[1],'pipeno2'=&gt;$row[2],'heatno2'=&gt;$row[3],'jointno'=&gt;$row[4]);
  $row_info = json_encode($row_info);
  print_r($row_info); 
  ?&gt;
  </code></pre>
  
  <blockquote>
    <p>the errors I get when I hit the submit button with or without data is
    this</p>
    
    <p>Notice: Undefined variable: remark in C:\Users\iegbulefu\Documents\My
    Web Sites\Personal Site1\autopopulate.php on line 27</p>
    
    <p>Warning: pg_query(): Query failed: ERROR: syntax error at or near
    "GGFF" LINE 1:
    ...no2=67677,Djointno=8,measuredlength=80,serialno=99GGFF,remar... ^
    in C:\Users\iegbulefu\Documents\My Web Sites\Personal
    Site1\autopopulate.php on line 30</p>
  </blockquote>