2014-01-18 105 views
0

我有點新jQuery的AJAX功能。我有一個問題,這是我的功能,它在一個文件head_menu.php上,這是包含在index.php文件。通過POST發送jQuery變量

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); 
     } 

    ); 
    }); 

我只是想發個帖子變量菜單,到index.php頁面來改變工具欄菜單,當我們點擊主頁或創建用戶時。 在index.php我有這個。

$menu = $_REQUEST['menu']; 

我該怎麼做。

回答

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" }, 
+0

這是$阿賈克斯。不是$ .post。我們通過event,e =='undefined' –

+0

@AndreiZhamoida jQuery.post() - 描述:使用HTTP POST請求從服務器加載數據。 http://api.jquery.com/jquery.post/ 變量e是固定的。習慣來命名事件變量只是'e' – CappY

+0

$ .post是最短的$ .ajax,但語法不同 –

-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> 
+0

感謝您的回答。但在index.php我只是把'json_encode(); $ menu = $ _ POST ['menu'];'獲取變量? – Liam

+0

然後你把:json_encode(array());由於您不需要服務器的響應,因此請使用 。 獲取變量在服務器端把這個: if(isset($ _ POST ['menu'])){ $ menu = $ _POST ['menu']; //你的動作在這裏 } – phpCore