如何将变量放入javascript标记中?

时间:2012-08-27 20:49:02

标签: php mysql mysqli

我设法让这部分代码正常工作。

变量会毫无问题地回应,但是一旦我尝试将它们放入javascript标记中,它们就会停止工作。

我做错了什么?

    <?php
$id_1 = $_GET['id'];
$tag_id = "tag_id";
$Activity_Tag_String = "Activity_Tag_String";
$group_tag_string = "group_tag_string";

$link = mysqli_connect("localhost", "username", "password", "database");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT $tag_id, $Activity_Tag_String, $group_tag_string, advertiser_id FROM tbl_tags WHERE advertiser_id = '$id_1'";
$result = mysqli_query($link, $query);


?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Mate Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head> 
<body>
<script type="text/javascript">
var branch = window.location.href;

var axel = Math.random() + "";
var a = axel * 10000000000000;
document.write('<iframe src="http://fls.doubleclick.net/activityi;src=<?php $tag_id?>;type=<?php $Activity_Tag_String?>;cat=<?php $group_tag_string?>;u1='';ord=' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>');

</script>
</body>
</html>

2 个答案:

答案 0 :(得分:6)

<?php $tag_id?>

应该是

<?= $tag_id ?>

<?php echo $tag_id ?>

您的版本只是简单地执行PHP的等价:

$tag_id;

这是一个无所事事的陈述。你需要实际回显该变量的内容。

同样,请注意您的代码容易受到SQL injection attacks的攻击,并且您应该在执行任何其他操作之前解决此问题。

答案 1 :(得分:0)

对于js脚本部分尝试:

<?php echo $variable;?>

或简而言之

<?=$var?>