-3

I am trying to define javascript variable from php variable in the running php code.

Here is my code:

echo "<script>"; 
echo "var s1 = '<?php echo isset($s1) ? $s1 : ''  ?>';";
echo "getAirdrop();";
echo "</script>";

But this code not defining the javascript variable.

2
  • 1
    You're already in a PHP block. Adding <?php is completely unnecessary. Commented Jul 23, 2021 at 12:25
  • 1
    Does this answer your question? Passing JS variable to PHP Commented Jul 23, 2021 at 15:55

2 Answers 2

1

You don't need php tags again.

Try it like this:

echo "<script>";
echo "var s1 = '";
echo isset($s1) ? $s1 : '';
echo "';";
echo "getAirdrop();";
echo "</script>";
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it in one line, just concatenate all yours echo :

echo "<script> let s1 = ".isset($s1) ? $s1 : ''." getAirdrop(); </script>"

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.