search
HomeBackend DevelopmentPHP TutorialHow to retain the filled-in information after PHP fails to submit the form_PHP Tutorial

This article will introduce to you some summary methods on how to retain the filled-in information after PHP fails to submit the form. The most commonly used method is to use caching. This method may cause problems if the network speed is slow. The best way is to use ajax. .

1. Use the header header to set the cache control header Cache-control.

PHP code
1.

tr>
The code is as follows Copy code
header('Cache-control: private, must-revalidate'); //Support page bounce
 代码如下 复制代码
header('Cache-control: private, must-revalidate'); //支持页面回跳

2. Use the session_cache_limiter method.

PHP code

The code is as follows Copy code
1.session_cache_limiter('private, must-revalidate'); //Write before the session_start method
 代码如下 复制代码
1.session_cache_limiter('private, must-revalidate'); //要写在session_start方法之前

The following is an introduction to the session_cache_limiter parameters:

The meaning of several parameters in session_cache_limiter is:
nocache: Of course it is not cached (for example: the form information is cleared), but public variables can be cached
private: private mode cache (for example: form information is retained, but valid within the lifetime)
private_no_cache: private mode but does not expire (form information is retained)
publice: public mode, (form information is also retained Reserved)

Set cache expiration time: session_cache_expire function setting, the default is 180 minutes.

Commonly encountered problems:

1. session_cache_limiter("private"); The form information is retained, but if I modify the submitted information, the information presented on the form page is still the information in the cache, which cannot be automatically refreshed in time. If session_cache_limiter("private"); is not used, it cannot Retain form information

Solution:

Add

The code is as follows Copy code
 代码如下 复制代码

session_cache_limiter( "private, must-revalidate" );即可

session_cache_limiter( "private, must-revalidate" ); That's it


Another way we can use ajax to instance


index.html template file large content:

The code is as follows Copy code



jQuery Ajax Example Demonstration<br><br><script src="./js/jquery.js" type="text/javascript"></script><br><script type="text/javascript"><br />$(document).ready(function(){//This is jQueryready, it is like the main of C language and all operations are included in it<br /> $("#button_login") .mousedown(function(){<br /> login(); //The function login() is triggered after clicking the button with the ID "button_login";<br /> });<p> function login(){ //函数 login();<br /> var username = $("#username").val();//取框中的用户名<br /> var password = $("#password").val();//取框中的密码<br /> $.ajax({ //一个Ajax过程<br /> type: "post", //以post方式与后台沟通<br /> url : "login.php", //与此php页面沟通<br /> dataType:'json',//从php返回的值以 JSON方式 解释<br /> data: 'username='+username+'&password='+password, //发给php的数据有两项,分别是上面传来的u和p<br /> success: function(json){//如果调用php成功<br /> //alert(json.username+'n'+json.password); //把php中的返回值(json.username)给 alert出来<br /> $('#result').html("姓名:" + json.username + "<br/>密码:" + json.password); //把php中的返回值显示在预定义的result定位符位置<br /> }<br /> });<br /> }<br /> //$.post()方式:<br /> $('#test_post').mousedown(function (){<br /> $.post(<br /> 'login.php',<br /> {<br /> username:$('#username').val(),<br /> password:$('#password').val()<br /> },<br /> function (data) //回传函数<br /> {<br /> var myjson='';<br /> eval_r('myjson=' + data + ';');<br /> $('#result').html("姓名1:" + myjson.username + "<br/>密码1:" + myjson.password);<br /> }<br /> );<br /> });<br /> //$.get()方式:<br /> $('#test_get').mousedown(function (){<br /> $.get(<br /> 'login.php',<br /> {<br /> username:$('#username').val(),<br /> password:$('#password').val()<br /> },<br /> function(data) //回传函数<br /> {<br /> var myjson='';<br /> eval_r("myjson=" + data + ";");<br /> $('#result').html("姓名2:" + myjson.username + "<br/>密码2:" + myjson.password);<br /> }<br /> );<br /> });<br />});<br /></script><br>


Enter name:


Enter password:







There seems to be a bug in the original source code, but it runs normally after modification.

Contents of the login.php file:

?>
The code is as follows
 代码如下 复制代码
echo json_encode(array ('username'=>$_REQUEST['username'],'password'=>$_REQUEST['password']));
?>
Copy code

echo json_encode(array ('username'=>$_REQUEST['username'],'password'=>$ _REQUEST['password']));

In this case, we don’t need to refresh the page when submitting. If it fails, there will be a submission directly, which is 100% The data will not be lost after the save and submit fails.

http://www.bkjia.com/PHPjc/444583.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/444583.html
TechArticle
This article will introduce to students how to retain the filled-in information after PHP fails to submit the form. Some methods are summarized, the most commonly used Just use caching. This method may be possible if the network speed is slow...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function