Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Validate contact form with Smarty

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
random1739
Smarty Rookie


Joined: 03 Jul 2007
Posts: 5

PostPosted: Mon Nov 26, 2007 1:34 am    Post subject: Validate contact form with Smarty Reply with quote

I've posted around various places before regarding this without any luck. I have a working php "contact us" form on my website using a smarty template.

I really need to add some form of validation to the email and phone number fields on the form.

A few other forums suggested using javascript to validate, but when I add javascript to the head.tpl file for my contact page, nothing seems to happen. The form gets sent without being stopped

Original working code (no validation):
Code:
<?php

require_once('libs/Smarty.class.php');
$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = false;

if (isset($_POST['action']) && $_POST['action'] == 'Submit request')
{
   $body = '';
   $body .= 'How did you hear about Dive Adventures? ' . $_POST['heard'] . "\n";
   $body .= "\n";
   $body .= '* Your adventure' . "\n";
   $body .= 'Which destination are you interested in? ' . $_POST['holiday'] . "\n";
   $body .= 'When do you intend to travel? ' . $_POST['month'] . ' ' . $_POST['year'] . "\n";
   $body .= 'What type of diving do you enjoy doing? ' . $_POST['diving_type'] . "\n";
   $body .= 'Number of guests: ' . "\n";
   $body .= 'Adults: ' . $_POST['adults'] . ' (' . $_POST['adult_divers'] . ' are divers)' . "\n";
   $body .= 'Children: ' . $_POST['children'] . ' (' . $_POST['child_divers'] . ' are divers)' . "\n";
   $body .= 'Infants: ' . $_POST['infants'] . ' (' . $_POST['infant_divers'] . ' are divers)' . "\n";
   $body .= 'Comments: ' . "\n";
   $body .= $_POST['comments'] . '.' . "\n";
   $body .= "\n";
   $body .= '* Your details' . "\n";
   $body .= 'Name ' . $_POST['name'] . "\n";
   $body .= 'Email ' . $_POST['email'] . "\n";
   $body .= 'Address ' . $_POST['address'] . "\n";
   $body .= 'City ' . $_POST['city']  . "\n";
   $body .= 'Postcode ' . $_POST['postcode']  . "\n";
   $body .= 'State ' . $_POST['state']  . "\n";
   $body .= 'Country ' . $_POST['country']  . "\n";
   $body .= 'Contact by telephone? ' . ($_POST['contactbytelephone'] ? 'Yes' : 'No' ) . "\n";
   $body .= 'Telephone number ' . $_POST['phone'] . "\n";
   $body .= 'Monthly newsletter? ' . ($_POST['receivenewsletter'] ? 'Yes' : 'No' ) . "\n";
   //echo str_replace("\n", '<br>', $body);

   $to = 'divesydney@optus.com.au';
   $from = "diveadventures <sydney@diveadv.com.au>";
   $subject = "Holiday information - From www.diveadventures.com.au";
   $headers = "From: $from";
   mail($to, $subject, $body, $headers);

   $smarty->assign('submit', 1);
   
}


smarty_constants(&$smarty);

$smarty->display('head.tpl');
$smarty->display('contact.tpl');
$smarty->display('tail.tpl');


function smarty_constants(&$smarty)
{
   $HEARDS = array (
      'The Internet' => 'The Internet',
      'Word of Mouth' => 'Word of Mouth',
      'Business reference' => 'Business reference',
      'Advertisement' => 'Advertisement',
      'Dive magazine' => 'Dive magazine',
      'Other' => 'Other'
   );

   $smarty->assign('heards', $HEARDS);


   $HOLIDAYS = array (
      'Australia / Great Barrier Reef' => 'Australia / Great Barrier Reef',
      'Cocos (Keeling) / Christmas Islands' => 'Cocos (Keeling) / Christmas Islands',
      'Cook Islands' => 'Cook Islands',
      'East Timor' => 'East Timor',
      'Fiji' => 'Fiji',
      'Indonesia' => 'Indonesia',
      'Liveaboards' => 'Liveaboards',
      'Malaysia' => 'Malaysia',
      'Maldives' => 'Maldives',
      'Marshall Islands' => 'Marshall Islands',
      'Micronesia' => 'Micronesia',
      'Niue' => 'Niue',
      'Papua New Guinea' => 'Papua New Guinea',
      'Philippines' => 'Philippines',
      'Red Sea / Egypt' => 'Red Sea / Egypt',
      'Solomon Islands' => 'Solomon Islands',
      'Tahiti' => 'Tahiti',      
      'Tonga' => 'Tonga',      
      'Vanuatu' => 'Vanuatu',      
      'Western Samoa' => 'Western Samoa',      
      'Other (Refer to comments)' => 'Other (Refer to comments)'
      
   );

   $smarty->assign('holidays', $HOLIDAYS);


   $DIVING_TYPES = array (
      'Wreck' => 'Wreck',
      'Cave' => 'Cave',
      'Reef' => 'Reef',
      'Wall' => 'Wall',
      'Temperate Water' => 'Temperate Water',
      'Tropical' => 'Tropical',
      'Deep' => 'Deep',
      'Drift' => 'Drift',
      'Exploratory' => 'Exploratory',
      'Live-aboards' => 'Live-aboards',
      'Technical Diving' => 'Technical Diving',
      'Mixed Gases' => 'Mixed Gases'
   );

   $smarty->assign('diving_types', $DIVING_TYPES);


   $MONTHS = array (
      'January' => 'January',
      'February' => 'February',
      'March' => 'March',
      'April' => 'April',
      'May' => 'May',
      'June' => 'June',
      'July' => 'July',
      'August' => 'August',
      'September' => 'September',
      'October' => 'October',
      'November' => 'November',
      'December' => 'December'
   );

   $smarty->assign('months', $MONTHS);

   $YEARS = array (
      '2007' => '2007',
      '2008' => '2008',
      '2009' => '2009',
      '2010' => '2010',
   );

   $smarty->assign('years', $YEARS);

   return;
}

