0
ビルドしようとしているフォームがあります。基本的に私はすでにPHPの検証で構築されているフォームの基本的なブートストラップテンプレートを使用しています。内蔵、それは具体的に電子メールアドレスを検証します。しかし、私は有効なURLのための別のフィールドを追加しようとしており、phpはこの他のフィールドを検証して電子メールに渡すようにしています。PHP URLフォーム検証
現在、私のコードは、HTMLで次のようになります。
<div class="row">
<div class="col-md-8">
<h3>Order/Estimate Form</h3>
<p style="color:red;">*Please Fill In All Fields*</p>
<form name="sentMessage" id="contactForm" novalidate>
<div class="control-group form-group">
<div class="controls">
<label>Full Name:</label><p style="color:gray;">* required field</p>
<input type="text" class="form-control" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Company</label>
<input type="company" class="form-control" id="company">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Phone Number:</label><p style="color:gray;">* required field</p>
<input type="tel" class="form-control" id="phone" required data-validation-required-message="Please enter your phone number.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Email Address:</label><p style="color:gray;">* required field</p>
<input type="email" class="form-control" id="email" required data-validation-required-message="Please enter your email address.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Download Link</label><p style="color:gray;">* required field</p>
<input type="url" class="form-control" id="url" required data-validation-required-message="Please enter a valid shareable download link from wetransfer.com">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Subject:</label><p style="color:gray;">* required field</p>
<input type="subject" class="form-control" id="subject" required data-validation-required-message="Please enter a subject.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Message:</label><p style="color:gray;">* required field</p>
<textarea rows="10" cols="100" class="form-control" id="message" required data-validation-required-message="Please enter your message" maxlength="999" style="resize:none"></textarea>
</div>
</div>
<div id="success"></div>
<!-- For success/fail messages -->
<button type="submit" class="btn btn-primary">Send Message</button>
</form>
</div>
</div>
そして、次のようにこれに接続されている私のPHPは次のとおりです。
<?php
// check if fields passed are empty
if(empty($_POST['name']) ||
empty($_POST['phone']) ||
empty($_POST['subject']) ||
empty($_POST['email']) ||
empty($_POST['url']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
!filter_var($_POST['url'],FILTER_VALIDATE_URL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$url = $_POST['url'];
$email_address = $_POST['email'];
$message = $_POST['message'];
// create email body and send it
$to = '[email protected]'; // PUT YOUR EMAIL ADDRESS HERE
$email_subject = "Quote/Pricing Form: $name"; // EDIT THE EMAIL SUBJECT LINE HERE
$email_body = "You have received a new message from your website's contact form.\n\n"."Here are the details:\n\nName: $name\n\nPhone: $phone\n\nEmail: $email_address\n\nURL: $url\n\nSubject: $subject\n\nMessage:\n$message";
$headers = "From: [email protected]\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
はさらに、ここでjQueryの検証コードです:
$(function() {
$("#contactForm input,#contactForm textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// something to have when submit produces an error ?
// Not decided if I need it yet
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input#name").val();
var phone = $("input#phone").val();
var email = $("input#email").val();
var url = $("input#url").val();
var subject = $("input#subject").val();
var message = $("textarea#message").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$.ajax({
url: "./bin/contact_me.php",
type: "POST",
data: {
name: name,
phone: phone,
email: email,
url: url,
subject: subject,
message: message
},
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>Sorry " + firstName + " it seems that my mail server is not responding...</strong> Could you please email me directly to <a href='mailto:[email protected]?Subject=Message_Me from myprogrammingblog.com;>[email protected]</a> ? Sorry for the inconvenience!");
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
$('#success').html('');
});