我对jQuery的AJAX功能有点新意。我有一个问题,这是我的函数,它在一个文件head_menu.php上,它包含在index.php文件中。
HTML:
<a class="dock-item" href="index.php" class="post" id="menu01"><img src="images/home.png" alt="home" /><span>Home</span></a>`
<a class="dock-item" href="index.php" class="post" id="menu02"><img src="images/home.png" alt="home" /><span>Create Users</span></a> `
JQUERY:
$(document).ready(function() {
$("#menu01").click(function(event){
$.post(
"index.php",
{ menu: "main.php" },
this.html(data);
}
);
$("#menu02").click(function(event){
$.post(
"index.php",
{ menu: "create_users.php" },
this.html(data);
}
);
});
我只是尝试将一个post变量菜单发送到index.php页面来更改侧边栏菜单,当我们点击Home或Create Users时。 在index.php中我有这个。
$menu = $_REQUEST['menu'];
我该怎么做。
答案 0 :(得分:1)
数据索引是保存发送到服务器的数据。你可以这样做:
<a class="dock-item" href="create_users.php" class="post"><img src="images/home.png" alt="home" /><span>Home</span></a>
<a class="dock-item" href="main.php" class="post"><img src="images/home.png" alt="home" /><span>Create Users</span></a>
$("a.post").click(function(event){
$.ajax(
url: "index.php",
data: { menu: $(this).attr("href") },
success: function(data) {
$('selector').html(data);
}
);
event.preventDefault();
return false; /* Dont do what you have to do , dont go to href */
});
或者将href更改回index.php并将变量放在您想要的任何属性中。也许
id="create_users"
id="main"
和
data: { menu: $(this).attr("id") + ".php" },
答案 1 :(得分:-1)
<a class="dock-item" href="index.php" class="post" id="menu01"><img src="images/home.png" alt="home" /><span>Home</span></a>
<a class="dock-item" href="index.php" class="post" id="menu02"><img src="images/home.png" alt="home" /><span>Create Users</span></a>
<script>
$.customPOST = function(data,callback){
$.post('index.php',data,callback,'json');
}
$(document).ready(function() {
$("#menu01").click(function(){
$.customPOST({menu: "main.php",function(response){
//server will return an OBJECT called "response"
//in "index.php" you will need to use json_encode();
//in order to return a response to the client
//json_encode(); must have an array as argument
//the array must be in the form : 'key' => 'value'
//on the server side you will have $_POST['menu'}
});
return false;
});
$("#menu02").click(function(){
$.customPOST({menu: "create_users.php",function(response){
//server will return an OBJECT called "response"
//in "index.php" you will need to use json_encode();
//in order to return a response to the client
//json_encode(); must have an array as argument
//the array must be in the form : 'key' => 'value'
//on the server side you will have $_POST['menu'}
});
return false;
});
});
</script>