?>


I've tried to add javascript code from the "phpForms" Demo to the head.tpl file. I added this, and tried to match the name="" for the fields I want to be checked for content

Code:

<script language="JavaScript">
<!--
var aIds = Array( 'pg_801e316097' );
function ChangePage( to_hide, to_show )
{
   var tbl_hide = document.getElementById( aIds[to_hide] );
   var tbl_show = document.getElementById( aIds[to_show] );

   if ( typeof(tbl_hide)=='object' && tbl_hide!=null )
    if ( typeof(tbl_show)=='object' && tbl_show!=null )
    {
       tbl_hide.style.display = 'none';
       tbl_show.style.display = '';
    }
}
function NotEmpty( old_res, id, field_title )
{
   if ( old_res==0 )
   {
      var el = document.getElementById( id );
      if ( typeof(el)=='object' && el!=null )
        if ( el.value=='' )
        {
           alert( 'You should fill "' + field_title + '"' );
           return 1;
        }
   }
   return 0;
}
function Email( old_res, id, field_title )
{ // test
   if ( old_res==0 )
   {
      var el = document.getElementById( id );
      if ( typeof(el)=='object' && el!=null )
      {
         var re = /^[a-zA-Z0-9_\.\-]+@([a-zA-Z0-9][a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/;

         if (el.value.search(re) != -1 )  return 0;
         else
         {
            alert( '"'+field_title+'" must be email' );
            return 1;
         }
      }
   }
   return 0;
}

-->
</script>


Can anyone please post any helpful suggestions about the simplest and most effective way I can enable field validation for the name, email address and phone number.

I just want something that will check that the fields are filled, I don't really need complex format validation.
Back to top
View user's profile Send private message
master_kaos
Smarty Regular


Joined: 02 Aug 2007
Posts: 54

PostPosted: Tue Nov 27, 2007 3:40 pm    Post subject: Reply with quote

you may need to add {literal} tags around the javascript
Or you could maybe try using the SmartyValidate plugin
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=7561
But that can add some complication if all you need is really basic.
Back to top
View user's profile Send private message
random1739
Smarty Rookie


Joined: 03 Jul 2007
Posts: 5

PostPosted: Sun Dec 02, 2007 10:53 pm    Post subject: Reply with quote

Thanks for the reply. Wouldn't the smarty validate plugin only work for the PHP code? At the moment the PHP side of things works ok.

I just want to add code (javascript OR php, whatever is simpler) that will stop the form from being emailed to me if a field is left empty.

I tried using the littoral tags, and I stopped getting error messages, but the form was still being emailed to me when the fields were empty Confused
Back to top
View user's profile Send private message
master_kaos
Smarty Regular


Joined: 02 Aug 2007
Posts: 54

PostPosted: Tue Dec 04, 2007 3:10 pm    Post subject: Reply with quote

well for an example here is a simple form

Code:

<script type='text/javascript'>
   function validateForm(form)
   {
      error = "";
      if(form.name.value == "")
           {
                  error += "You must enter in your name.\n";
      }      
      if(form.email.value == "")
      {
         error += "You must enter in an email\n";         
      }
      if(error != "")
      {
         alert(error);   
         return false;
      }
      return true;
   
   }
</script>
<form name="main" onSubmit="return validateForm(this);">
   Name: <input type="text" name="name">
   Email: <input type="text" name="email">
   <input type="submit" value="Submit">
</form>



You can then keep adding more fields as you need[/code]
Back to top
View user's profile Send private message
Ancient
Smarty Pro


Joined: 07 Jul 2007
Posts: 196
Location: Omaha, Nebraska, United States of America

PostPosted: Wed Dec 05, 2007 2:54 am    Post subject: Reply with quote

try in php
Code:
if(empty($this) || empty($this))
{
$error = 'You didn't fill out all the fields.';
}
//assign all info
$smarty->assign(array(
'HOLIDAYS' => $_POST['blah'],
'DOB' => $_POST['blah'])
);
// redisplay form with post info inside


the form should have <input value={$HOLIDAYS} even if there is not data / assigned var created yet incase they have to be returned to the form.

But I think javascript will be the best way to avoid possible syntax issues.
_________________
Smarty all the way.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
random1739
Smarty Rookie


Joined: 03 Jul 2007
Posts: 5

PostPosted: Sun Dec 09, 2007 10:50 pm    Post subject: Reply with quote

Thanks for your reply. It's taken me a few months on and off (as a javascript and PHP newbie), but I've replaced the Smarty PHP page with a new javascripted page which successfully rejects forms without a contact email or phone number Very Happy


The smarty-based page that our previous webmaster set up just didn't lend itself to any easy form of validation/rejection at all.

Thanks a lot to those who replied. For anyone who might have a similar problem, I used the jscript & PHP code written lower down on this page as a template:

http://www.ibdhost.com/contact/

It took a while to customise what I needed, but it seems to work great for me at the moment
Back to top
View user's profile Send private message
elenahusky
Smarty n00b


Joined: 17 Jan 2012
Posts: 1
Location: Australia

PostPosted: Tue Jan 17, 2012 5:41 am    Post subject: vanuatu holidays Reply with quote

Hey there..!! I have been following your post since last six months and I must say I got so many information.
Thank you for sharing it with us.
vanuatu holidays
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP