Creating an HTML enquiry form
- Rocket Rab
- Posts: 2248
- Joined: Mon Sep 18, 2006 5:37 pm
- Contact:
- Rocket Rab
- Posts: 2248
- Joined: Mon Sep 18, 2006 5:37 pm
- Contact:
Oops! My fault.
The Firefox error console (in the tools menu) tells me:
Oh, and, I also forgot to specify that the javascript chkform function needs to go in contact.html. There will be a validation code that goes into thankyou.php, but not just yet. The javascript stuff has to be in contact.html because it has to go into action as the user submits the form (and the form is in contact.html). By the time the user gets to thankyou.php, the form has already been submitted.
P.S. - return to my PC? I never left! But I really am going to bed now...
The Firefox error console (in the tools menu) tells me:
It's in the section of the chkform function checking the number of adults... "valid false" should say "valid = false". I'm going to edit the code above to fix it...Error: missing ; before statement
Source File: http://www.iledereholidayhomes.com/thankyou.php
Line: 46, Column: 12
Source Code:
valid false
Oh, and, I also forgot to specify that the javascript chkform function needs to go in contact.html. There will be a validation code that goes into thankyou.php, but not just yet. The javascript stuff has to be in contact.html because it has to go into action as the user submits the form (and the form is in contact.html). By the time the user gets to thankyou.php, the form has already been submitted.
P.S. - return to my PC? I never left! But I really am going to bed now...
Brooke
- Rocket Rab
- Posts: 2248
- Joined: Mon Sep 18, 2006 5:37 pm
- Contact:
- Rocket Rab
- Posts: 2248
- Joined: Mon Sep 18, 2006 5:37 pm
- Contact:
IT WORKS !!! Brooke to the rescue ! yabba dabba do!vrooje wrote:Oh, and, I also forgot to specify that the javascript chkform function needs to go in contact.html. The javascript stuff has to be in contact.html because it has to go into action as the user submits the form .... by the time the user gets to thankyou.php, the form has already been submitted.
Test it here, folks: www.iledereholidayhomes.com/contact.html
And ain't it just so obvious that the javascript chkform function should be in contact.html? I didn't think of it, though! Went and plonked it straight into thankyou.php.... I obviously have a long way to go yet...
Thats great Brooke, and many thanks for posting that code publically, and so well commented
I'm sure I could use and adapt it to suit any type of form or do further validation e.g. on RR's form you can omit dates or give invalid dates, or you can fill in any number of guests (I sent an enquiry for over 200 people !)
Moving this discussion along at a slight tangent, I wonder if, philosophically one wants to do complete validation on an enquiry form? I'd certainly use all those techniques on a booking form, but we want to make it as easy as possible for people to get in touch with us to start the ball rolling on an enquiry. Also, I have had people use my enquiry form for general information discussion, where they do not have specific dates, or party size but just want to fill in the comments box. I have even had guests, who have already booked, use the form to send "email" when they have lost my email address.
I've found that minor errors on the enquiry form (apart from email address!!) can be sorted out in the post-enquiry correspondence phase, and it actually helps strike up some rapport with the prospects .
Oh well, just another 2c worth !
I'm sure I could use and adapt it to suit any type of form or do further validation e.g. on RR's form you can omit dates or give invalid dates, or you can fill in any number of guests (I sent an enquiry for over 200 people !)
Moving this discussion along at a slight tangent, I wonder if, philosophically one wants to do complete validation on an enquiry form? I'd certainly use all those techniques on a booking form, but we want to make it as easy as possible for people to get in touch with us to start the ball rolling on an enquiry. Also, I have had people use my enquiry form for general information discussion, where they do not have specific dates, or party size but just want to fill in the comments box. I have even had guests, who have already booked, use the form to send "email" when they have lost my email address.
I've found that minor errors on the enquiry form (apart from email address!!) can be sorted out in the post-enquiry correspondence phase, and it actually helps strike up some rapport with the prospects .
Oh well, just another 2c worth !
- Rocket Rab
- Posts: 2248
- Joined: Mon Sep 18, 2006 5:37 pm
- Contact:
I'll second that very wholeheartedly. The comments are particularly helpful, thanks Brooke. And while I'm about it, I also want to thank e-richard again for so generously unravelling my original form.e-richard wrote:Thats great Brooke, and many thanks for posting that code publically, and so well commented
.e-richard wrote:Moving this discussion along at a slight tangent, I wonder if, philosophically one wants to do complete validation on an enquiry form?
That's a very good point, imho. I've decided not to force validation of dates / numbers of guests for exactly that reason. Probably best not to make punters jump through too many hoops before booking
I think that's a good point! Perhaps just the e-mail or phone number, then. At a bare minimum it's essential to be able to respond to someone.Moving this discussion along at a slight tangent, I wonder if, philosophically one wants to do complete validation on an enquiry form?
In that case, you would remove the following code from chkform:
Code: Select all
// begin checking the name
if(frm.NAME.value.length==0) {
alertstring +="You must enter your name.\n"
valid = false
}
// begin checking number of adults
if(frm.Adults.value.length==0) {
alertstring+="Please enter the number of people in your party.\n"
valid = false
}
Well, it's only obvious now, because you understand it. I have tutoring students who say things to me like, "well, I only got an A because the test was easy," and I say the same thing. No, it was easy and you got an A because you worked hard to understand it!And ain't it just so obvious that the javascript chkform function should be in contact.html?
Now, the last stage is to write the validation that goes into thankyou.php. Are we agreed to just check that there's either an e-mail address or a phone number?
Brooke
- Rocket Rab
- Posts: 2248
- Joined: Mon Sep 18, 2006 5:37 pm
- Contact:
Brooke,
I'm going to keep the check on the name and the number of adults, because I think those are legitimate and useful, but I won't go any further by doing validation on the dates, for example, or by blocking a form with an obvious guest number error....so I'll leave that bit of code in my contact.html.
RR
I'm going to keep the check on the name and the number of adults, because I think those are legitimate and useful, but I won't go any further by doing validation on the dates, for example, or by blocking a form with an obvious guest number error....so I'll leave that bit of code in my contact.html.
RR
Gotcha....so I'll leave that bit of code in my contact.html.
I just made some changes to your thankyou.php that assume you'll only be checking the e-mail and phone number... but actually it's probably better to test the simpler version before adding a couple of extra statements about the name and number of adults.
LV, I'll try to post the changes that I made to RR's thankyou.php file. Of course, none of this is tested yet... but here goes!
First, I swiped a PHP e-mail validation function from this website:
Code: Select all
<?php
function valid_email($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
?>
So, I pasted that at the very bottom of thankyou.php, the form processing page.
Then, after the PHP code that retrieves the form data and stores it in variables (in this case $email and $phone1), I added:
Code: Select all
$msgok = 0;
if (valid_email($email)) {
// if the e-mail address is valid, then the form submission is valid
$msgok = 1;
} else {
// e-mail address not present or not valid, check for phone number
if (strlen($phone1) == 0) {
// phone is empty as well, no contact information given
$msgok = 0;
} else {
// phone number but no e-mail address provided, form is OK
$msgok = 1;
}
}
$msgok is intended to track whether the form is valid or not, so that information can be used later in the form. Note that this doesn't do anything fancy with the phone number check -- it's only invalid if it's empty. If more complicated checks are desired, it's easy to add that later.
I have not added it yet, but if I also wanted to check whether the name ($name) and number of adults ($num_adults) were present, I would add the following:
Code: Select all
if (strlen($name) == 0) {
$msgok = 0;
}
if (strlen($num_adults) == 0) {
$msgok = 0;
}
Code: Select all
if ($msgok == 1) {
// ..... pre-existing message assembly and delivery code here
}
The last step is then to let the client know if their submission was valid or not. RR's page already includes code that formats and re-displays their message, so I did the same thing as above, except this time there's a bit of a change afterward:
Code: Select all
<!-- some previous HTML here, setting up the table in which to display the message, etc.
The HTML here is whatever needs to be there whether the message was sent or not -->
<?php
if ($msgok == 1) {
?>
<!-- ..... pre-existing re-print of message in HTML here -->
<?php
} else {
?>
<!-- insert HTML code here that says "your form was invalid, not sent!" in a polite way -->
<?php
} // end else statement -- next line takes us out of PHP coding mode and puts us back into HTML mode
?>
Like I said, we haven't tested any of this yet. Theoretically it should work, but probability says that I probably missed a semicolon or something. We'll soon find out...
Brooke
- Rocket Rab
- Posts: 2248
- Joined: Mon Sep 18, 2006 5:37 pm
- Contact:
Amazing, Brooke, it works! That is an awful lot of code to add at once without testing - but you did it.
10/10 Go to the top of the class!
RR
(Short forum message, but long PM to Brooke: some funny stuff happens when I try to insert the bit of extra code myself - I must be doing something skewwhiff! )
10/10 Go to the top of the class!
RR
(Short forum message, but long PM to Brooke: some funny stuff happens when I try to insert the bit of extra code myself - I must be doing something skewwhiff! )
On the subject of checking number of guests, one could stop visitors from making mistakes and seeing errors by using drop downs instead of type in boxes.
For example, if you have a 2 bedroom apartment, you can ask (on the HTML form:
Total number of persons (including children): Permissable answers 1-4
Number of children (16 or under): Permissable answers 1-3
Now, if you have multiple properties as RR does, then the range of permissable answers needs to change depending on which property is chosen, but that requires more javascript in the form itself, and I'll wait for Brookes lesson number 7 to get that one !
Just an idea..
For example, if you have a 2 bedroom apartment, you can ask (on the HTML form:
Total number of persons (including children): Permissable answers 1-4
Number of children (16 or under): Permissable answers 1-3
Now, if you have multiple properties as RR does, then the range of permissable answers needs to change depending on which property is chosen, but that requires more javascript in the form itself, and I'll wait for Brookes lesson number 7 to get that one !
Just an idea..
- Rocket Rab
- Posts: 2248
- Joined: Mon Sep 18, 2006 5:37 pm
- Contact: