I'm completely new to PHP so apologies if this is a very obvious question. The problem I have is that I would like to create a form that collects a few extra details than the traditional "name" "email" and "message".
我是PHP的新手,所以如果这是一个非常明显的问题,请道歉。我遇到的问题是我想创建一个表单,收集一些额外的细节,而不是传统的“名称”“电子邮件”和“消息”。
Specifically, I'd like to have the following labels;
具体来说,我想要以下标签;
(i) Will you be attending (ii) Your Name (iii) Your Email (iv) Your Guests Name (v) Message
(i)您将参加(ii)您的姓名(iii)您的电子邮件(iv)您的客人姓名(v)消息
Here's what I have so far
这是我到目前为止所拥有的
<form name="sentMessage" id="contactForm" novalidate>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Attendance</label>
<input type="text" class="form-control" placeholder="Will you be attending?" id="going" required data-validation-required-message="Please type [Yes] or [No]">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Your Name</label>
<input type="text" class="form-control" placeholder="Your name" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email Address</label>
<input type="email" class="form-control" placeholder="Your email Address" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Your Name</label>
<input type="text" class="form-control" placeholder="Your guests name" id="guest" required data-validation-required-message="Please enter your guests name. Type 'none' if none.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls" id="message">
<label>Any dietary requirements</label>
<textarea rows="5" class="form-control" placeholder="Have you any dietary requirements?" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-outline-dark">SEND YOUR REPLY</button>
</div>
</div>
</form>
and the PHP
和PHP
$name = $_POST['name'];
$email_address = $_POST['email'];
$guestname = $_POST['guestname'];
$message = $_POST['message'];
and the JS
和JS
$(function() {
$("#contactForm input,#contactForm textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").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: "contact_me.php",
type: "POST",
data: {
name: name,
phone: phone,
email: email,
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. Please try again later!");
$('#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('');
});
When I submit the form, I receive information for name, email and message - but I'm missing details for 'guest name' and 'will you be attending'
当我提交表格时,我会收到有关姓名,电子邮件和信息的信息 - 但我错过了“客人姓名”和“你会参加”的详细信息吗?
Any ideas on what I am doing wrong?
关于我做错的任何想法?
2 个解决方案
#1
0
(You've made a type for the guest name
(你为客人名字做了一个类型
$guestname = $_POST['guest'];
Then you didnt show us how you check the "will you be attending, so i guess
然后你没有告诉我们你如何检查“你会参加,所以我想
$attending = $_POST['going'];
#2
0
I can see at least two obvious mistakes in the code that you submitted to us. Firstly,
我可以在您提交给我们的代码中看到至少两个明显的错误。首先,
$guestname = $_POST['guestname']
your HTML form input has
你的HTML表单输入有
id="guest"
instead of 'guestname'. Change either so that they match.
而不是'客人名'。改变它们以使它们匹配。
and your php code is missing the id="going" POST
并且你的php代码缺少id =“go”POST
Therefore it should be:
因此它应该是:
$name = $_POST['name'];
$email_address = $_POST['email'];
$guestname = $_POST['guest'];
$message = $_POST['message'];
$isAttending = $_POST['going'] /* $isAttending or something along those lines */
#1
0
(You've made a type for the guest name
(你为客人名字做了一个类型
$guestname = $_POST['guest'];
Then you didnt show us how you check the "will you be attending, so i guess
然后你没有告诉我们你如何检查“你会参加,所以我想
$attending = $_POST['going'];
#2
0
I can see at least two obvious mistakes in the code that you submitted to us. Firstly,
我可以在您提交给我们的代码中看到至少两个明显的错误。首先,
$guestname = $_POST['guestname']
your HTML form input has
你的HTML表单输入有
id="guest"
instead of 'guestname'. Change either so that they match.
而不是'客人名'。改变它们以使它们匹配。
and your php code is missing the id="going" POST
并且你的php代码缺少id =“go”POST
Therefore it should be:
因此它应该是:
$name = $_POST['name'];
$email_address = $_POST['email'];
$guestname = $_POST['guest'];
$message = $_POST['message'];
$isAttending = $_POST['going'] /* $isAttending or something along those lines */