我有一个PHP代码和HTML代码。我学习了$.post
。
我想稍后使用获取结果数组的元素:FileName
,Width
,等等。
我该怎么做?
PHP代码:
<?php
$json_obj = (object) array('FileName' => "vonat.jpg", 'Width' => "155", 'Height' => "150");
$json_obj2 = (object) array('FileName' => "pum.jpg", 'Width' => "222", 'Height' => "200");
$json_obj = $json_obj2;
$json = json_encode($json_obj);
// echo $json_obj -> FileName;
//echo $json_obj -> Width;
echo $json;
return $json;
?>
HTML代码:
<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#driver").click(function(event){
$.post(
"kep.php",
{ name: "Bártfai" },
function(result) {
var imageFileName = result["FileName"];
alert(„imageFileName”+imageFileName); this is didnt working
var element=$("<div />");
element.data(„FileName”, imageFilename); I would like to build up/construct an <DIV>
element
$('#stage').html(result); this is working
//$('#stage').append(result);
}
);
},"json");
});
</script>
</head>
<body>
<p>Click on the button to load result.html file:</p>
<div id="stage" style="background-color:yellow;">
STAGE
</div>
<input type="button" id="driver" value="Load Data" />
</body>
</html>
答案 0 :(得分:0)
您想稍后在javascript中使用它吗? 如果是这种情况,只需将其放在一个全局变量中:
<script type="text/javascript" language="javascript">
var resultForLater;
$(document).ready(function() {
$("#driver").click(function(event){
$.post(
"kep.php",
{ name: "Bártfai" },
function(result) {
resultForLater = result; //Put it in the global variable so you can use it elsewhere
var imageFileName = result["FileName"];
alert(„imageFileName”+imageFileName); this is didnt working
var element=$("<div />");
element.data(„FileName”, imageFilename); I would like to build up/construct an <DIV>
element
$('#stage').html(result); this is working
//$('#stage').append(result);
}
);
},"json");
});
</script>