# Solved: form help



## ms_khw (Jul 12, 2004)

I am trying to create a form that provides the use of a calendar. I finally found a script that works in all browsers. The completed form will not send. How do I set it up to send to my mailbox and how do I redirect to another page. I have added my code, please help.

Contact
















*My Company*

My Phone
My address

Name: 

Email: 

Phone: 

City: 

*Note booking dates can only be provided according to availability.

Request your booking date: 

Please input another date: 

Photo Session Request: 
Please Select
Maternity
Maternity/Newborn Pkg.
Hospital Newborn
Baby
Child/Chrildren
Family
Couple/Single
Seniors
Prom
Engagement
Wedding
Fearless Bride

Message:



  


----------



## dukevyner (Nov 4, 2011)

Well the first thing we need to address... is that when posting on here code should be put in quotes by pressing the speech bubble button on the text editor... this makes it a lot easier for us to read... now to answer your question...

The form is created correctly... in order to do what you want... that is email the info and and redirect the user... you will need to use a server side scripting... and seeing as php is the most common (and my favourite or in the US favorite) i'll show you using that. i made some changes to your current code.... they are in red


> *My Company*
> 
> My Phone
> My address
> ...


 Here is the process page... this must be done in php... and should be called "process.php"

```
<?php if(isset($_POST['submit'])){ //checks to make sure that they have actually submit a form, and not just entered the url
$name = $_POST['name']; //This will get the data that is posted by the form, the 'name' is the is the id of the input... in this case the persons name
$email = $_POST['email']; //"$email =" Is used to define a variable in this case the name of the variable is email... the $_POST is the same... but for email
$phone = $_POST['phone']; //Now we continue untill we have got a variable for each input in the form
$city = $_POST['city'];
$date = $_POST['date']; // The item in the form will have to have an id='date' added
$date2 = $_POST['date2']; // The item in the form will have to have an id='date2' added
$session = $_POST['session'];
$message = $_POST['message'];
// Now that we have recived all the data from the form, we can email it
$subject = "contact";
   $today = date("D M j Y g:i:s a T");

   $pageload ='
Name: $name
E-mail: $email
Phone: $phone
Date: $date
 Date2: $date2
 Session: $session
 Message: $message';

$content = "Message sent at ".$today." \n\n$pageload";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: [email][email protected][/email]' . "\r\n";
$headers .= 'From: [email][email protected][/email]' . "\r\n";
   mail($to, $subject, $content, $headers); //sends email with data.
echo''; //redirects the user to the page you choose, such as a thank you page. Note this can be done in php using header location... but i always have trouble with it on my testing server... so to insure that it worked... i used a http-equiv...in case you didn't notice i like to use ellipses...

} else{die('You did not complete the form please [URL=http://yoursite.com/formurl] back[/URL]');}//This takes anyone who did not submit a form but came to this page back to the from.
?>
```
Hope this helps... and sorry for all the ellipses...

-Luke


----------



## ms_khw (Jul 12, 2004)

I can't thank you enough for all of your assistance. I have followed everything as you said but the calendar does not work/appear. Going back in dreamweaver I am alerted to a syntax error on line 37. Could this be the problem? Also what doctype should I be using for this? Checking all browsers, everything remains in place. Did I do something wrong with that line while adding my url?


> header('Location: http://mysite/thankyou.html'); //redirects the user to the page you choose, such as a thank you page.


msk


----------



## dukevyner (Nov 4, 2011)

Hi again... i'm sorry i did make an edit... i was having trouble with the header location replace the whole line with the one that... should be above...

```
echo''; //redirects the user to the page you choose, such as a thank you page. Note this can be done in php using header location... but i always have trouble with it on my testing server... so to insure that it worked... i used a http-equiv...in case you didn't notice i like to use ellipses...
```


----------



## ms_khw (Jul 12, 2004)

A little confused. So you are placing :


> echo''


 on line 37? Is this correct? And what doctype should I be using?
If yes should it appear as the following



