Creating an HTML enquiry form

The place to discuss anything to do with computers, software, hardware, no matter how basic or technical. We all use this stuff, but we don't always understand it!
User avatar
Rocket Rab
Posts: 2248
Joined: Mon Sep 18, 2006 5:37 pm
Contact:

Post by Rocket Rab »

ok, well, I've changed the two files and uploaded them, but nothing seems to happen so far. I have pop-up windows authorised, btw...

Anyone else got time to test the form (ideally omitting to enter e.g. your email and tel.)...?

www.iledereholidayhomes.com/contact.html

Rab (v. curious..)
la vache!
Posts: 11065
Joined: Wed Feb 16, 2005 7:22 pm

Post by la vache! »

I've just sent an enquiry sent without phone and e mail - it seems to have gone through :?
User avatar
Rocket Rab
Posts: 2248
Joined: Mon Sep 18, 2006 5:37 pm
Contact:

Post by Rocket Rab »

Thanks, LV. Yep, I had the same experience. I wonder if we can sort it before Brooke returns to her PC ? Now there's a challenge... 8)
User avatar
vrooje
Posts: 3202
Joined: Thu Dec 09, 2004 2:48 am
Location: Burgundy, France

Post by vrooje »

Oops! My fault.

The Firefox error console (in the tools menu) tells me:
Error: missing ; before statement
Source File: http://www.iledereholidayhomes.com/thankyou.php
Line: 46, Column: 12
Source Code:
valid false
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...

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
User avatar
Rocket Rab
Posts: 2248
Joined: Mon Sep 18, 2006 5:37 pm
Contact:

Post by Rocket Rab »

Bonne Nuit!! .....

.....off for a retry, THANKS Brooke

RR
User avatar
Rocket Rab
Posts: 2248
Joined: Mon Sep 18, 2006 5:37 pm
Contact:

Post by Rocket Rab »

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.
IT WORKS !!! Brooke to the rescue ! yabba dabba do!

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! :roll: Went and plonked it straight into thankyou.php.... I obviously have a long way to go yet... :lol:
e-richard
Posts: 5008
Joined: Sun Oct 17, 2004 11:33 am
Location: Algarve, Portugal
Contact:

Post by e-richard »

Thats great Brooke, and many thanks for posting that code publically, and so well commented :D

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 :roll: .

Oh well, just another 2c worth !
User avatar
AndyLucia
Posts: 484
Joined: Mon Jul 10, 2006 3:37 pm
Location: Isla Canela, Huelva
Contact:

Post by AndyLucia »

Thanks for all of this. At least I know what I'm doing this weekend!!
AndyLucia
If you can't say anything nice, don't say anything at all!!
User avatar
Rocket Rab
Posts: 2248
Joined: Mon Sep 18, 2006 5:37 pm
Contact:

Post by Rocket Rab »

e-richard wrote:Thats great Brooke, and many thanks for posting that code publically, and so well commented :D
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: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
User avatar
vrooje
Posts: 3202
Joined: Thu Dec 09, 2004 2:48 am
Location: Burgundy, France

Post by vrooje »

Moving this discussion along at a slight tangent, I wonder if, philosophically one wants to do complete validation on an enquiry form?
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.

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
   }
Then it would only check that either a valid e-mail address or a phone number were included...
And ain't it just so obvious that the javascript chkform function should be in contact.html?
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! :)

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
User avatar
Rocket Rab
Posts: 2248
Joined: Mon Sep 18, 2006 5:37 pm
Contact:

Post by Rocket Rab »

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
User avatar
vrooje
Posts: 3202
Joined: Thu Dec 09, 2004 2:48 am
Location: Burgundy, France

Post by vrooje »

