簡體   English   中英

Ajax/Php 表單提交

[英]Ajax/Php Form Submission

我在網頁 1。

在網頁 2 上,我有一個表單需要提交一些 GET 請求值,並且還會觸發一些事件,例如

form.php?a=1&b=2&c=3

現在,如果我通過瀏覽器Url訪問網頁2,就可以了。

我想知道,有沒有辦法可以通過 Ajax 來做這些事情? 比如,如果我通過 ajax 向網頁 2 發送請求,它會被視為正常的瀏覽器請求,並從那里提交表單。

謝謝

如果您願意在網頁 1 上使用 JavaScript,那么可以。

一種方法是使用 fetch: MDN

// gets data from form.php and logs it to console
fetch(`form.php?a=1&b=2&c=3`)
    .then(response => response.text())
    .then(text => console.log(text));

只需在單擊按鈕時動態更改 a、b 和 c 的值,這樣您就可以調用 js function 示例而不是提交表單:

document.getElementById("formbtn").addEventListener("click", () => {
    let a = document.getElementById('a').value;
    let b = document.getElementById('b').value;
    let c = document.getElementById('c').value;
    fetch(`form.php?a=${a}&b=${b}&c=${c}`)
        .then(response => response.text())
        .then(text => console.log(text));
});

是的,您可以使用 Jquery Ajax。了解更多

   $.ajax({
        url: 'form.php?a=1&b=2&c=3',
        type: 'GET',
        contentType: false,
        processData: false,
        success: function(response) {
            console.log(JSON.parse(response));
        }
    })

暫無
暫無

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

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