1

Here's the code:

                <nav>
                  <ul id="alphabet" class="pagination">
                    <li><a href="#" onClick="alert(#);">#</a></li>
                    <?php
                        $letterArray = str_split(strtoupper("abcdefghijklmnopqrstuvwxyz"));
                        foreach ($letterArray as $s) {
                            echo '<li><a href="#" onClick="alert('.$s.');">'.$s.'</a></li>';
                        }
                    ?>
                  </ul>
                </nav>

When I click a letter, I get, for example:

Uncaught ReferenceError: N is not defined

And if I surround $s inside alert with '' or "", it just alerts "$s". How can I "transfer" this variable to Javascript as an actual string instead of the variable literally?

1
  • Just look at the actual HTML/Javascript code that your script has generated, and the error will probably be clear right away. Now it's going to be a guessing game about the value of $s, and what the outcome might be. Commented Dec 4, 2015 at 6:46

3 Answers 3

2

Your foreach code should be:

foreach ($letterArray as $s) {
                            echo '<li><a href="#" onClick="alert(\''.$s.'\');">'.$s.'</a></li>';
                        }

You are passing $s without single quote or double quote. So When you click on your link then its consider as function.

So you can pass $s as string with backslash(\).

Sign up to request clarification or add additional context in comments.

Comments

1

You need to give to JavaScript this string:

alert("N")

To achieve this, you want to print

alert("

then output the value of the variable $s, then output

")

This line will do it:

echo '<li><a href="#" onClick="alert("'.$s.'");">'.$s.'</a></li>';

Alternately (and better, in my opinion) is to output json_encode of the value, which will automatically add quotes to strings, and also escape any characters which should be escaped (like quotes):

echo '<li><a href="#" onClick="alert('.json_encode($s).');">'.$s.'</a></li>';

json_encode is very useful when passing data to JavaScript, as it can also handle arrays and numbers, not just strings.

Comments

0

I would rather do it this way

                <nav>
                  <ul id="alphabet" class="pagination">
                    <li><a href="#" onClick="alert(#);">#</a></li>
                    <?php
                        $letterArray = str_split(strtoupper("abcdefghijklmnopqrstuvwxyz"));
                        foreach ($letterArray as $s) { ?>
                            <li><a href="#" onClick="alert('<?=$s?>');"><?=$s?></a></li>';
                   <?php } ?>
                  </ul>
                </nav>

3 Comments

This is an alternate way of doing exactly the same thing that OP did, and will result in exactly the same error that OP's code did, since you don't output any quotes either.
missing quotes here alert("<?=$s?>");
Oh my bad sorry i missed quotes

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.