這兩天學了學PHP,發現PHP真是個好東西,免費開源又簡單,就一個字:爽! 學完了PHP的基本語法想找個PHP的模板學學,baidu,google了半天,發現了smarty,phplib等將近25個引擎,可真不少啊…… 到底要學哪個呢? 別忙,停停再說吧,因爲我有了個新的想法: 用PHP加上前陣子學的jQuery怎麼樣呢?小試一把....
1.jquery.js 此處下載: http://code.google.com/p/jqueryjs/
2.目錄結構:
html目錄中存放html.html文件
inc目錄中存放 jQuery.php文件
js目錄中存放jQuery.js文件
主目錄下存放html.php文件
3.內容:
html.html文件:
<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>PHP+jQuery組合測試</title><style>h5{color:#CC3333;margin:0px;padding:0px;}#test{ font:bold 14px; color:#003366; cursor:pointer;}</style></head><body><h5>PHP+jQuery超級組合實驗2:會話與參數傳遞</h5><hr/><div id="user"></div><form method="post" action="html.php" id="form1" name="form1"><label for="name"><input id="name" name="name" maxlength="20"/><br/><label for="age"><input id="age" name="age" maxlength="3"/><br/><input type="submit" name="submit" value="提交"/></form><h5>PHP+jQuery超級組合實驗1:簡單測試</h5><hr/><div id="test">Hello World! Click me please!</div><p id="pinfo">hello.</p></body></html>
jQuery.php
<?phpfunction jQinit($html){ if($html != NULL && $html != "") { $html=file_get_contents($html); echo $html; } /*echo "<script language='javascript' src='js/jQuery.js'></script>";*/ $jqstr=file_get_contents("js/jQuery.js"); echo "<script language='javascript'>"; echo $jqstr; echo "</script>";}//參數$jstr:jQuery腳本字符串;$html模板頁面路徑.function jQuery($jstr){//jQuery腳本$jQuery=<<<EOT<script language="javascript"> $("document").ready(function(){ $jstr; });</script> EOT;//輸出具體jQuery內容echo $jQuery;}?>
html.php
<?php session_start();?><?php//引用jQuery模塊require("inc/jQuery.php");//初始化本頁對應的htmljQinit("html/html.html");?><?php//PHP+jQuery超級組合實驗2:會話與參數傳遞$jq="";$name="";$age="";if(
{1}
POST["name"] != "" &&
{1}
POST["age"] != ""){ $name=
{1}
POST["name"]; $age=
{1}
POST["age"];
{1}
SESSION["name"] = $name;
{1}
SESSION["age"] = $age;}if(
{1}
SESSION["name"] != "" &&
{1}
SESSION["age"] != ""){$jq=<<<EOT $("#user").text("您的名字:{
{1}
SESSION['name']} 年齡:{
{1}
SESSION['age']}");EOT;//調用jQueryjQuery($jq);}?><?php//PHP+jQuery超級組合實驗1:簡單測試$info="確實不錯!";$jQuery=<<<EOT //alert("測試成功!"); $("#test").click(function(){ alert("拉登,您好."); }); $("#pinfo").text("今天天氣不錯啊!{$info}");EOT;//調用jQueryjQuery($jQuery);?>
效果預覽: