php HTML submit form without refreshing_php tips
Usually we use ajax to submit forms without refreshing. Some time ago, I learned from my boss that another method of submitting a form without refreshing is implemented using the iframe framework. Now I’ve sorted it out and shared it with everyone.
The first type:
html page
<!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提交数据。 --> <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 page: 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; }
Second type:
html page
<!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数据转为json对象 var obj = eval("("+html+")"); //判断返回的状态 if(obj.status < 1){ alert(obj.msg); }else{ alert(obj.msg); window.location.href="http://www.baidu.com"; } } } </script> </body> </html>
PHP page: form.php
<?php //设置时区 date_default_timezone_set('PRC'); /* 返回的提交消息 status:状态 msg:提示信息 */ $msg = array('status'=>0,'msg'=>''); //获取提交过来的数据 $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;
The above is the entire content of this article. I hope it will be helpful to everyone in learning PHP programming.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
