Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php echo link including php variable

Status
Not open for further replies.

peterpann

Programmer
Jun 19, 2007
63
GB
How can php write a link to call a javascript function which uses a php variable ?
In this simple example I want the alert box to show the value (222) of the variable, but I cannot get beyond result1 showing the alert with "$myvar", or
result2 showing the alert with the variable but without a link.

<?php
$myvar = "222";

$link = 'alert("$myvar")';
$result1 = "";
$result1 .= "<a href='javascript:";
$result1 .= $link;
$result1 .= " '>";
$result1 .= $link;
$result1 .= "</a>";

$result2 = "";
$result2 .= "<script type='text/javascript'>";
$result2 .= "alert('$myvar');";
$result2 .= "</script>";
?>

<html>
<head>
</head>
<body>

<?php
echo $result1;
echo $result2;
?>

</body>
</html>
 
Hi

Let us cut the complications for now :
PHP:
[teal]<?php[/teal]
[navy]$myvar[/navy] [teal]=[/teal] [i][green]"s'p\"e<c&i;a>l"[/green][/i][teal];[/teal]
[teal]?>[/teal]
[b]<html>[/b]
[b]<head>[/b]
[b]</head>[/b]
[b]<body>[/b]
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][i][green]"javascript:alert(<?=htmlentities(json_encode($myvar))?>)"[/green][/i][b]>[/b]Huh ?[b]</a>[/b]
[b]</body>[/b]
[b]</html>[/b]
Note that you can [tt]alert()[/tt] a number without much care, but if that value could ever contain something else than digits, better encode it properly.


Feherke.
feherke.github.io
 
Thanks feherke. I guessed it would be you who came to my rescue.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top