Form表单提交数据详解

一、从form表单提交数据怎么拿到返回

使用form表单提交数据后,如果需要拿到返回的内容,可以使用JavaScript的XMLHttpRequest对象(简称XHR)

示例代码:

<form id="myForm" action="/example" method="post">
  <label for="myInput">输入内容:</label>
  <input id="myInput" type="text" name="inputContent">
  <button type="submit">提交</button>
</form>

  const form = document.getElementById('myForm');
  form.addEventListener('submit', function(event) {
    event.preventDefault(); // 阻止表单默认提交行为
    const xhr = new XMLHttpRequest();
    xhr.open(form.method, form.action);
    xhr.onload = function() {
      if (xhr.status === 200) {
        alert('返回的内容是:' + xhr.responseText);
      }
    };
    xhr.send(new FormData(form));
  });

二、form表单提交数据不跳转页面

使用Ajax技术,可以在不刷新页面的情况下提交表单并获取返回结果

示例代码:

<form id="myForm" action="/example" method="post">
  <label for="myInput">输入内容:</label>
  <input id="myInput" type="text" name="inputContent">
  <button type="button" onclick="submitForm()">提交</button>
</form>

  function submitForm() {
    const xhr = new XMLHttpRequest();
    xhr.open(document.getElementById('myForm').method, document.getElementById('myForm').action);
    xhr.onload = function() {
      if (xhr.status === 200) {
        alert('返回的内容是:' + xhr.responseText);
      }
    };
    xhr.send(new FormData(document.getElementById('myForm')));
  }

三、form表单提交数据量太大

如果需要提交大量数据,可以使用分块上传的方式,将数据按照一定大小进行切分,每次上传一部分,直到所有数据都被上传

示例代码:

<input type="file" id="myFile">

  const blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;
  const file = document.getElementById('myFile').files[0];
  const chunkSize = 1024 * 1024; // 每个分块文件大小为1MB
  const chunks = Math.ceil(file.size / chunkSize); // 计算需要分成几块
  const hash = 'abcdefg'; // 使用MD5等算法计算文件的哈希值
  const xhr = new XMLHttpRequest();
  xhr.open('POST', '/example', true);
  for (let i = 0; i = file.size) ? file.size : (i + 1) * chunkSize;
    const chunk = blobSlice.call(file, start, end);
    const formData = new FormData();
    formData.append('chunkData', chunk);
    formData.append('chunkIndex', i);
    formData.append('totalChunks', chunks);
    formData.append('hash', hash);
    xhr.send(formData);
  }

四、form表单提交数据格式

form表单提交数据的格式取决于表单的enctype属性的值,常用的类型有application/x-www-form-urlencoded(默认值)和multipart/form-data(用于上传文件或二进制数据)

示例代码:

<form enctype="multipart/form-data" action="/example">
  <input type="file" name="myFile">
  <button type="submit">上传</button>
</form>

五、form表单提交数据类型

常用的form表单提交数据类型有:text、hidden、password、radio、checkbox、file、select、textarea等

示例代码:

<form action="/example">
  <label for="myInput1">文本框:</label>
  <input id="myInput1" type="text" name="inputData1">
  <label for="myInput2">隐藏域:</label>
  <input id="myInput2" type="hidden" name="inputData2" value="123">
  <label for="myInput3">密码框:</label>
  <input id="myInput3" type="password" name="inputData3">
  <label for="myInput4">单选按钮:</label>
  <input id="myInput4" type="radio" name="inputData4" value="1">选项1
  <input id="myInput4" type="radio" name="inputData4" value="2">选项2
  <label for="myInput5">多选按钮:</label>
  <input id="myInput5" type="checkbox" name="inputData5" value="1">选项1
  <input id="myInput5" type="checkbox" name="inputData5" value="2">选项2
  <label for="myInput6">文件:</label>
  <input id="myInput6" type="file" name="inputData6">
  <label for="myInput7">下拉列表:</label>
  <select id="myInput7" name="inputData7">
    <option value="1">选项1</option>
    <option value="2">选项2</option>
  </select>
  <label for="myInput8">多行文本框:</label>
  <textarea id="myInput8" name="inputData8"></textarea>
  <button type="submit">提交</button>
</form>

六、form表单提交数据丢失

如果在提交表单的过程中数据丢失,可以先检查表单控件的name属性是否被正确填写,同时要确保表单控件没有被其他JavaScript代码修改

七、form表单提交数据json表示

可以使用JavaScript的JSON对象将表单提交的数据转化为JSON格式的字符串

示例代码:

<form id="myForm" action="/example" method="post">
  <label for="myInput1">输入内容1:</label>
  <input id="myInput1" type="text" name="inputContent1">
  <label for="myInput2">输入内容2:</label>
  <input id="myInput2" type="text" name="inputContent2">
  <button type="button" onclick="toJson()">提交</button>
