[英]Sending jquery variable by POST
I'm kinda new with jQuery's AJAX functions. 我有点喜欢jQuery的AJAX函数。 I'm having a problem, this my function, its on a file head_menu.php, thats is included on the index.php file.
我遇到了问题,这是我的函数,它在文件head_menu.php中,即包含在index.php文件中。
HTML: 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: 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);
}
);
});
I'm just trying to send a post variable menu, to the index.php page to change the sidebar menu, this when we click on Home or Create Users. 我只是想将post变量菜单发送到index.php页面,以更改侧边栏菜单,这是当我们单击Home或Create Users时。 In the index.php I have this.
在index.php中,我有这个。
$menu = $_REQUEST['menu'];
How do I do this. 我该怎么做呢。
data index is holding data sent to the server. 数据索引保存发送到服务器的数据。 You could do that:
您可以这样做:
<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 */
});
Or change href back to index.php and put variable in any attribute you want. 或将href改回index.php并将变量放在所需的任何属性中。 Maybe
也许
id="create_users"
id="main"
and 和
data: { menu: $(this).attr("id") + ".php" },
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.