jQuery POST 練習 – 簡單範例

這個主要是練習使用 jQuery 做 ajax POST 的動作.然後把結果塞回網頁上面.

先簡單寫個 helloworld 的 php. 限定它只接收 POST method.

1
2
3
4
5
6
7
<?
if (isset($_POST["name"])) {
    echo “aloha “.$_POST["name"];
} else {
    echo “get out!;
}
?>

在html內弄個區塊顯示回傳值:

1
<div class=”message”></div>

jQuery 部份只要寫下面給行 codes就可以達成 :

1
2
3
4
5
6
7
8
9
<script type=”text/javascript”>
    $(document).ready(function() {
        $.post(’helloworld.php,{
            name: “sam”
        }, function(txt){
            $(’div.message).html(txt);
        });
    });
</script>

這樣就完成了簡單的 jQuery 使用 ajax 呼叫POST method. 看一下 DEMO

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.