jQuery 與 PHP 之間互傳值使用 post

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

helloworld.php
<?php
if (isset($_POST["name"])) {
    echo "hello ".$_POST["name"];
} else {
    echo "get out!";
}
?>

jQuery 部份只要寫下面給行 codes就可以達成,完整程式碼如下 :

jquerypost.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>jQuery POST PHP</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    $.post('helloworld.php',{
      name: "world"
    },function(txt){
      $('div.message').html(txt);
    });
  });
</script>
</head>
<body>
<h2>jQuery POST PHP</h2>

<div class="message"></div>

</body>
</html>

留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

PHP 與 JavaScript 之間傳值利用 json