[英]jQuery form validate and submit multiple forms on the the same page gives “Uncaught SyntaxError: Invalid or unexpected token”
I am trying to submit user details to a MailChimp list via PHP for multiple forms on the same page. 我正在尝试通过PHP在同一页面上的多个表单将用户详细信息提交到MailChimp列表。
My code is as follows: 我的代码如下:
index.html 的index.html
<form id="contact-form" name="contact-form" action="assets/php/send.php" method="post" novalidate="novalidate">
<fieldset>
<div id="alert-area"></div>
<input class="col-sm-6 col-xs-12" id="fname" type="text" name="fname" placeholder="first name">
<input class="col-sm-6 col-xs-12" id="lname" type="text" name="lname" placeholder="last name">
<input class="col-sm-6 col-xs-12" id="number" type="text" name="number" placeholder="number">
<input class="col-sm-6 col-xs-12" id="email" type="text" name="email" placeholder="email">
<input class="btn btn-default blue" id="submit" type="submit" name="submit" value="Submit" onclick="ga('send', 'event', ‘scholars’, 'click', ‘s-rs’);">
<div id='response'></div>
</fieldset>
</form>
<form id="contact-form" name="contact-form" action="assets/php/send.php" method="post" novalidate="novalidate">
<fieldset>
<div id="alert-area"></div>
<input class="col-sm-6 col-xs-12" id="fname" type="text" name="fname" placeholder="first name">
<input class="col-sm-6 col-xs-12" id="lname" type="text" name="lname" placeholder="last name">
<input class="col-sm-6 col-xs-12" id="number" type="text" name="number" placeholder="number">
<input class="col-sm-6 col-xs-12" id="email" type="text" name="email" placeholder="email">
<input class="btn btn-default blue" id="submit" type="submit" name="submit" value="Submit" onclick="ga('send', 'event', ‘scholars’, 'click', ‘s-rs’);">
<div id='response'></div>
</fieldset>
</form>
send.php send.php
<?php
$api_key = 'XXXXXXXXX';
$list_id = 'XXXXXXXXXX';
// Include the MailChimp API wrapper
include('./inc/MailChimp.php');
// Then call/use the class
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp($api_key);
// Submit subscriber data to MailChimp
$result = $MailChimp->post("lists/$list_id/members", [
'email_address' => $_POST["email"],
'merge_fields' => ['FNAME'=>$_POST["fname"], 'LNAME'=>$_POST["lname"], 'NUMBER'=>$_POST["number"]],
'status' => 'subscribed',
]);
if ($MailChimp->success()) {
// Success message
echo "<h4>Thank you for your interest. </h4>";
} else {
// Display error
echo "<h4>Whoops! Please try again.</h4>";
}
?>
Validate function 验证功能
function validateForm(){
$('form').each(function(){
$(this).validate({
// all fields are required
rules: {
fname: {
required: true
},
lname: {
required: true
},
email: {
required: true,
email: true
},
number: {
required: true,
number: true
}
},
// if valid, post data via AJAX
submitHandler: function(form){
$.post("assets/php/send.php", {
fname: $("#fname").val(),
lname: $("#lname").val(),
email: $("#email").val(),
number: $("#number").val()
}, function (data) {
$('#response').html(data);
});
}
})
})
}
This works fine for the first form but gives a "Uncaught SyntaxError: Invalid or unexpected token" for other forms 这对于第一种形式工作正常,但对于其他形式则给出了“未捕获的SyntaxError:无效或意外的令牌”
The SyntaxError was because of rogue quote marks in the ga onCLick event. SyntaxError是由于ga onCLick事件中的流氓引号引起的。
Solved the multiple form submit issue with the following jQuery code 使用以下jQuery代码解决了多表单提交问题
function validateForm(){
$('form').each(function(){
$(this).validate({
// all fields are required
rules: {
fname: {
required: true
},
lname: {
required: true
},
email: {
required: true,
email: true
},
number: {
required: true,
number: true
}
},
// if valid, post data via AJAX
submitHandler: function (form) {
$.post("assets/php/send.php", $(form).serializeArray().reduce(function(obj, item) {
obj[item.name] = item.value;
return obj;
}, {})
, function (data) {
$(form).find('#response').html(data);
});
}
})
})
}
All forms have unique ID. 所有表格都有唯一的ID。 This code works with modal form too. 该代码也适用于模式形式。 Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.