簡體   English   中英

使用 jquery ajax json 發送表單數據

[英]Send form data with jquery ajax json

我是 PHP/jquery 的新手,我想問一下如何使用 ajax 以 json 格式從表單字段(如(姓名、年齡等))發送 json 數據。 可悲的是,我找不到任何有關此的相關信息,甚至可以動態執行此操作嗎? 谷歌搜索只返回答案,比如手動建立數據。 比如: name: XYage: 32 ,等等。

有沒有辦法做到這一點?

謝謝您的幫助!

編輯:

<form action="test.php" method="post">
Name: <input type="text" name="name"><br>
Age: <input type="text" name="email"><br>
FavColor: <input type="text" name="favc"><br>
<input type="submit">
</form>

這是一個簡單的

這是我的test.php僅用於測試

<?php

// this is just a test
//send back to the ajax request the request

echo json_encode($_POST);

這是我的index.html

<!DOCTYPE html>
<html>

<head>

</head>
<body>

<form id="form" action="" method="post">
Name: <input type="text" name="name"><br>
Age: <input type="text" name="email"><br>
FavColor: <input type="text" name="favc"><br>
<input id="submit" type="button" name="submit" value="submit">
</form>




<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        // click on button submit
        $("#submit").on('click', function(){
            // send ajax
            $.ajax({
                url: 'test.php', // url where to submit the request
                type : "POST", // type of action POST || GET
                dataType : 'json', // data type
                data : $("#form").serialize(), // post data || get data
                success : function(result) {
                    // you can see the result from the console
                    // tab of the developer tools
                    console.log(result);
                },
                error: function(xhr, resp, text) {
                    console.log(xhr, resp, text);
                }
            })
        });
    });

</script>
</body>
</html>

兩個文件都放在同一目錄中

您可以像這樣使用serialize()

$.ajax({
  cache: false,
  url: 'test.php',
  data: $('form').serialize(),
  datatype: 'json',
  success: function(data) {

  }
});

這里接受的答案確實從一個表單中生成一個json,但json內容實際上是一個帶有url編碼內容的字符串。

為了使更真實的json POST,使用從Serialize表單數據到JSON的一些解決方案來formToJson函數並將contentType: 'application/json;charset=UTF-8'到jQuery ajax調用參數。

$.ajax({
    url: 'test.php',
    type: "POST",
    dataType: 'json',
    data: formToJson($("form")),
    contentType: 'application/json;charset=UTF-8',
    ...
})

通過POST方法可以將表單域中的數據發送回服務器(php),這可以在PHP內部的超全局數組$ _POST中找到。 在將其發送到服務器之前,無需將其轉換為JSON。 小例子:

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    echo '<pre>';
    print_r($_POST);
}
?>
<form action="" method="post">
<input type="text" name="email" value="joe@gmail.com" />
<button type="submit">Send!</button>

使用AJAX,您可以完成相同的操作,只需要不刷新頁面。

既然可以使用 FormData javascript API 來完成,為什么還要使用 JQuery。

var form = document.querySelector('form');
form.onsubmit = function(event){
    var formData = new FormData(form);
    
    fetch("/test.php",
    {
       body: formData,
       method: "post"
    }).then(…);
    
    //Dont submit the form.
    return false; 
}

參考: https : //metamug.com/article/html5/ajax-form-submit.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM