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/zh-hk/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

發表回復

登錄後才能評論