Anonymous
Как заставить работать две формы на одной странице index.php и получать только ответ в формате JSON без перезагрузки стр
Сообщение
Anonymous » 21 июн 2024, 00:33
Код index.php:
Код: Выделить всё
Index
Submit
Submit
const form1 = document.querySelector("#form1")
form1.addEventListener("submit", async (e) => {
e.preventDefault()
const query1 = form1.query1.value
const formData = new FormData()
formData.append("queryl", query1)
try {
await fetch('./index.php', {
method: 'POST',
body: formData,
headers: { 'Accept': 'application/json, text/plain, */*' }
})
.then((res) => res.json())
.then((data) => {
alert(data[0])
})
}
catch(error) { console.log(error) }
const form2 = document.querySelector("#form2")
form2.addEventListener('submit', async (e) => {
e.preventDefault()
const query2 = form2.query2.value
const formData = new FormData()
formData.append("query2", query2)
try {
await fetch('./index.php', {
method: 'POST',
body: formData,
headers: { 'Accept': 'application/json, text/plain, */*' }
})
.then((res) => res.json())
.then((data) => {
alert(data)
})
}
catch(error) { console.log(error) }
})
})
Даже при запрете по умолчанию и выходе после echo json_encode страница перезагружается. Мне нужно обработать результаты для каждой формы без какого-либо перекодирования. Мне нужно получить только ответ json в javascript для работы с результатами. Возможно ли использовать только этот файл index.php?
exit();
Подробнее здесь:
https://stackoverflow.com/questions/786 ... n-response
1718919226
Anonymous
Код index.php: [code] Index Submit Submit const form1 = document.querySelector("#form1") form1.addEventListener("submit", async (e) => { e.preventDefault() const query1 = form1.query1.value const formData = new FormData() formData.append("queryl", query1) try { await fetch('./index.php', { method: 'POST', body: formData, headers: { 'Accept': 'application/json, text/plain, */*' } }) .then((res) => res.json()) .then((data) => { alert(data[0]) }) } catch(error) { console.log(error) } const form2 = document.querySelector("#form2") form2.addEventListener('submit', async (e) => { e.preventDefault() const query2 = form2.query2.value const formData = new FormData() formData.append("query2", query2) try { await fetch('./index.php', { method: 'POST', body: formData, headers: { 'Accept': 'application/json, text/plain, */*' } }) .then((res) => res.json()) .then((data) => { alert(data) }) } catch(error) { console.log(error) } }) }) [/code] Даже при запрете по умолчанию и выходе после echo json_encode страница перезагружается. Мне нужно обработать результаты для каждой формы без какого-либо перекодирования. Мне нужно получить только ответ json в javascript для работы с результатами. Возможно ли использовать только этот файл index.php? exit(); Подробнее здесь: [url]https://stackoverflow.com/questions/78649668/how-make-it-work-two-forms-in-the-same-index-php-page-and-get-only-json-response[/url]