0

I have a 2 line script I have been using and adjusting to redirect pages using the post method. All of a sudden I am getting "Uncaught TypeError: Cannot read property 'innerHTML'" error. This code is in a php record maintenance page where I adjust the post fields to return to the calling page after the record maintenance. This code is probably used over 70 times without getting this error. Any suggestions why I would get this now?

    if($_POST['maint'] == "inventory" && $_POST['rec'] == "edit") {
   $pro=$conn->query("Select caseqty From products Where idpro=" . $_POST['spro'])->fetch();   
   $oh=($pro['caseqty'] * $_POST['ohc']) + $_POST['ohb'];

   $str=$conn->prepare("Update products Set location=:loc, onhand=:oh, barcodecase=:bcc, barcodebottle=:bcb Where idpro=:id");
   $str->bindParam(':loc', $_POST['loc'], PDO::PARAM_STR);        
   $str->bindParam(':oh', $oh, PDO::PARAM_STR);        
   $str->bindParam(':bcc', $_POST['bcc'], PDO::PARAM_STR);        
   $str->bindParam(':bcb', $_POST['bcb'], PDO::PARAM_STR);        
   $str->bindParam(':id', $_POST['spro'], PDO::PARAM_STR);        
   $str->execute();?>
   <script language='javascript'>
      document.body.innerHTML += '<form id="dynForm" action="office.php" method="post"><input type="hidden" name="pg" value="office"><input type="hidden" name="spg" value="inventory"><input type="hidden" name="client" value="<?php echo $_POST['client'];?>"><input type="hidden" name="spro" value="<?php echo $_POST['spro'];?>"></form>';
      document.getElementById('dynForm').submit();
   </script> <?php 
}
  • 1
    What is the full error message please? Usually there's a bit after _"Cannot read property 'innerHTML'"_ like _"of ..."_ – Phil Mar 31 '20 at 03:38
  • I suspect that document.body.innerHTML is the wrong code that you've written. Give an id to the body and change that code to document.getElementById.innerHTML – noobprogrammer Mar 31 '20 at 03:41
  • Here's the full error... Uncaught TypeError: Cannot read property 'innerHTML' of null at rec_maint.php:5 – wolfpride71 Mar 31 '20 at 03:45
  • This code is actually copied, pasted and fields are adjusted (like action="warehouse.php" or instead of client the input would be customer). This exact code has been working on a number of pages without this error. – wolfpride71 Mar 31 '20 at 03:49
  • 1
    Please provide the fully rendered page, or at least a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – kmoser Mar 31 '20 at 03:55
  • I modified the original post. – wolfpride71 Mar 31 '20 at 04:01
  • 1
    Ah, I bet this code is in the `` of your document. You can't access `document.body` there – Phil Mar 31 '20 at 04:04
  • HA! I'm starting a new site and the previous maint pages have the tags. I tried the head tag no luck, changed to body and it's working!!! Thank you guys. Stay safe, stay healthy! – wolfpride71 Mar 31 '20 at 04:11

0 Answers0