0

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>";
  • 1
    "doesnt work" ? Got only one JS line ? – Syscall 2 hours ago
  • <script> var email = <?=$rowEmail?>; console.log(email) </script> – Adam 2 hours ago
  • This is vulnerable to JS and HTML injections (at least). Use json_encode to output literals that are safe to use in JavaScript context. – Peter 1 hour ago
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.

| improve this answer | |
  • Isn't that the 3rd thing he did? – loicEzt 1 hour ago
  • yes, but he didn't output(echo) the string afterwards, so nothing happened ... – Angel Deykov 1 hour ago
  • It is and it works just fine. Provided he included the jQuery library properly. – Trukken 1 hour ago
  • This works! thanks – Martney Acha 1 hour ago
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;
| improve this answer | |
0

I don't like this way of mixing things. But if I had to I would do:

<?php
//some php content
$rowEmail = "EmailTest";
?>

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

You can read also about short-hand tags <?=$var;?> here

| improve this answer | |
-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>
| improve this answer | |
  • Didn't work, because $script was not echoed afterwards ... – Angel Deykov 1 hour ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.