Contact form php element and MAYBE some JS if applicable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bernster
Forum Newbie
Posts: 5
Joined: Sun Mar 11, 2012 7:00 am

Contact form php element and MAYBE some JS if applicable

Post by Bernster »

Hi all... first timer, so go EASY!
I'm developing a template website for a friend (its a Template Monster site) and he wants me to add a dropdown menu (select) for day of week and time of booking most suitable.

Website: http://www.ututor.com.au

Page in Question: http://ututor.com.au/booking.html

Help required: I've added the field to select the day of week in HTML (visible in view source), but Can't get it to work (everything works but the day selected) - PLEASE NOTE HE ALSO WANTS TO ADD A TIME FOR STUDENTS TO BOOK.

MailHandler.php reads: (PLEASE NOTE > I have called the day select "days" > also need to add PreferredTime (help also needed here - separately)

Code: Select all

<?php
	$owner_email = $_POST["owner_email"];
	$headers = 'From:' . $_POST["email"];
	$subject = 'A message from your site visitor ' . $_POST["name"];
	$days = $POST['days'];
	$messageBody = "";
	
	if($_POST['name']!='nope'){
		$messageBody .= '<p>Visitor: ' . $_POST["name"] . '</p>' . "\n";
		$messageBody .= '<br>' . "\n";
	}
	if($_POST['email']!='nope'){
		$messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
		$messageBody .= '<br>' . "\n";
	}
	if($_POST['state']!='nope'){		
		$messageBody .= '<p>State: ' . $_POST['state'] . '</p>' . "\n";
		$messageBody .= '<br>' . "\n";
	}
	if($_POST['phone']!='nope'){		
		$messageBody .= '<p>Phone Number: ' . $_POST['phone'] . '</p>' . "\n";
		$messageBody .= '<br>' . "\n";
	}	
	if($_POST['subject']!='nope'){
		$messageBody .= '<p>Subject of study: ' . $_POST["subject"] . '</p>' . "\n";
		$messageBody .= '<br>' . "\n";
	}
	if($_POST['days']!='nope'){
		$messageBody .= '<p>Preferred day: ' . $_POST["days"] . '</p>' . "\n";
		$messageBody .= '<br>' . "\n";
	}
	if($_POST['fax']!='nope'){		
		$messageBody .= '<p>Fax Number: ' . $_POST['fax'] . '</p>' . "\n";
		$messageBody .= '<br>' . "\n";
	}
	if($_POST['message']!='nope'){
		$messageBody .= '<p>Message: ' . $_POST['message'] . '</p>' . "\n";
	}
	
	if($_POST["stripHTML"] == 'true'){
		$messageBody = strip_tags($messageBody);
	}

	try{
		if(!mail($owner_email, $subject, $messageBody, $headers)){
			throw new Exception('mail failed');
		}else{
			echo 'mail sent';
		}
	}catch(Exception $e){
		echo $e->getMessage() ."\n";
	}
?>
So thats the issue > can someone please assist. I'm sure I'm close?

JS for those required reads:

Code: Select all

(function($){
	$.fn.extend({
		forms:function(opt){
			if(opt===undefined)
				opt={}
			this.each(function(){
				var th=$(this),
					data=th.data('forms'),
					_={
						errorCl:'error',
						emptyCl:'empty',
						invalidCl:'invalid',
						successCl:'success',
						successShow:'4000',
						mailHandlerURL:'bin/MailHandler.php',
						ownerEmail:'admin@ututor.com.au',
						stripHTML:true,
						smtpMailServer:'localhost',
						targets:'input,textarea',
						controls:'a[data-type=reset],a[data-type=submit]',
						validate:true,
						rx:{
							".name":{rx:/^[a-zA-Z'][a-zA-Z-' ]+[a-zA-Z']?$/,target:'input'},
							".state":{rx:/^[a-zA-Z'][a-zA-Z-' ]+[a-zA-Z']?$/,target:'input'},
							".email":{rx:/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i,target:'input'},
							".phone":{rx:/^\+?(\d[\d\-\+\(\) ]{5,}\d$)/,target:'input'},
							".subject":{rx:/^[a-zA-Z'][a-zA-Z-' ]+[a-zA-Z']?$/,target:'input'},
							".days":{rx:/^[a-zA-Z'][a-zA-Z-' ]+[a-zA-Z']?$/,target:'input'},
							".fax":{rx:/^\+?(\d[\d\-\+\(\) ]{5,}\d$)/,target:'input'},
							".message":{rx:/.{20}/,target:'textarea'}
						},
						preFu:function(){
							_.labels.each(function(){
								var label=$(this),
									inp=$(_.targets,this),
									defVal=inp.val(),
									trueVal=(function(){
												var tmp=inp.is('input')?(tmp=label.html().match(/value=['"](.+?)['"].+/),!!tmp&&!!tmp[1]&&tmp[1]):inp.html()
												return defVal==''?defVal:tmp
											})()
								trueVal!=defVal
									&&inp.val(defVal=trueVal||defVal)
								label.data({defVal:defVal})								
								inp
									.bind('focus',function(){
										inp.val()==defVal
											&&(inp.val(''),_.hideEmptyFu(label),label.removeClass(_.invalidCl))
									})
									.bind('blur',function(){
										!inp.val()
											?inp.val(defVal)										
											:(_.isValid(label)
												?_.showErrorFu(label)
												:_.hideErrorFu(label)),
											(_.isEmpty(label)
												?_.showEmptyFu(label)
												:_.hideEmptyFu(label))
									})
									.bind('keyup',function(){
										label.hasClass(_.invalidCl)
											&&_.isValid(label)
												?_.showErrorFu(label)
												:_.hideErrorFu(label)
									})
								label.find('.'+_.errorCl+',.'+_.emptyCl).css({display:'block'}).hide()
							})
							_.success=$('.'+_.successCl,_.form).hide()
						},
						isValid:function(el){
							var ret=true,
								empt=_.isEmpty(el)
							if(empt)
								ret=false,
								el.addClass(_.invalidCl)
							else
								$.each(_.rx,function(k,d){
									if(el.is(k))
										d.rx.test(el.find(d.target).val())
											?(el.removeClass(_.invalidCl),ret=false)
											:el.addClass(_.invalidCl)
								})
							return ret
						},
						isEmpty:function(el){
							var tmp
							return (tmp=el.find(_.targets).val())==''||tmp==el.data('defVal')
						},
						validateFu:function(){							
							_.labels.each(function(){
								var th=$(this)								
								_.isEmpty(th)
									?_.showEmptyFu(th)
									:_.hideEmptyFu(th)
								_.isValid(th)
									?_.showErrorFu(th)
									:_.hideErrorFu(th)
							})
						},
						submitFu:function(){
							_.validateFu()
							if(!_.form.has('.'+_.invalidCl).length)
								$.ajax({
									type: "POST",
									url:_.mailHandlerURL,
									data:{
										name:$('.name input',_.form).val()||'nope',
										email:$('.email input',_.form).val()||'nope',
										phone:$('.phone input',_.form).val()||'nope',
										subject:$('.subject input',_.form).val()||'nope',
										days:$('.days input',_.form).val()||'nope',
										fax:$('.fax input',_.form).val()||'nope',
										state:$('.state input',_.form).val()||'nope',
										message:$('.message textarea',_.form).val()||'nope',
										owner_email:_.ownerEmail,
										stripHTML:_.stripHTML
									},
									success: function(){
										_.showFu()
									}
								})			
						},
						showFu:function(){
							_.success.slideDown(function(){
								setTimeout(function(){
									_.success.slideUp()
									_.form.trigger('reset')
								},_.successShow)
							})
						},
						controlsFu:function(){
							$(_.controls,_.form).each(function(){
								var th=$(this)
								th
									.bind('click',function(){
										_.form.trigger(th.data('type'))
										return false
									})
							})
						},
						showErrorFu:function(label){
							label.find('.'+_.errorCl).slideDown()
						},
						hideErrorFu:function(label){
							label.find('.'+_.errorCl).slideUp()
						},
						showEmptyFu:function(label){
							label.find('.'+_.emptyCl).slideDown()
							_.hideErrorFu(label)
						},
						hideEmptyFu:function(label){
							label.find('.'+_.emptyCl).slideUp()
						},
						init:function(){
							_.form=this
							_.labels=$('label',_.form)

							_.preFu()
							
							_.controlsFu()
															
							_.form
								.bind('submit',function(){
									if(_.validate)
										_.submitFu()
									else
										_.form[0].submit()
									return false
								})
								.bind('reset',function(){
									_.labels.removeClass(_.invalidCl)									
									_.labels.each(function(){
										var th=$(this)
										_.hideErrorFu(th)
										_.hideEmptyFu(th)
									})
								})
							_.form.trigger('reset')
						}
					}
				if(!data)
					(typeof opt=='object'?$.extend(_,opt):_).init.call(th),
					th.data({cScroll:_}),
					data=_
				else
					_=typeof opt=='object'?$.extend(data,opt):data
			})
			return this
		}
	})
})(jQuery)
$(function(){
	$('#contact-form').forms({
		ownerEmail:'admin@ututor.com.au'
	})
})
(again > I've thrown in "Days" but sure this is wrong.

Thanks
Bernie in Sydney
Last edited by Benjamin on Tue Oct 16, 2012 2:11 pm, edited 1 time in total.
Reason: Added [syntax=php||htm||css||javascript||sql||etc] - Please use [syntax] tags when posting code in the forums! Thanks.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Contact form php element and MAYBE some JS if applicable

Post by social_experiment »

Code: Select all

<?php
$days = $POST['days'];
// should be
$days = $_POST['days'];
?>
welcome to the forum; for future reference using the PHP Code button to encase code will make your code more readable and easier to give advice on
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
codems
Forum Newbie
Posts: 1
Joined: Tue Oct 16, 2012 10:11 am

Re: Contact form php element and MAYBE some JS if applicable

Post by codems »

the email engine you're using is not corresponding to the java variables, if you'd like, send me the original files for both the mailhandler.php and your java forms.js or whatever it's called, i'll go through them and make the changes and will send you the corrected ones. after you get the files and are happy maybe you could either donate a tip or put a good word for my username in the forum.
CodeNewb
Forum Newbie
Posts: 1
Joined: Fri Jan 18, 2013 10:03 am

Re: Contact form php element and MAYBE some JS if applicable

Post by CodeNewb »

Hey,

I'm new to this forum and am needing help with the same exact issue here. Has this been resolved?

I'm so close to getting this drop down menu to work, but I know something is missing...

Any help would be appreciated and returned

Thx
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Contact form php element and MAYBE some JS if applicable

Post by pickle »

I just skipped over your question because it wasn't specific enough. Generally I'd just move on, but since you're new I'll give you some advice.

Distill your problem, code, and question down to the bare minimum. "everything works but the day selected" isn't very specific. You're asking a forum of strangers to look through hundreds of lines of code to find a problem that hasn't really been described.

When posting, mention exactly what the problem is (ie: "when the form is POSTed, the chosen day is not selected when the page reloads"), what you've tried to fix it (ex: "I've echoed the POST data and the day isn't included"), and include only the relevant code so we don't have to find it ourselves. If more code is needed, it will be asked for.

I've found in the past, trying to distill the problem down usually forces me to examine the situation so closely that I solve the problem myself.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply