你好我有一个简单的ajax调用,但我看不到结果。我究竟做错了什么 ?谢谢。
的index.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
function prova(SelectedFriend){
$.post("result.php", {Selected:Selected});
return false;
}
</script>
</head>
<body>
<?
$user="name1";
?>
<div onclick="prova(<? echo $user; ?>)" style="cursor:pointer;">
<? echo $user; ?>
</div>
<?
$user="name2";
?>
<div onclick="prova(<? echo $user; ?>)" style="cursor:pointer;">
<? echo $user; ?>
</div>
<?
$user="name3";
?>
<div onclick="prova(<? echo $user; ?>)" style="cursor:pointer;">
<? echo $user; ?>
</div>
<div id="Result"></div>
</body>
</html>
和result.php
<?
echo $_POST['Result'];
?>
我的浏览器控制台说&#34; Uncaught ReferenceError:name1未定义&#34;当我点击name1。
答案 0 :(得分:1)
$user
必须放在简单的引号中:
<div onclick="prova('<? echo $user; ?>')" style="cursor:pointer;">
此外,您的函数未使用该参数:
function prova(SelectedFriend){
$.post("result.php", {SelectedFriend:SelectedFriend});
return false;
}
还必须更正result.php
文件:
<?php
echo $_POST['SelectedFriend'];