php的無刷新操作實現(xiàn)方法分析
本文實例講述了php的無刷新操作實現(xiàn)方法。分享給大家供大家參考,具體如下:
方法一:
我們通過http的204狀態(tài)碼,頁面不跳轉(zhuǎn)。
1.html代碼如下:
<!DOCTYPE HTML><html lang='zh-CN'><head> <meta charset='UTF-8'> <title></title></head><body> <a href='http://m.lshqa.cn/bcjs/add.php' rel='external nofollow' >投票</a></body></html>
add.php代碼如下:
<?php$num = file_get_contents(’./num.txt’);$num = intval($num) + 1;file_put_contents(’./num.txt’, $num);header(’HTTP/1.1 204 No Content’);
方法二:
利用圖片加載的特性,來完成請求。
<!DOCTYPE HTML><html lang='zh-CN'><head> <meta charset='UTF-8'> <title></title></head><body> <input type='button' value='投票' /> <div id='request'></div></body><script type=''> var addBtn = document.getElementById('addBtn'); addBtn.onclick = function() { //創(chuàng)建img標(biāo)簽 var img = document.createElement('img'); //設(shè)置標(biāo)簽src屬性 img.setAttribute('src', 'add.php'); document.createElement('request').appendChild(img); };</script></html>
方法三:
利用css,javascript的加載特性,完成請求,原理與img加載一樣。
方法四:
利用iframe的特性
2.html代碼如下:
<!DOCTYPE HTML><html lang='zh-CN'><head> <meta charset='UTF-8'> <title></title></head><body> <form action='ret.php' method='post' target='request'> 用戶名:<input type='text' name='uname' value='' /> 密碼:<input type='password' name='upwd' value='' /> <input type='submit' name='submit' value='提交' /> </form> <iframe frameborder='0' name='request'></iframe> <div id='result'></div></body></html>
ret.php代碼如下:
<?php$uname = !empty($_POST[’uname’]) ? $_POST[’uname’] : ’’;$upwd = !empty($_POST[’upwd’]) ? $_POST[’upwd’] : ’’;if($uname == ’admin’ && $upwd == ’123456’) { echo '<script>parent.document.getElementById(’result’).innerHTML=’OK’;</script>';} else { echo '<script>parent.document.getElementById(’result’).innerHTML=’NO’;</script>';}
我們通過設(shè)置form提交的target到iframe,使表單無跳轉(zhuǎn)。
ajax能實現(xiàn)文件上傳嗎?
分析,文件上傳,是需要客戶端把文件內(nèi)容發(fā)送到服務(wù)器,也就是XHR對象在POST數(shù)據(jù)時,把文件內(nèi)容也發(fā)送給服務(wù)器。也就是XHR對象能夠獲取你要上傳的文件內(nèi)容,但是出于安全的考慮,JS是無法獲取本地文件內(nèi)容的。
ajax插件是如何實現(xiàn)文件上傳的?
1、iframe2、flash實現(xiàn),如swfupload3、html5 (添加了文件讀取api,使ajax上傳文件成為可能。)
更多關(guān)于PHP相關(guān)內(nèi)容可查看本站專題:《PHP+ajax技巧與應(yīng)用小結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章:
