1

I need to put php variable inside php variable with javascript value, here is some of my codes that I tried but doesnt work (its file is in .php):

$rowEmail = "EmailTest";

$script = "<script type='text/javascript'>$('#reminderModal').modal('toggle'); console.log('"echo $rowEmail;"');</script>";

$script = "<script type='text/javascript'>$('#reminderModal').modal('toggle'); console.log('".echo $rowEmail."');</script>";

$script = "<script type='text/javascript'>$('#reminderModal').modal('toggle'); console.log('".$rowEmail."');</script>";
3
  • 1
    "doesnt work" ? Got only one JS line ? Commented Dec 17, 2020 at 9:57
  • <script> var email = <?=$rowEmail?>; console.log(email) </script> Commented Dec 17, 2020 at 10:02
  • This is vulnerable to JS and HTML injections (at least). Use json_encode to output literals that are safe to use in JavaScript context. Commented Dec 17, 2020 at 10:24

3 Answers 3

2

You have some misunderstanding on how to output data via PHP. Need something like:

$rowEmail = "EmailTest";

$script = "<script type='text/javascript'>console.log('". $rowEmail ."');</script>";
echo $script;
Sign up to request clarification or add additional context in comments.

Comments

1
$script = "<script type='text/javascript'>$('#reminderModal').modal('toggle'); console.log('" . $rowEmail . "');</script>";

You don't need the echos, just concatanate the string.

If it is not a must for the love of what's good in this world do not store tags in variables like that. Makes it incredibly hard to read and debug later on.

3 Comments

Isn't that the 3rd thing he did?
yes, but he didn't output(echo) the string afterwards, so nothing happened ...
It is and it works just fine. Provided he included the jQuery library properly.
-1

Your last line is correct I don't know you why say it doesn't work, I've tried it and it works :

<?php
$rowEmail = "EmailTest";
$script = "<script type='text/javascript'>$('#reminderModal').modal('toggle'); console.log('".$rowEmail."');</script>";

echo $script;
// displays <script type='text/javascript'>$('#reminderModal').modal('toggle'); console.log('EmailTest');</script>

1 Comment

Didn't work, because $script was not echoed afterwards ...

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.