欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iframe无刷新提交表单,iframe仿ajax提交表单

程序员文章站 2024-03-01 10:19:52
...

本文摘要

使用ajax可以实现无刷新提交表单,但有人表示ajax的效率不行,或者是其他缺点,例如客户端臃肿,太多客户段代码造成开发上的成本,如果网速慢,则会出现ajax请求缓慢,页面空白的情况,对客户的体验不好。ajax请求不利于搜索引擎优化,一般搜不到ajax添加到页面的信息。

这次就介绍一下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>";
}
?>

iframe无刷新提交表单,iframe仿ajax提交表单
iframe无刷新提交表单,iframe仿ajax提交表单
iframe无刷新提交表单,iframe仿ajax提交表单

动图演示

iframe无刷新提交表单,iframe仿ajax提交表单

在线演示

http://www.likeyunba.com/demo...

本文作者

Author:TANKING
Date:2021-01-13
Wechat:sansure2016
Web:http://www.likeyun.cn/
Qrcode:Join in