admin管理员组文章数量:1026989
I'm facing a weird problem where I have to reach a form page via two methods: create and edit. When posting from the create URL, the same form works perfectly, but whenever I post via edit or re-create URL, the page refreshes itself and tries to post on the URL itself.
Urls are: http://localhost:8000/admin/orders/create http://localhost:8000/admin/orders/create/1234
and the code is:
$('#quote_form').validate({
rules: {
item: {
required: true,
},
quantity: {
required: true,
}
},
focusInvalid: true,
invalidHandler: function(form, validator) {
$('#' + validator.errorList[0].element.id).focus();
},
submitHandler: function() {
$('#newQuote').empty();
let dataArray = $('#quote_form').serializeArray();
let item = $('#item').val();
let customerValue = '';
if ($('#customer').val().length !== 0 && $('#customer').val() !== undefined) {
customerValue = $('#customer').val();
} else if ($('#customer_id').val().length !== 0 && $('#customer_id').val() !== undefined) {
customerValue = $('#customer_id').val();
}
dataArray.push({
name: 'customer',
value: customerValue
});
$.ajax({
url: ADMIN_AJAX_URL + "orders/create-order-session",
method: 'post',
data: dataArray,
success: function(response) {
let result = JSON.parse(response);
if (result.html != '') {
clearForm($("#quote_form"));
$('#newQuote').append(result.html);
} else {
$('.error_text').html(result.message);
$('.error_row').show();
}
}
});
}
});
I've tried to post via the submit button and form validation etc, but not bored fruit.
I'm facing a weird problem where I have to reach a form page via two methods: create and edit. When posting from the create URL, the same form works perfectly, but whenever I post via edit or re-create URL, the page refreshes itself and tries to post on the URL itself.
Urls are: http://localhost:8000/admin/orders/create http://localhost:8000/admin/orders/create/1234
and the code is:
$('#quote_form').validate({
rules: {
item: {
required: true,
},
quantity: {
required: true,
}
},
focusInvalid: true,
invalidHandler: function(form, validator) {
$('#' + validator.errorList[0].element.id).focus();
},
submitHandler: function() {
$('#newQuote').empty();
let dataArray = $('#quote_form').serializeArray();
let item = $('#item').val();
let customerValue = '';
if ($('#customer').val().length !== 0 && $('#customer').val() !== undefined) {
customerValue = $('#customer').val();
} else if ($('#customer_id').val().length !== 0 && $('#customer_id').val() !== undefined) {
customerValue = $('#customer_id').val();
}
dataArray.push({
name: 'customer',
value: customerValue
});
$.ajax({
url: ADMIN_AJAX_URL + "orders/create-order-session",
method: 'post',
data: dataArray,
success: function(response) {
let result = JSON.parse(response);
if (result.html != '') {
clearForm($("#quote_form"));
$('#newQuote').append(result.html);
} else {
$('.error_text').html(result.message);
$('.error_row').show();
}
}
});
}
});
I've tried to post via the submit button and form validation etc, but not bored fruit.
Share Improve this question asked Nov 16, 2024 at 2:14 Abid RizviAbid Rizvi 131 silver badge3 bronze badges 3 |1 Answer
Reset to default 1To prevent the default form submission behavior, pass event.preventDefault()
in your submitHandler
. The submitHandler
function receives a form parameter, so you should pass that into $(form).serializeArray()
instead of using $('#quote_form').serializeArray()
.
$('#quote_form').validate({
rules: {
item: {
required: true,
},
quantity: {
required: true,
}
},
focusInvalid: true,
invalidHandler: function(form, validator) {
$('#' + validator.errorList[0].element.id).focus();
},
submitHandler: function(form, event) {
event.preventDefault();
$('#newQuote').empty();
let dataArray = $(form).serializeArray();
let item = $('#item').val();
let customerValue = '';
if ($('#customer').val().length !== 0 && $('#customer').val() !== undefined) {
customerValue = $('#customer').val();
} else if ($('#customer_id').val().length !== 0 && $('#customer_id').val() !== undefined) {
customerValue = $('#customer_id').val();
}
dataArray.push({
name: 'customer',
value: customerValue
});
$.ajax({
url: ADMIN_AJAX_URL + "orders/create-order-session",
method: 'post',
data: dataArray,
success: function(response) {
let result = JSON.parse(response);
if (result.html != '') {
clearForm($("#quote_form"));
$('#newQuote').append(result.html);
} else {
$('.error_text').html(result.message);
$('.error_row').show();
}
}
});
}
});
I'm facing a weird problem where I have to reach a form page via two methods: create and edit. When posting from the create URL, the same form works perfectly, but whenever I post via edit or re-create URL, the page refreshes itself and tries to post on the URL itself.
Urls are: http://localhost:8000/admin/orders/create http://localhost:8000/admin/orders/create/1234
and the code is:
$('#quote_form').validate({
rules: {
item: {
required: true,
},
quantity: {
required: true,
}
},
focusInvalid: true,
invalidHandler: function(form, validator) {
$('#' + validator.errorList[0].element.id).focus();
},
submitHandler: function() {
$('#newQuote').empty();
let dataArray = $('#quote_form').serializeArray();
let item = $('#item').val();
let customerValue = '';
if ($('#customer').val().length !== 0 && $('#customer').val() !== undefined) {
customerValue = $('#customer').val();
} else if ($('#customer_id').val().length !== 0 && $('#customer_id').val() !== undefined) {
customerValue = $('#customer_id').val();
}
dataArray.push({
name: 'customer',
value: customerValue
});
$.ajax({
url: ADMIN_AJAX_URL + "orders/create-order-session",
method: 'post',
data: dataArray,
success: function(response) {
let result = JSON.parse(response);
if (result.html != '') {
clearForm($("#quote_form"));
$('#newQuote').append(result.html);
} else {
$('.error_text').html(result.message);
$('.error_row').show();
}
}
});
}
});
I've tried to post via the submit button and form validation etc, but not bored fruit.
I'm facing a weird problem where I have to reach a form page via two methods: create and edit. When posting from the create URL, the same form works perfectly, but whenever I post via edit or re-create URL, the page refreshes itself and tries to post on the URL itself.
Urls are: http://localhost:8000/admin/orders/create http://localhost:8000/admin/orders/create/1234
and the code is:
$('#quote_form').validate({
rules: {
item: {
required: true,
},
quantity: {
required: true,
}
},
focusInvalid: true,
invalidHandler: function(form, validator) {
$('#' + validator.errorList[0].element.id).focus();
},
submitHandler: function() {
$('#newQuote').empty();
let dataArray = $('#quote_form').serializeArray();
let item = $('#item').val();
let customerValue = '';
if ($('#customer').val().length !== 0 && $('#customer').val() !== undefined) {
customerValue = $('#customer').val();
} else if ($('#customer_id').val().length !== 0 && $('#customer_id').val() !== undefined) {
customerValue = $('#customer_id').val();
}
dataArray.push({
name: 'customer',
value: customerValue
});
$.ajax({
url: ADMIN_AJAX_URL + "orders/create-order-session",
method: 'post',
data: dataArray,
success: function(response) {
let result = JSON.parse(response);
if (result.html != '') {
clearForm($("#quote_form"));
$('#newQuote').append(result.html);
} else {
$('.error_text').html(result.message);
$('.error_row').show();
}
}
});
}
});
I've tried to post via the submit button and form validation etc, but not bored fruit.
Share Improve this question asked Nov 16, 2024 at 2:14 Abid RizviAbid Rizvi 131 silver badge3 bronze badges 3-
... but not bored fruit
... what does this mean? – Paul T. Commented Nov 16, 2024 at 4:36 - @PaulT. I'd assume the OP meant "but it did not bear fruit". The idea of a fruit being bored and needing entertainment did make me smile though – ADyson Commented Nov 16, 2024 at 8:35
- I think its "Bore Fruit" but people not give you margin of a single letter. Anyways I'm glad that you smiled on something. By the way I'm a web developer not an article writer. – Abid Rizvi Commented Nov 18, 2024 at 14:17
1 Answer
Reset to default 1To prevent the default form submission behavior, pass event.preventDefault()
in your submitHandler
. The submitHandler
function receives a form parameter, so you should pass that into $(form).serializeArray()
instead of using $('#quote_form').serializeArray()
.
$('#quote_form').validate({
rules: {
item: {
required: true,
},
quantity: {
required: true,
}
},
focusInvalid: true,
invalidHandler: function(form, validator) {
$('#' + validator.errorList[0].element.id).focus();
},
submitHandler: function(form, event) {
event.preventDefault();
$('#newQuote').empty();
let dataArray = $(form).serializeArray();
let item = $('#item').val();
let customerValue = '';
if ($('#customer').val().length !== 0 && $('#customer').val() !== undefined) {
customerValue = $('#customer').val();
} else if ($('#customer_id').val().length !== 0 && $('#customer_id').val() !== undefined) {
customerValue = $('#customer_id').val();
}
dataArray.push({
name: 'customer',
value: customerValue
});
$.ajax({
url: ADMIN_AJAX_URL + "orders/create-order-session",
method: 'post',
data: dataArray,
success: function(response) {
let result = JSON.parse(response);
if (result.html != '') {
clearForm($("#quote_form"));
$('#newQuote').append(result.html);
} else {
$('.error_text').html(result.message);
$('.error_row').show();
}
}
});
}
});
本文标签:
版权声明:本文标题:php - Post a form via jquery from a same page where url is slightly changed but working on one url and refreshes the page on oth 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745668420a2162311.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
... but not bored fruit
... what does this mean? – Paul T. Commented Nov 16, 2024 at 4:36