0

I have a form submit in which if an error occurs then i want the border of the text element to be red.

if(isset($_POST["forgotsubmit"])){
if(!empty($_POST["forgotemail"])){      
    else{
        echo "<script>document.getElementById('forgotemaill').style.border= 'border: 2px solid red';</script>";
    }
}
}

Now after this in console shows

forgot-password:61 Uncaught TypeError: Cannot read property 'style' of null
at forgot-password:61
(anonymous) @ forgot-password:61
  • In your actual code did you really type an extra "l" on the end of "forgotemail"? – Pointy Jan 29 '18 at 13:10
  • @Ashish Choudhary Just looking at your javascript syntax. Try dropping "border: " – Rick Riggs Jan 29 '18 at 13:10
  • @Pointy Yes its is forgotemaill only – Ashish Choudhary Jan 29 '18 at 13:46
  • @Community I believe that this was marked Duplicate out of context. The OP here is no doubt doing a server side check of a textbox, and from what I can glean, there is an existing label on the page hence the extra 'l' at the end. Meaning the answer in the duplicate (provided)[https://stackoverflow.com/questions/19235345/javascript-typeerror-cannot-read-property-style-of-null] has nothing to do with solving this particular question, as the Element already exists on the page. There are different reasons you can receive this error. Please un-mark this as a duplication. – Rick Riggs Jan 30 '18 at 16:47

2 Answers2

0

The current error indicates that it cannot find an element. If you look closely you will notice there is a typo in the JS

document.getElementById('forgotemaill') <-- should this be forgotemail ?

Darren
  • 76
  • 1
  • 6
0

Just looking at your javascript syntax. Try dropping "border: "

echo "<script>document.getElementById('forgotemaill').style.border= '2px solid red';</script>";

You can try this instead:

echo "<script>document.getElementById('forgotemaill').setAttribute('style', 'border: 2px solid red');</script>";

This should work, as it will force a style to exist on the object.

Your original is assuming that 'style' exists and you are able to change it's value.

Rick Riggs
  • 336
  • 2
  • 12