</form>

  function toJson() {
    const inputs = document.querySelectorAll('#myForm input[type="text"]');
    const jsonObj = {};
    inputs.forEach(function(input) {
      jsonObj[input.name] = input.value;
    });
    const jsonStr = JSON.stringify(jsonObj);
    const xhr = new XMLHttpRequest();
    xhr.open(document.getElementById('myForm').method, document.getElementById('myForm').action);
    xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
    xhr.onload = function() {
      if (xhr.status === 200) {
        alert('返回的内容是:' + xhr.responseText);
      }
    };
    xhr.send(jsonStr);
  }

八、前端form表单提交数据

前端可以通过JavaScript处理表单提交的数据(例如验证数据是否正确、格式化数据等),然后再发送给后端,也可以使用Ajax技术在不刷新页面的情况下提交数据

九、post提交form表单数据

如果需要在form表单中使用HTTP的POST方法提交数据,需要将表单的method属性设置为post

示例代码:

<form method="post" action="/example">
  <label for="myInput">输入内容:</label>
  <input id="myInput" type="text" name="inputContent">
  <button type="submit">提交</button>
</form>

十、form表单提交数据验证密码

可以在前端使用JavaScript的正则表达式验证表单控件的数据是否符合要求

示例代码:

<form id="myForm" action="/example" method="post">
  <label for="myInput">密码:</label>
  <input id="myInput" type="password" name="password">
  <button type="submit" onclick="return validate()">提交</button>
</form>

  function validate() {
    const password = document.getElementById('myInput').value;
    const reg = /^[\w!@#$%^&*()_\-+={}\[\]|;:'",.\/?\\]{6,}$/;
    if (!reg.test(password)) {
      alert('密码必须包含字母、数字、特殊字符,长度至少为6位');
      return false;
    }
    return true;
  }

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/152593.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-11-13 06:05
下一篇 2024-11-13 06:05

相关推荐

  • Python读取CSV数据画散点图

    本文将从以下方面详细阐述Python读取CSV文件并画出散点图的方法: 一、CSV文件介绍 CSV(Comma-Separated Values)即逗号分隔值,是一种存储表格数据的…

    编程 2025-04-29
  • Python中读入csv文件数据的方法用法介绍

    csv是一种常见的数据格式,通常用于存储小型数据集。Python作为一种广泛流行的编程语言,内置了许多操作csv文件的库。本文将从多个方面详细介绍Python读入csv文件的方法。…

    编程 2025-04-29
  • 如何用Python统计列表中各数据的方差和标准差

    本文将从多个方面阐述如何使用Python统计列表中各数据的方差和标准差, 并给出详细的代码示例。 一、什么是方差和标准差 方差是衡量数据变异程度的统计指标,它是每个数据值和该数据值…

    编程 2025-04-29
  • Python多线程读取数据

    本文将详细介绍多线程读取数据在Python中的实现方法以及相关知识点。 一、线程和多线程 线程是操作系统调度的最小单位。单线程程序只有一个线程,按照程序从上到下的顺序逐行执行。而多…

    编程 2025-04-29
  • Python爬取公交数据

    本文将从以下几个方面详细阐述python爬取公交数据的方法: 一、准备工作 1、安装相关库 import requests from bs4 import BeautifulSou…

    编程 2025-04-29
  • Python两张表数据匹配

    本篇文章将详细阐述如何使用Python将两张表格中的数据匹配。以下是具体的解决方法。 一、数据匹配的概念 在生活和工作中,我们常常需要对多组数据进行比对和匹配。在数据量较小的情况下…

    编程 2025-04-29
  • Python数据标准差标准化

    本文将为大家详细讲述Python中的数据标准差标准化,以及涉及到的相关知识。 一、什么是数据标准差标准化 数据标准差标准化是数据处理中的一种方法,通过对数据进行标准差标准化可以将不…

    编程 2025-04-29
  • 如何使用Python读取CSV数据

    在数据分析、数据挖掘和机器学习等领域,CSV文件是一种非常常见的文件格式。Python作为一种广泛使用的编程语言,也提供了方便易用的CSV读取库。本文将介绍如何使用Python读取…

    编程 2025-04-29
  • Python根据表格数据生成折线图

    本文将介绍如何使用Python根据表格数据生成折线图。折线图是一种常见的数据可视化图表形式,可以用来展示数据的趋势和变化。Python是一种流行的编程语言,其强大的数据分析和可视化…

    编程 2025-04-29
  • Python如何打乱数据集

    本文将从多个方面详细阐述Python打乱数据集的方法。 一、shuffle函数原理 shuffle函数是Python中的一个内置函数,主要作用是将一个可迭代对象的元素随机排序。 在…

    编程 2025-04-29

发表回复

登录后才能评论