Can't get javascript to work

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.

Here is my site: http://op-prototype.infinityfreeapp.com

Here is my code block that invokes javascript:

<?php
function show_errors($errors){
	?> 
		<script type="text/javascript" src="js/footerJS.js">errMsg(); </script>
	<?php
}
?>

Thanks

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.

On line 119 of the (rendered) page, I see this code:

<script type="text/javascript" src="js/footerJS.js">errMsg();</script>

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

And why don’t you use this instead?

<?php
function show_errors($errors){
		echo "Login unsuccessful. Please re-enter credentials.";
}
?>
2 Likes

Another strange behaviour is that I see the line of javascript that calls the function gets printed on to html page after form is sent.

This line of javascript gets printed twice in a row.

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

You can put <?php and ?> everywhere inside html. like this:

<div class='error'><? php show_errors(); ?></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>.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.