writing JavaScript with PHP
writing JavaScript with PHP
我正在尝试纠正一些JavaScript来使用PHP定义变量。 该变量中有一点jQuery,应该由客户端呈现。 我说"应该",但实际上的意思是我想要它。我应该怎么做才能使 o2 与 o1 相同(即隐藏 jQuery(?
谢谢!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title></title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script>
$(function() {
var o1={"foo":{"bar": "abc/index.php?="+$("#id").val()}};
console.log(o1);
<?php
$d='abc/index.php';
$json='{"foo":{"bar": "'.$d.'?id='"+$('"#id'").val()"}}';
//echo($json);
$json=json_decode($json);
//echo(print_r($json,1));
$json=json_encode($json);
//echo($json);
echo("var o2=".$json.";");
?>
console.log(o2);
});
</script>
</head>
<body>
<input type="hidden" id="id" value="123" />
</body>
</html>
你有这个:
<input type="hidden" id="id" value="123" />
所以我可以假设您是从数据库获取该 id,对吗?也许是这样的:
<input type="hidden" id="id" value="<?php echo $row[id];?>" />
如果是这种情况,请执行以下操作(我测试并有效(:
您不需要隐藏的输入来执行此操作。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title></title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script>
$(function() {
<?php
//From your SQL query you obtained
//$row['id'] = 123;
$id = $row['id'];
?>
var o1={"foo":{"bar": "abc/index.php?=" + <?php echo $id ?>}};
console.log(o1);
<?php
$d='abc/index.php';
$json='{"foo":{"bar": "'.$d.'?='.$id.'"}}';
//echo($json);
$json=json_decode($json);
//echo(print_r($json,1));
$json=json_encode($json);
//echo($json);
echo("var o2=".$json.';');
?>
console.log(o2);
});
</script>
</head>
<body>
</body>
</html>
未经测试,可能包含错误:
<script>
$(function() {
var o1={"foo":{"bar": "abc/index.php?="+$("#id").val()}};
console.log(o1);
<?php
$d='abc/index.php';
$json='{"foo":{"bar": "' . $d . '?="+$("#id").val()}}';
echo("var o2=".$json.';');
?>
console.log(o2);
});
</script>
我更喜欢用json_encode制作json,以避免json的解析和验证出现意外问题。
json_encode 和 json_decode仅适用于 UTF-8,如果另一个字符集具有特殊字符,则会导致空白字符串。
<script>
<?php
$d = "abc/index.php";
$json['foo']['bar'] = $d . '?id=$("#id").val()';
echo "var o2 = ".json_encode($json);
?>
console.log(o2);
</script>
相关文章:
- timeago.js with datatable and PHP
- PHP $_GET with jQuery $.get call
- PHP Error with Javascript
- JavaScript with PHP SESSION
- Ajax Call with Post in PHP
- chartJs with MySQL and PHP
- HTML用PHP替换with函数
- InnerHTML with PHP
- Ajax call VERY SLOW with PHP
- jQuery load with php in it?
- Wordpress: POST to .php with Ajax
- Javascript array to PHP with json.stringify
- Call PHP with JavaScript
- js/php with include files
- Javascript array to php with jQuery ajax
- PHP with Javascript - What is the scope of PHP variables ins
- PHP with javascript
- Using TeeChart PHP with javascript
- GET id from php with javascript
- Javascript value to PHP with Jquery