Home > Article > Backend Development > How to make the click submit text box not clear in php
When we fill out a form on a PHP interface and click "Submit", the form is usually cleared so that we can fill in new form data. Sometimes, we hope that the data in the form text box will not be cleared after clicking the submit button. In this article, we will explore how to accomplish this task.
In HTML, we can use form elements to create forms, such as text boxes, radio buttons, multi-select boxes, etc. When we click the submit button, the form data will be passed to the server side for processing. PHP can easily process this form data and return the results to the user.
However, if we want the form text box to not be cleared, we need to use some JavaScript code on the submit button. Specifically, we need to use JavaScript's preventDefault() method to prevent the default behavior from occurring, and then use AJAX to send the form data to the server side for processing.
To demonstrate how to implement this process, we will create a simple PHP form that contains a text box and a submit button. When the user clicks the submit button, the data in the form text box will remain on the page.
First, we need to create an HTML form, including a text box and a submit button:
<form id="myForm" method="post"> <input type="text" name="myInput" id="myInput"> <button type="submit" id="myButton">提交</button> </form>
Please note that we use an ID on the submit button for convenience later JavaScript code used.
Next, we need to write some JavaScript code to handle the form submission event. Specifically, we'll use jQuery to handle AJAX requests, and the preventDefault() method to prevent form submission and preserve the data in the form text box.
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> // 阻止默认的表单提交事件 $('#myForm').submit(function(event) { event.preventDefault(); // 获取表单数据 var formData = { 'myInput': $('input[name=myInput]').val() }; // 发送 AJAX 请求 $.ajax({ type: 'POST', url: 'processForm.php', // 处理表单数据的 php 文件 data: formData }) .done(function(response) { // 请求成功处理 console.log(response); }) .fail(function(error) { // 请求失败处理 console.error(error); }); }); </script>
In this code, we use the $() function to get the form elements and add submit event listening. In the event, we use the preventDefault() method to prevent the default form submission event. Next, we use the $().val() method to get the form data and store it in the formData object. Finally, we use the $.ajax() method to send the form data to the server for processing.
Please replace the URL and PHP file name in the above code according to your own needs.
Finally, we need to ensure that the data in the form text box will not be cleared after the page is refreshed. We can use sessionStorage or localStorage to store the values in the textbox and restore them from storage when the page loads.
<script> // 存储表单数据 $(document).ready(function() { var myInputValue = sessionStorage.getItem('my-input-value'); $('input[name=myInput]').val(myInputValue); }); // 存储表单数据并在提交时更新 $('#myForm').submit(function(event) { var myInputValue = $('input[name=myInput]').val(); sessionStorage.setItem('my-input-value', myInputValue); }); </script>
In the above code, we use the sessionStorage.getItem() method to get the value of the form input box when the document is loaded, and use the $().val() method to set the value in the text box. On form submission, we use the $().val() method to get the value in the text box and use the sessionStorage.setItem() method to store it in sessionStorage.
In this way, we have completed the process of not clearing the form text box. When the user fills out the form and clicks the submit button, the form data will be sent to the server side for processing, and the value in the text box will be stored in sessionStorage for next time use.
The above is the detailed content of How to make the click submit text box not clear in php. For more information, please follow other related articles on the PHP Chinese website!