1

I have a PHP code and I use some JavaScript code inside just for alerting some text and a variable that I defined inside my PHP code.

How can I use my PHP variable in my JavaScript code to display it with an alert?

Code:

<?php 
$a="Jhon";
$b=true;
if($b)
{
echo "
<script type=\"text/javascript\">
     alert('Hello'.$jhon);
</script>
";
}
?>
Script47
  • 14,230
  • 4
  • 45
  • 66
Majid
  • 97
  • 9
  • Should work except for an error with a `'` mark -> `alert('Hello $jhon');` - you're already using a double-quoted string so you don't need to put the concatenation in it; that's just breaking the embedded JS (view source would show: `alert('Hello'Jhon);` ... actually the variable in PHP is `$a` so replace all instances of `$jhon` with `$a` as well – CD001 May 03 '18 at 14:14

4 Answers4

1
<?php 
  $a="Jhon";
  $b=true;
?>

<script type="text/javascript">
     if(<?php echo $b; ?>){
       alert('Hello<?php echo $b; ?>');
     }
</script>
n1md7
  • 2,854
  • 1
  • 12
  • 27
0

Wrap alert to function and call it at onload for example:

<?php 
$a="Jhon";
$b=true;
if($b)
{
echo "
<script type=\"text/javascript\">
    function callAlert(){
     alert('Hello$a');
     }
</script>
";
}
?>
<body onload="callAlert()">
</body>
Alexey Usachov
  • 1,364
  • 2
  • 8
  • 15
0

JavaScript operator for joining strings is +, not .. Also, $a needs to be in quotes, since otherwise JS will think that the value of $a is an identifier. Try this:

echo "
<script>
    alert('Hello, ' + '$a')
</script>
";

Also, what is $jhon?

D. Pardal
  • 6,173
  • 1
  • 17
  • 37
  • 1
    Doesn't need the concatenation at all - it's just a PHP variable in a string so `alert('Hello {$a}');` should do it. – CD001 May 03 '18 at 14:18
-1

There are multiple ways to do that: One:

<?php
$a="Jhon";
$b=true;
if($b){ ?>

<script type="text/javascript">
    alert('Hello <?=$a?>');
</script>

<?php } ?>

Two:

<?php
$a="Jhon";
$b=true;

if($b):?>
<script type="text/javascript">
    alert('Hello <?=$a?>');
</script>
<?php endif;?>

I can give you more if you want I hope this will help

slon
  • 1,002
  • 1
  • 8
  • 12
  • Both these options are unsafe and will break if `$a` contains `'` or a new line character. – Quentin May 03 '18 at 14:21
  • One more thing, to use php short tags, they must be enabled, otherwise use alert('Hello '); – slon May 03 '18 at 14:21
  • 1
    No. `=` is always enabled and is not related to the `short_tags` directive which only covers ``. – Quentin May 03 '18 at 14:23
  • php code or any code gets broken for any some characters, the question is not about how to make characters safe. Please use proper function to encode or decode text if breaks. Of course it will break if we put garbage – slon May 03 '18 at 14:24
  • @Quentin please read question or comments carefully, otherwise you're giving wrong comments. Comments about short_tags were given additional to my answer, not to your comments. – slon May 03 '18 at 14:26
  • Short_tags are not always enabled, your comments are wrong. – slon May 03 '18 at 14:27
  • "Comments about short_tags were given additional to my answer, not to your comments" — The comment was still wrong. – Quentin May 03 '18 at 14:30
  • "Short_tags are not always enabled, your comments are wrong" — While it is true that short_tags are not always enabled, `=` is not a short tag ([unless you are using PHP 5.3 or earlier](http://php.net/manual/en/ini.core.php#ini.short-open-tag)…in which case **you have major security risks and should upgrade now**). – Quentin May 03 '18 at 14:30
  • This is exactly short_tags, where needs to be changed short_open_tag = on – slon May 03 '18 at 14:32
  • No. `` is a short tag. `=` is not. [See the documentation I already linked to](http://php.net/manual/en/ini.core.php#ini.short-open-tag). – Quentin May 03 '18 at 14:33
  • You are wrong, please don't put misleading comments. – slon May 03 '18 at 14:34
  • I've linked to supporting documentation in the PHP manual. Please stop accusing me of being wrong without providing a more authoritative source. – Quentin May 03 '18 at 14:35
  • Please read the documentation that you provided, and = both short_tags, please read it for php versions too. If the question doesn't indicate version, we have to provide all approaches for different versions. Thank you – slon May 03 '18 at 14:40
  • See my earlier comment regarding versions **where I already pointed out that `=` was covered by `short_open_tag` in ancient history**. PHP 5.3 has been end of life for **years**. It is dead. It is a security risk. Nobody should be using it. If anybody is, then they have bigger problems then the short_tags setting. In any **supported** version of PHP, `=` is not covered by `short_open_tags`. – Quentin May 03 '18 at 14:42