I am on webpage 1.
On webpage 2, I have a form that needs some GET request values to be submitted and some events are also triggered e.g
form.php?a=1&b=2&c=3
Now, if I visit webpage 2 via the browser Url, it works.
I want to know, is there a way I can do this stuff via Ajax? Like, if I send a request to webpage 2 via ajax, it is treated like a normal browser request and the form is submitted from there.
Thanks
CodePudding user response:
If you are willing to use JavaScript on webpage 1, then yes.
A way of doing this, is using 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));
Just change the values of a, b and c dynamically on button click, so that instead of submitting the form you call a js function example:
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));
});
CodePudding user response:
Yes you can
$.ajax({
url: 'form.php?a=1&b=2&c=3',
type: 'GET',
contentType: false,
processData: false,
success: function(response) {
console.log(JSON.parse(response));
}
})