1

I am trying to pull a string variable into my javascript function from a PHP file and am being returned: Uncaught SyntaxError: Unexpected end of input.

I have tried to use onclick="showContent('home')" but this does not load the page at all. I assumed that placing home in parentheses would act as a string parameter and fill that variable in the javascript function.

In my header.php: echo "<img src='../images/home.png' onclick='showContent('home')' alt='Home' /></a>";

In my menu.js:

function showContent(showThis){ alert(showThis); }

7
  • 2
    Possible duplicate of "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP Commented Oct 15, 2019 at 19:19
  • Double-check where you have $home defined, it's not able to use the variable. Commented Oct 15, 2019 at 19:29
  • So your question has nothing to do with PHP now? Commented Oct 15, 2019 at 19:58
  • @PatrickQ No, it still does. The header.php file is where the html is being generated from. Commented Oct 15, 2019 at 20:00
  • That doesn't matter, your question has no PHP code in it. Commented Oct 15, 2019 at 20:01

2 Answers 2

0

Try like this:

echo "<img src='../images/home.png' onclick='showContent(\"$home\")' alt='Home' />";
Sign up to request clarification or add additional context in comments.

3 Comments

This shows up as empty. And when I click view source code it appears like this in the final HTML: onclick='showContent("")'
@FrankDoe Then $home is empty. You should include (in your question) the code that is supposed to be assigning a value to $home
Updated my question. My apologies.
0

Try this. You are not interpolating the variable $home correctly into the JS.

echo "<img src=\"../images/home.png\" onclick=\"showContent('{$home}')\" alt=\"Home\" />";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.