Hi. I’m having difficulty getting javascript to work on my page.
The site is HTML and PHP, and I’m trying to call a javascript function when the user logs in incorrectly.
Please make sure to format your posts properly. Discourse will try to format your code as text unless you wrap it in a code block.
It would be good to first narrow down whether this is a Javascript problem or a PHP problem. Can you check the page source on an error condition to see if the Javascript code is printed there? If not, it’s a PHP issue where your code doesn’t generate the code.
I’ve checked for any error output in the console of the browser but there is no error message.
I’ve enabled PHP display errors by activating Alter PHP Directives in the Control Panel.
I’ve added console messages and alerts to the javascript function but it just seems that the function is not getting called from the PHP page. I included an error message to print to the page (with print_r) when the PHP function is called and the error gets printed to the page, but the javascript does not execute.
It’s strange because the script works when I test on my local machine, but not when it’s live on the infinityfree server.
After I submit an invalid login, Maybe try to put this javascript code before </body>? Javascript cannot get the elements below itself, which means if your Javascript code is fetching a specific element you must put it after every element (except </body>)
Thanks for the suggestions.
I’ve changed the order of my include files and made sure the javascript line is below the <body>.
Unfortunately the javascript still doesn’t execute.
I will try your idea of echoing the error message with PHP. How would I target a specific <div> so the error message would echo inside the <div> ?
Ok, so I changed my error function so that my PHP script will simply echo the error message inside the
. Here is my code: <div id="loginErrorMsg"><?php echo $errors; ?></div>
The content of the message will be set by the PHP script depending on what the issue is.
My next issue now is that the error message gets printed out on to the page in addition to being inside the designated <div>.