> header(echo');[/url] //redirects the user to the page you choose, such as a thank you page.


----------



## dukevyner (Nov 4, 2011)

I made a few mistakes in the previous code...but none that should be causing a santax error... please try replacing all privious code in "*process.php*" with the following


> <?php if(isset($_POST['submit'])){ //checks to make sure that they have actually submit a form, and not just entered the url
> $name = $_POST['name']; //This will get the data that is posted by the form, the 'name' is the is the id of the input... in this case the persons name
> $email = $_POST['email']; //"$email =" Is used to define a variable in this case the name of the variable is email... the $_POST is the same... but for email
> $phone = $_POST['phone']; //Now we continue untill we have got a variable for each input in the form
> ...


----------



## dukevyner (Nov 4, 2011)

ms_khw said:


> I can't thank you enough for all of your assistance. I have followed everything as you said but the calendar does not work/appear. Going back in dreamweaver I am alerted to a syntax error on line 37. Could this be the problem? Also what doctype should I be using for this? Checking all browsers, everything remains in place. Did I do something wrong with that line while adding my url?
> 
> msk


actually on my code i don't have a line 37... and i didn't use a doctype, you can use any doctype

*Note:* in my last reply i advise you to replace the php code as i made a few mistakes these should NOT have caused a syntax error...but would not have provided the results correctly


----------



## ms_khw (Jul 12, 2004)

I must be doing something wrong. There does not appear to be an error on line 37 now. But it still does not show the calendar.
This is my form saved as php:


> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> 
> Test Form
> 
> ...


And this is my process.php



> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> 
> Untitled Document
> 
> ...


----------



## dukevyner (Nov 4, 2011)

I'm sorry you miss understood my edit to you html file... Hold on(using phone booting comp)


----------



## dukevyner (Nov 4, 2011)

I intended for you to only change the stuff in red in the original html file... lol sorry for the miss understanding... your html file should look like this


> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> 
> Contact
> 
> ...


----------



## ms_khw (Jul 12, 2004)

How can I ever thank you enough! I worked on this for days, banging my head against the wall. Everything works beautifully in all browsers! Thrilled indeed. Thank you for taking the time to assist me.
msk


----------



## dukevyner (Nov 4, 2011)

ms_khw said:


> How can I ever thank you enough! I worked on this for days, banging my head against the wall. Everything works beautifully in all browsers! Thrilled indeed. Thank you for taking the time to assist me.
> msk


you may want to make a few changes, to "validate" the form. This makes sure that people can't people can't submit incomplete data such as leaving the phone blank, or name, or the email not being real... if you would like some assistance in doing so i'd be happy to help


----------



## ms_khw (Jul 12, 2004)

Yes validating the form is extremely important, if it is not too much to ask I'd really appreciate the help.


----------



## dukevyner (Nov 4, 2011)

There are many methods for validating... it simply depends on how you want to do it...you can do it in javascript, php, even dreamweaver has a simple validation


----------



## ms_khw (Jul 12, 2004)

I have dreamweaver cs5.5 I have heard about spry validating, but not familiar with it. Is that the only method within dreamweaver? Everything is working so well I'd hate to mess things up.


----------



## dukevyner (Nov 4, 2011)

I will do one in php because it's my second language... of course you can find a js one or even do it your self in dreamweaver... i am just offering a choice.. in order for it to work as well as possible... the main document... must be changed to a php document... this is as simple as changing it from form.html... to form.php(form.php is not a manditory name...just an example besure that all links to the page are changed... including the ones in process.php)
Form page

```
<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Contact

[TABLE]
[TR][TD]
[/TD][/TR]
[/TABLE]

[TABLE]
[TR]
[TD][IMG]http://forums.techguy.org/images/parents_walking_child.jpg[/IMG][/TD]
[TD]

[B][SIZE=15]My Company[/SIZE][/B]

My Phone
My address

<?php if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
		echo '[LIST]';
		foreach($_SESSION['ERRMSG_ARR'] as $msg) {
			echo '[*]',$msg,''; 
		}
		echo '[/LIST]';
		unset($_SESSION['ERRMSG_ARR']);}?>

Name: 

Email:  

Phone: 

City:    

*Note booking dates can only be provided according to availability.

Request your booking date: 

Please input another date:   

Photo Session Request:         
Please Select
Maternity
Maternity/Newborn Pkg.
Hospital Newborn
Baby
Child/Chrildren
Family
Couple/Single
Seniors
Prom
Engagement
Wedding
Fearless Bride

Message:

 

[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[/TABLE]
```
Process.php

```
<?php session_start(); if(isset($_POST['submit'])){ //checks to make sure that they have actually submit a form, and not just entered the url
$name = $_POST['name']; //This will get the data that is posted by the form, the 'name' is the is the id of the input... in this case the persons name
$email = $_POST['email']; //"$email =" Is used to define a variable in this case the name of the variable is email... the $_POST is the same... but for email
$phone = $_POST['phone']; //Now we continue untill we have got a variable for each input in the form
$city = $_POST['city'];
$date = $_POST['date']; // The item in the form will have to have an id='date' added
$date2 = $_POST['date2']; // The item in the form will have to have an id='date2' added
$session = $_POST['session'];
$message = $_POST['message'];

if($name == '') { //checks if name is empty
		$errmsg_arr[] = 'Name missing.';
		$errflag = true;
	}
	if($email == '') { //checks if email is empty
		$errmsg_arr[] = 'E-mail missing.';
		$errflag = true;
	}
	if(!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $email)&& $email!=''){//checks if email is real and not empty
		$errmsg_arr[] = 'E-mail is not real.';}//checks if email is real
	if($phone == '' || strlen($phone)!=10) {//checks to make sure that the phone number is 10 numbers long
		$errmsg_arr[] = 'Phone number missing or not valid.';
		$errflag = true;
	}
	if($city == '') { //checks if city is empty
	$errmsg_arr[] = 'City missing.';
	$errflag = true;
	}
	if($date == '') { //checks if date is empty
		$errmsg_arr[] = 'Booking date missing.';
		$errflag = true;
	}
	//gets the date submitted by the user and breaks it down to $y=yyyy, $m=mm, and $d=dd... this will be used to insure that the user 			can not pick a day...that has already passed, or the current day
	$y = current(explode("-", $date));
	$mdelete = substr ($date, 0, (stripos ($date, "-")+1));
	$m = str_replace ($mdelete, '', $date);
	$m = current(explode("-", $m));
	$ddelete = substr ($date, 0, (stripos ($date, "-")+4));
	$d = str_replace ($ddelete, '', $date);
	$check = mktime(0, 0, 0, $m, $d, $y);
	$todaydate = mktime(0, 0, 0, date("m"), date("d"), date("y"));
	//this checks if the date is in the past or today... and displays the propper error
	if ($check == $todaydate) {
	  $errmsg_arr[] = 'Booking date must not be today.';
		$errflag = true;
	}if ($check < $todaydate && $check!=0) {
	  $errmsg_arr[] = 'Booking date must not be in the past.';
		$errflag = true;
	}
	if($date2 == '') { //checks if date2 is empty
	$errmsg_arr[] = ' Other booking date missing.';
	$errflag = true;
	} 
	if($session == 'select') { //checks if session is empty
		$errmsg_arr[] = 'Photo session type missing.';
		$errflag = true;
	}

	//If there are input validations, redirect back to the form
	if($errflag) {
		$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
		session_write_close();
		echo''; die();}

//If no errors, and now that we have recived all the data from the form, we can email it
$subject = "contact";
   $today = date("D M j Y g:i:s a T");

   $pageload ='
Name: '.$name.'
E-mail: '.$email.'
Phone: '.$phone.'
City: '.$city.'
Date: '.$date.'
 Date2: '.$date2.'
 Session: '.$session.'
 Message: '.$message.'';

$content = "Message sent at ".$today." \n\n$pageload";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: [email protected]' . "\r\n";
$headers .= 'From: [email protected]' . "\r\n";
   mail($to, $subject, $content, $headers); //sends email with data.
   echo''; //redirects the user to the page you choose, such as a thank you page.

} else{die('You did not complete the form please [URL=form.php] back[/URL]');}//This takes anyone who did not submit a form but came to this page back to the from.
?>
```
If you try this out... witch you are welcome to here http://channeljackers.co.cc/1.php... you will see there is quite alot going on


----------



## dukevyner (Nov 4, 2011)

The process page will now check to make sure that all text boxes(aside from "message" i thought this one was optional if not tell me, or if you are feeling confident try to fix it your self... it should be just copy paste and change a word or too) have been filled and will redirect back to the form, and display all errors in the form... it will also make sure that the dates the user enter are not the current date, or dates in the past, and will display an error if either... the "phone" text box was configured to only allow numbers... display an error code if the number is not 10 digits long(standard phone length... this can be configured)


----------



## dukevyner (Nov 4, 2011)

While I did code this for you, i hope that my comments throughout the code have helped you understand what some of php does and how to do it for next time... remember ctrl+c, ctrl+v is just the computer form of flattery, don't be afraid to go back into your old work, even if it isn't your code and copy paste it to a page you are working...


----------



## ms_khw (Jul 12, 2004)

Okay, my main page is a php, now I have added your


> <?php session_start();?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


 Should I copy & paste the entirety of the code, replacing what I have? And should I replace the code for the process page with what you have just shown here. A little confused on how to validate. And yes your comments were extremely helpful in the code. PHP is confusing to me, but I'm saving all of your posts and comments for future use. I can't thank you enough.


----------



## dukevyner (Nov 4, 2011)

ms_khw said:


> Okay, my main page is a php, now I have added your Should I copy & paste the entirety of the code, replacing what I have? And should I replace the code for the process page with what you have just shown here. A little confused on how to validate. And yes your comments were extremely helpful in the code. PHP is confusing to me, but I'm saving all of your posts and comments for future use. I can't thank you enough.


Yes... there are quite a few changes... replace what you have completely with what i put above, don't worry i only changed what i needed to... and that wasn't much on your page


----------



## ms_khw (Jul 12, 2004)

I have replaced everything as you advised. But now I'm getting syntax errors on lines 134, 350, 353, 354 and 356 on my main page.


----------



## dukevyner (Nov 4, 2011)

ms_khw said:


> I have replaced everything as you advised. But now I'm getting syntax errors on lines 134, 350, 353, 354 and 356 on my main page.


Yeah there seems to be a problem with the way it displays on this site and it copies wrong...


----------



## dukevyner (Nov 4, 2011)

delete the file... and download this http://www.mediafire.com/?9rw5y382aw2hkv4


----------



## ms_khw (Jul 12, 2004)

What is it, a new form?


----------



## dukevyner (Nov 4, 2011)

ms_khw said:


> What is it, a new form?


Is the same file... You can rename it but be sure to do so to any links in process. Php


----------



## dukevyner (Nov 4, 2011)

dukevyner said:


> Is the same file... (the main file as u say) You can rename it but be sure to do so to any links in process. Php


----------



## ms_khw (Jul 12, 2004)

Ok, I've downloaded the file as you said and should I give it an extension of php or html. Now I will copy and paste your process.php, correct?


----------



## dukevyner (Nov 4, 2011)

yes... be sure to replace everything in it here it is again


> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> 
> <?php session_start(); if(isset($_POST['submit'])){ //checks to make sure that they have actually submit a form, and not just entered the url
> $name = $_POST['name']; //This will get the data that is posted by the form, the 'name' is the is the id of the input... in this case the persons name
> ...


----------



## ms_khw (Jul 12, 2004)

I've done as you say, _hopefully_. I've copied your process.php code and placed my email address where you have indicated. Now the form does not send. I've tried it by completing the form and leaving areas empty, it does not tell me that they must be completed. I've gone wrong somewhere.

My process page:


> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> 
> Contact Form
> 
> ...


My form saved as form.php



> <?php session_start();?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> 
> Contact
> ...


----------



## dukevyner (Nov 4, 2011)

> [TD]
> 
> 
> 
> ...


 remove what is in red...and add what is in green... sorry that was my fault... i am tired
p.s. be sure to only change this little bit...*leave the rest of the file alone*


----------



## ms_khw (Jul 12, 2004)

Once again I can't thank you enough. I know you put a lot of work into helping me. I am so grateful, and yes you do need some rest after everything I've put you through. The form appears to be working as it should.

One question. If I wanted to make it so my form could be submitted without putting anything in the Booking date, Other booking date and Photo session, if it is a simple matter how do I do it. If I wanted the warnings to be in red say, is this an easy thing? Promise to let you rest after this.


----------



## dukevyner (Nov 4, 2011)

ms_khw said:


> Once again I can't thank you enough. I know you put a lot of work into helping me. I am so grateful, and yes you do need some rest after everything I've put you through. The form appears to be working as it should.
> 
> One question. If I wanted to make it so my form could be submitted without putting anything in the Booking date, Other booking date and Photo session, if it is a simple matter how do I do it. If I wanted the warnings to be in red say, is this an easy thing? Promise to let you rest after this.


Sorry was quite late here in aussie land yes its quite simple to allow specific items be left blank... all changes will be in the process.php

i will mark what to remove in red


> <?php session_start(); if(isset($_POST['submit'])){ //checks to make sure that they have actually submit a form, and not just entered the url
> $name = $_POST['name']; //This will get the data that is posted by the form, the 'name' is the is the id of the input... in this case the persons name
> $email = $_POST['email']; //"$email =" Is used to define a variable in this case the name of the variable is email... the $_POST is the same... but for email
> $phone = $_POST['phone']; //Now we continue untill we have got a variable for each input in the form
> ...


 and some css should be easy to change the color of the error messages... i tried but it didn't work... i'll look into it


----------



## dukevyner (Nov 4, 2011)

change this in the top of your form page red=remove Green=Add


> <?php session_start();?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> 
> Contact


 it may be hard to see... you need to replace the # with a period this will turn the errors red


----------



## ms_khw (Jul 12, 2004)

Ok. If it proves hard to see, I'll leave well enough alone. Once again thank you for all of your help and patience.


----------



## dukevyner (Nov 4, 2011)

ms_khw said:


> Ok. If it proves hard to see, I'll leave well enough alone. Once again thank you for all of your help and patience.


I meant the dot may be hard to see on the code


----------



## ms_khw (Jul 12, 2004)

Thank you, everything is working fine. I can't tell you how immensely appreciative I am of all of your help. msk


----------



## dukevyner (Nov 4, 2011)

ms_khw said:


> Thank you, everything is working fine. I can't tell you how immensely appreciative I am of all of your help. msk


You're very welcome


----------

