Итак, вы сказали мне, что не поняли моего представления о том, что мне сказали.
Так что это краткий ответ моей идеи.
Предположим, у вас есть три формы на разных страницах или в * 1003. * та же страница как это -
<form id='registration_form'>
form fields like input, radio buttons, checkboxes, textareas etc
<input type='submit' class='form_btn'>
</form>
//***************************
<form id='login_form'>
form fields like input, radio buttons, checkboxes, textareas etc
<input type='submit' class='form_btn'>
</form>
//***************************
<form id='update_form'>
add form fields like input, radio buttons, checkboxes, textareas etc
<input type='submit' class='form_btn'>
</form>
//****************************
Каждая форма имеет уникальный атрибут id и общий класс для кнопки.
<script>
// create variables
let process_url, method, form, dataString;
// for submit button.
let form_btn = document.getElementsByClassName('form_btn');
// AJAX code to process the form data.
function your_ajax_function(){
$.ajax({
type: method,
url: process_url,
data: dataString,
cache: true,
beforeSend: function(){
// fetch those ID's and classes in function set_dynamic_variables()
// and then use here.
},
success: function(html){
// fetch those ID's and classes in function set_dynamic_variables()
// and then use here.
}
});
}
function set_dynamic_variables(e){
form = //write your own code to fetch the form-ID in which e is present.
switch (form) {
case 'registration_form':
process_url = './process/registration_form.process.php';
method = 'POST';
dataString = $('#' + form).serialize();
break;
case 'login_form':
process_url = './process/login_form.process.php';
method = 'POST';
dataString = $('#' + form).serialize();
break;
case 'update_form':
process_url = './process/update_form.process.php';
method = 'POST';
dataString = $('#' + form).serialize();
break;
default:
// statements_def
break;
} // switch()
} // if
// adding the event-listener for all the submit button of forms
for(let i=0; i< submit_btn.length; i++){
submit_btn[i].addEventListener('click', function(e) {
e.preventDefault();
set_dynamic_variables(e);
your_ajax_function();
}, false);
</script>
@ Сайт золотой дружбы Итак, это то, что я сказал вам в обсуждении.
и мой код сможет работать правильно, даже если Вы кладете все формы на одной странице.