【发布时间】:2019-02-12 02:58:26
【问题描述】:
我有一个 shopify 商店,我想在产品页面上添加一个表单,问题是当我想使用带有外部链接的 ajax 提交一个表单时,但我想留在它转到外部链接的同一页面上
<form method="post" id="fastform"action="http://website.fun/bianca/doc.php" class="form" enctype="multipart/form-data">
<label class="label" style="color: rgb(0, 0, 0);">Nom : </label>
<input placeholder="Nom" class="input" width="100%" type="text" name="nom" id="nom">
<label class="label" style="color: rgb(0, 0, 0);">Télephone* : </label>
<input placeholder="Telephone" class="input" width="100%" type="text" name="phone" id="phone">
<input placeholder="Adresse" class="input" width="100%" type="text" name="adresse" id="adresse">
<div id="form-messages"></div>
<button type="submit" class="button-input btn btn-default" name="btn"style="margin-top: 20px;" id="btn">Commander</button>
这是 js en ajax 部分
<script>
$(function () {
// Get the form.
var form = $('#fastform');
// Get the messages div.
var formMessages = $('#form-messages');
// Set up an event listener for the contact form.
$(form).submit(function (event) {
// Stop the browser from submitting the form.
event.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
}.done(function(response) {
// Make sure that the formMessages div has the 'success' class.
$(formMessages).removeClass('error');
$(formMessages).addClass('success');
// Set the message text.
$(formMessages).text("votre commande et bien traiter");
// Clear the form.
$('#name').val('');
$('#email').val('');
$('#message').val('');
})).fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('success');
$(formMessages).addClass('error');
// Set the message text.
if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Oops! il\'ya un problem');
}
});
});
我希望页面保持不变并在同一页面上获得结果
【问题讨论】:
-
尝试在您的 html 表单中删除
action="http://website.fun/bianca/doc.php"。 -
但想处理该页面上存在于外部 url 上的表单
-
您可以将ajax url设置为
http://website.fun/bianca/doc.php作为字符串。 -
@abdessamadBen 你试过在提交按钮上使用 onclick="return false"
-
是的,我试过了,同样的问题
标签: javascript php ajax shopify