这次就先容一下iframe仿造ajax异步要求,实际上iframe是同步要求,只是把提交的跳转,发生在iframe的可视区域内。
代码index.html
<!DOCTYPE html><html><head> <title>iframe提交表单</title> <meta charset="utf-8"> <style type="text/css"> #result{ border: none; /去掉默认的边框/ width: 300px; /可视区域的宽度/ height: 60px; /可视区域的高度/ } </style></head><body><!-- 表单 --><h1>iframe提交表单</h1><form action="check.php" method="post" target='result'> <input type="text" class="input_css" name="user" placeholder="请输入账号"><br/> <input type="password" class="input_css" name="pwd" placeholder="请输入密码"><br/> <input type="submit" class="formbtn" value="上岸"><br/></form><!-- 用于查看提交结果 --><iframe name='result' id="result" scrolling="no"></iframe></body></html>
check.php

<style type="text/css">{ margin:0; padding:0;}</style><?php// 设置编码header("Content-type:text/html;charset=utf-8");// 得到POST过来的上岸所需参数$user = $_POST["user"];$pwd = $_POST["pwd"];// 过滤参数if ($user == '' && $pwd == '') { echo "<p style='color:#f00;font-size:15px;margin-top:10px;'>账号和密码不得为空</p>";}else if ($user == '' ) { echo "<p style='color:#f00;font-size:15px;margin-top:10px;'>账号不得为空</p>";}else if ($pwd == '' ) { echo "<p style='color:#f00;font-size:15px;margin-top:10px;'>密码不得为空</p>";}else{ echo "<p style='color:#000;font-size:15px;margin-top:10px;'>你提交的账号是:".$user."<br/>你提交的密码是:".$pwd."</p>";}?>