Form help

Probably getting boring, but I’m developing a site for a friend (www.ututor.com.au)
I am using a PHP file (MailHandler) and a forms.JS and HTML
I am TRYING to get the select drop down menu to work (ID of “days”, but no luck.
Basically, I want it to accept a choice of day of week and preferred time for students to book a session

HTML is:

Contact form submitted!
We will be in touch soon.
*This is not a valid name. *This field is required.
*This is not a valid email address. *This field is required.
*This is not a valid phone number. *This field is required.
*This is not a valid subject. *This field is required.

Preferred Tutoring Day

Monday Tuesday Wednesday Thursday Friday Saturday Sunday

Please provide a detailed description of course topics and questions that you would like some help with. The more specific you are, the better your assigned tutor is able to prepare for your session.

Message must be a minimum of 20 characters: *The message is too short. Must be a minimum of 20 characters. *This field is required. [b] PHP:[/b] <?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";
}

?>

JS

(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:‘[email protected]’,
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)
:frowning:
.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:‘[email protected]
})
})

QUESTION:
How to get the drop down menu to work in the php and JS - PLUS - I need to be able to have a drop down that will allow students to select a time slot (10:am to 11:00am).

Thanks
B

I don’t know what that option stuff is in the value field for the drop down, but that’s where the day number should be. Or you can do a switch or a lot of if statements to get those.

So - is that the problem?
Should it read

Preferred Tutoring Day

Monday Tuesday Wednesday Thursday Friday Saturday Sunday

???

Well, to explain SELECT dropdowns a little…

A SELECT allows you to pick an OPTION from one in the list.
Each OPTION has a name, a value and can have other tags such as an ID for use in javascript or style code to alter every other option background color, or others.

A user presses the down-arrow and SELECTS one of these. Either when the FORM is POSTED or when a Javascript is called, the “VALUE” inside the OPTION can be used for data. Therefore, this value can be option1, option2 option3 or the actual values, Monday, Tuesday, etc… Either is very correct.

Now, to use the data in the VALUE, your program has to do something with the value. Something like compare it ( If SELECT_NAME.value=“Monday” blah blah blah…)
Or, in PHP ( <?PHP echo $_POST['SELECT_NAME']; ?> ) to print the value…

So, basically, you can put anything you wish into the VALUE. But, what or where you use this data is important. If you are wanting the user to select a day of the week, Monday, Tuesday, etc would be better than Option1,2,3… You want to use Monday, Tuesday, etc…

Lastly, normally you have an ACTION field in your FORM that points to the PHP page. When the form is posted, it is sent to the PHP code to be handled. I do not see where the FORM is actually POSTED out to the PHP code. Along with the day of the weeks, this might be the problem… Can you tell us at what point the code fails?

The php is there, he just didn’t use the php tags to seperate it out. Its in there with the jquery code.

What I’ve done is to change the option1 to the actual day name
I’ve also corrected an error in the PHP from $days = $POST[‘days’]; to $days = $_POST[‘days’];

The code CURRENTLY fails to provide the day selected.

I’m only new to PHP > so even (<?PHP echo $_POST['SELECT_NAME']; ?>) > I would need to know where to place it and if the word SELECT_NAME is required in the HTML somewhere

Put this somewhere in your processing code
[php]echo “

”;
print_r($_POST);
echo “
”;[/php]

What it does is give you a visual reference of what’s contained in the POST array. It’ll look something like
array (
[key] => value
[key] => value
)

That tiny bit of code is a valuable tool for troubleshooting. it shows you exactly what’s being passed (or not being passed) to the php code.

Some of it is the php. With what you have, days can never equal nope. != ‘nope’ isn’t needed. if you’re just looking to see if its selected, just do if(isset($_POST[‘days’])) {}. You also don’t need to validate it. Its not something the user has to type in.

No luck > the print did nothing and the form when entered doesn’t change.

Here i the PHP, JS and HTML now

PHP:

<?php $owner_email = $_POST["owner_email"]; $headers = 'From:' . $_POST["email"]; $subject = 'A message from your site visitor ' . $_POST["name"]; $messageBody = ""; if($_POST['name']!='nope'){ $messageBody .= '

Visitor: ' . $_POST["name"] . '

' . "\n"; $messageBody .= '
' . "\n"; } if($_POST['email']!='nope'){ $messageBody .= '

Email Address: ' . $_POST['email'] . '

' . "\n"; $messageBody .= '
' . "\n"; } if($_POST['state']!='nope'){ $messageBody .= '

State: ' . $_POST['state'] . '

' . "\n"; $messageBody .= '
' . "\n"; } if($_POST['phone']!='nope'){ $messageBody .= '

Phone Number: ' . $_POST['phone'] . '

' . "\n"; $messageBody .= '
' . "\n"; } if($_POST['subject']!='nope'){ $messageBody .= '

Subject of study: ' . $_POST["subject"] . '

' . "\n"; $messageBody .= '
' . "\n"; } if(isset($_POST['days'])) { $messageBody .= '

Preferred day: ' . $_POST["days"] . '

' . "\n"; $messageBody .= '
' . "\n"; } if($_POST['fax']!='nope'){ $messageBody .= '

Fax Number: ' . $_POST['fax'] . '

' . "\n"; $messageBody .= '
' . "\n"; } if($_POST['message']!='nope'){ $messageBody .= '

Message: ' . $_POST['message'] . '

' . "\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"; } echo "
";
print_r($_POST);
echo "
"; ?>

HTML:

Preferred Tutoring Day

Monday Tuesday Wednesday Thursday Friday Saturday Sunday

JS: (I’ve removed validation
(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:‘[email protected]’,
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’},
“.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)
:frowning:
.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’,
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:‘[email protected]
})
})

I added the php code but nothign happened. My test was a booking on the page booking form
http://ututor.com.au/booking.html

Think I;m making a mess of this…

I’ll do some tests when i get home from work this evening and see what’s going on.

Well, without having all your code (unless what you posted is all the html, then its no wonder it doesn’t work), but this is what i tested.
[php]<?php
if(isset($_POST[‘submit’])) {

echo "<pre>";
print_r($_POST);
echo "</pre>";

}
?>

Untitled Document
Contact form submitted!
We will be in touch soon.

Preferred Tutoring Day

Monday Tuesday Wednesday Thursday Friday Saturday Sunday
[/php]

and this was the result:

Array ( [days] => Monday [submit] => Submit )

If its not working for you, then there’s something else going on in other parts of the code.

Richei,
I may not be clear on this, but, can you tell me where his code actually POSTS the form?
I do not see any actually posting address or any way that this code is sent to the PHP code.
Most likely I am not seeing something, but, I see some SUBMIT code inside the INIT code, but,
do not understand how that posts anything to make the PHP code kick in.
Please explain this to me so I can follow it… Thanks, E…

I just took the part he was asking about. I really hope that’s not all of his code - it can’t be. Like you said, the action and method is missing (some browsers will default to post though). The form isn’t closed at all. The php needs to be on top of the page though, which may be why he’s not seeing any results.

LOL, WHEW! I thought I was BLIND…

I did note that he had some JS SUBMIT code inside of his JS INIT code. So, I thought he was posting on initialization of his page. But, could not find where it was going… Glad it wasn’t me…

I think he should just get it posting without all the Javascript and then add some of it in a little by little, or post all of his data so we can actually understand what he is trying to do…

If you look at the php, he’s putting html code in the messageBody, then under all that, he’s removing it all. Kinda backwards :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service