php HTML無刷新提交表單
來源:易賢網(wǎng) 閱讀:1417 次 日期:2016-08-27 13:25:38
溫馨提示:易賢網(wǎng)小編為您整理了“php HTML無刷新提交表單”,方便廣大網(wǎng)友查閱!

通常對于無刷新提交表單,我們都是運用ajax實現(xiàn)的。前段時間跟著老大了解到另一種無刷新提交表單的方法,是利用iframe框架實現(xiàn)的?,F(xiàn)在整理出來分享給大家。

第一種:

html頁面

<!DOCTYPE HTML>

<html lang="en-US">

<head>

  <meta charset="utf-8">

  <title>無刷新提交表單</title>

  <style type="text/css">

    ul{ list-style-type:none;}

  </style>

</head>

<body>

  <iframe name="formsubmit" style="display:none;">

  </iframe>

  <!-- 將form表單提交的窗口指向隱藏的ifrmae,并通過ifrmae提交數(shù)據(jù)。 -->

  <form action="form.php" method="POST" name="formphp" target="formsubmit">

    <ul>

      <li>

        <label for="uname">用戶名:</label>

        <input type="text" name="uname" id="uname" />

      </li>

      <li>

        <label for="pwd">密 碼:</label>

        <input type="password" name="pwd" id="pwd" />

      </li>

      <li>

        <input type="submit" value="登錄" />

      </li>

    </ul>

  </form>

</body>

</html>

PHP頁面:form.php

<?php

 //非空驗證

 if(empty($_POST['uname']) || empty($_POST['pwd']))

 {

  echo '<script type="text/javascript">alert("用戶名或密碼為空!");</script>';

  exit;

 }

 //驗證密碼

 if($_POST['uname'] != 'jack' || $_POST['pwd'] != '123456')

 {

  echo '<script type="text/javascript">alert("用戶名或密碼不正確!");</script>';

  exit;

 } else {

  echo '<script type="text/javascript">alert("登錄成功!");</script>';

  exit;

 }

第二種:

html頁面

<!DOCTYPE HTML>

<html lang="en-US">

<head>

  <meta charset="utf-8">

  <title>iframe提交表單</title>

</head>

<body>

  <iframe name="myiframe" style="display:none;" onload="iframeLoad(this);"></iframe>

  <form action="form.php" target="myiframe" method="POST">

   用戶名:<input type="text" name="username" /><br/>

   密 碼:<input type="password" name="userpwd" /><br/>

    

   <input type="submit" value="登錄" />

  </form>

  <script type="text/javascript">

   function iframeLoad(iframe){

    var doc = iframe.contentWindow.document;

    var html = doc.body.innerHTML;

    if(html != ''){

     //將獲取到的json數(shù)據(jù)轉(zhuǎn)為json對象

     var obj = eval("("+html+")");

     //判斷返回的狀態(tài)

     if(obj.status < 1){

      alert(obj.msg);

     }else{

      alert(obj.msg);

      window.location.;

     }

    }

   }

  </script>

</body>

</html>

PHP頁面:form.php

<?php

 //設(shè)置時區(qū)

 date_default_timezone_set('PRC');

 /*

  返回的提交消息

  status:狀態(tài)

  msg:提示信息

 */

 $msg = array('status'=>0,'msg'=>'');

 //獲取提交過來的數(shù)據(jù)

 $name = $_POST['username'];

 $pwd = $_POST['userpwd'];

 //模擬登錄驗證

 $user = array();

 $user['name'] = 'jack';

 $user['pwd'] = 'jack2014';

 if($name != $user['name']){

  $msg['msg'] = '該用戶未注冊!';

  $str = json_encode($msg);

  echo $str;

  exit;

 }else if($pwd != $user['pwd']){

  $msg['msg'] = '輸入的密碼錯誤!';

  $str = json_encode($msg);

  echo $str;

  exit;

 }

 $msg['msg'] = '登錄成功!';

 $msg['status'] = 1;

 $str = json_encode($msg);

 echo $str;

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)php程序設(shè)計有所幫助。

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:php HTML無刷新提交表單
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)