...so I'll leave that bit of code in my contact.html.
Gotcha.

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&#40;$email&#41; &#123;
  // First, we check that there's one @ symbol, and that the lengths are right
  if &#40;!ereg&#40;"^&#91;^@&#93;&#123;1,64&#125;@&#91;^@&#93;&#123;1,255&#125;$", $email&#41;&#41; &#123;
    // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
    return false;
  &#125;
  // Split it into sections to make life easier
  $email_array = explode&#40;"@", $email&#41;;
  $local_array = explode&#40;".", $email_array&#91;0&#93;&#41;;
  for &#40;$i = 0; $i < sizeof&#40;$local_array&#41;; $i++&#41; &#123;
     if &#40;!ereg&#40;"^&#40;&#40;&#91;A-Za-z0-9!#$%&'*+/=?^_`&#123;|&#125;~-&#93;&#91;A-Za-z0-9!#$%&'*+/=?^_`&#123;|&#125;~\.-&#93;&#123;0,63&#125;&#41;|&#40;\"&#91;^&#40;\\|\"&#41;&#93;&#123;0,62&#125;\"&#41;&#41;$", $local_array&#91;$i&#93;&#41;&#41; &#123;
      return false;
    &#125;
  &#125;  
  if &#40;!ereg&#40;"^\&#91;?&#91;0-9\.&#93;+\&#93;?$", $email_array&#91;1&#93;&#41;&#41; &#123; // Check if domain is IP. If not, it should be valid domain name
    $domain_array = explode&#40;".", $email_array&#91;1&#93;&#41;;
    if &#40;sizeof&#40;$domain_array&#41; < 2&#41; &#123;
        return false; // Not enough parts to domain
    &#125;
    for &#40;$i = 0; $i < sizeof&#40;$domain_array&#41;; $i++&#41; &#123;
      if &#40;!ereg&#40;"^&#40;&#40;&#91;A-Za-z0-9&#93;&#91;A-Za-z0-9-&#93;&#123;0,61&#125;&#91;A-Za-z0-9&#93;&#41;|&#40;&#91;A-Za-z0-9&#93;+&#41;&#41;$", $domain_array&#91;$i&#93;&#41;&#41; &#123;
        return false;
      &#125;
    &#125;
  &#125;
  return true;
&#125;
?> 
Long, but pretty well-commented. It just returns true or false depending on whether the e-mail address adheres to standard rules (can't have two "@" characters, can't start with ".", and many other rules). It does not perform a DNS lookup to see if the host actually exists (the host being, e.g., gmail.com for blahblah@gmail.com). There are codes that do that too, but that seems like overkill! :)

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 &#40;valid_email&#40;$email&#41;&#41; &#123;
    // if the e-mail address is valid, then the form submission is valid
    $msgok = 1;
&#125; else &#123;

    // e-mail address not present or not valid, check for phone number
    if &#40;strlen&#40;$phone1&#41; == 0&#41; &#123;
        // phone is empty as well, no contact information given
        $msgok = 0;
    &#125; else &#123;
        // phone number but no e-mail address provided, form is OK
        $msgok = 1;
    &#125;

&#125;
Note that the very first if statement there calls the function I pasted at the bottom of the page. But this part of the code doesn't go at the bottom -- I placed it within the PHP code, just after defining the form variables (and still within the <?php ... ?> tags).

$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 &#40;strlen&#40;$name&#41; == 0&#41; &#123;
    $msgok = 0;
&#125;

if &#40;strlen&#40;$num_adults&#41; == 0&#41; &#123;
    $msgok = 0;
&#125;
Then, having established with this code whether the form submission is valid, I enclosed the part of the code where the message is assembled and sent in an if statement:

Code: Select all

if &#40;$msgok == 1&#41; &#123;
     // ..... pre-existing message assembly and delivery code here
&#125;
so that the inquiry will only be sent if the form submission was valid.

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 &#40;$msgok == 1&#41; &#123;
?>

    <!-- ..... pre-existing re-print of message in HTML here -->

<?php
&#125; else &#123;
?>

    <!-- insert HTML code here that says "your form was invalid, not sent!" in a polite way -->

<?php
&#125; // end else statement -- next line takes us out of PHP coding mode and puts us back into HTML mode
?>
In this case, there's an "else" statement, because I need to print something different if the form was not valid. That didn't exist yet in the code, so I wrote something, but I won't reproduce it here because a) it's specific to Jenny's website style and b) it uses (mostly) plain HTML, not PHP.

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
User avatar
Rocket Rab
Posts: 2248
Joined: Mon Sep 18, 2006 5:37 pm
Contact:

Post by Rocket Rab »

Amazing, Brooke, it works! That is an awful lot of code to add at once without testing - but you did it. 8)
10/10 Go to the top of the class! :D

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! :roll: )
e-richard
Posts: 5008
Joined: Sun Oct 17, 2004 11:33 am
Location: Algarve, Portugal
Contact:

Post by e-richard »

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..
User avatar
Rocket Rab
Posts: 2248
Joined: Mon Sep 18, 2006 5:37 pm
Contact:

Post by Rocket Rab »

Ain't no stoppin' us now.....(we've got the groove!) :lol:
Post Reply