我有一个包含字段名称,电子邮件等的表单。现在,我希望当用户单击“提交”表单时,获取此值并使用ajax url提交。这该怎么做。我没有使用loopback4 url作为操作类,而没有使用mysql作为数据库。这是我的代码:
<form class="form-area contact-form text-right" name="myForm" id="myForm" action="http://localhost:3000/contacts/post" method="post" novalidate>
<div class="row">
<div class="col-lg-6 form-group">
<input name="name" id="name" placeholder="Enter your name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter your name'" class="common-input mb-20 form-control" required="" type="text">
<input name="email" id="email" placeholder="Enter email address" pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,63}$" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter email address'" class="common-input mb-20 form-control" required="" type="email">
<div class="invalid-feedback">Please Enter a Valid Email Id.</div>
<input name="subject" id="subject" placeholder="Enter your subject" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter your subject'" class="common-input mb-20 form-control" required="" type="text">
<div class="mt-20 alert-msg" style="text-align: left;"></div>
</div>
<div class="col-lg-6 form-group">
<textarea class="common-textarea form-control" name="message" id="message" placeholder="Message" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Message'" required=""></textarea>
<button type="submit" class="primary-btn mt-20 text-white" style="float: right;">Send Message</button>
</div>
</div>
</form>
Ajax:
<script type='text/javascript'>
$("#myForm").submit(function(e) {
var form = $(this);
var url = form.attr('action');
$.ajax({
type: "POST",
url: url,
data: form.serialize(), // serializes the form's elements.
datatype: 'json',
success: function(data)
{
alert(data); // show response from the php script.
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
</script>
如果我提交,则不会插入数据库。如何解决这个问题。