homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

php beginner

 12:39 pm on May 10, 2010 (gmt 0)

Im very very new at this, Ive managed to teach myself how to build a website in dreamweaver, but im stuck with the php codes.

what I want to do is have a contact form with a submit button (i have this) with a submit button that captures the information the visitor has entered and redirects them to a thankyou page.

I think i am right in saying that submit button needs php script, but what this means i really dont know or understand!

and will the information be sent to my hosting email account? is there anyway of side stepping this and sending it to another address?

a step by step from someone patient would be brilliant!

thank you!



 2:35 pm on May 10, 2010 (gmt 0)

Hi there Laurasamps,

Almost right there ;-p

The form needs a script, if using php to interpret the information being sent you specify this by using the action="" attribute within the form tag. The submit button is just the type of input you use to instigate the form process being done, hope that makes sense ;-p

There are two options for this, you can either make a new file (formhandler.php or whatever you want to call it) or, leave the action="" part blank (it still needs to be there for validation reasons I think) so that when the form gets submitted, the action defaults to itself - by which I mean you can have the php code on the same page as the form itself. You would just use a: header("location: myurl"); to get you to a page of your choosing.

Yes, you can specify a static email, or use the one that the user submit's (if you want them to recieve a copy of whatever they send to you).

For getting to grips with some of the code try:[w3schools.com ] as they are probably the best learning tool out there, read the tips, and try the code, if you get any problems, just post back on the forum, there are plenty of people here to offer advice or suggestions.

Before you implement any email or feedback form, read up on data sanitising, here is a good point to start: [webmasterworld.com ].

It may seem like a lot to take in but, you will be suprised how quickly it sinks in.

Have fun!



 2:54 pm on May 10, 2010 (gmt 0)

thanks for your advise, i'll give it a go!


 4:03 pm on May 10, 2010 (gmt 0)

Hi, I have spent a few hours trying to figure things out. i have made a very basic form to play around with so i dont ruin my good one, as below:

<form method="post" action="file:///C|/Users/Laura/Desktop/sendmail.php">
<input name="email" type="text" />
<br />
Message:<br />
<textarea name="message" rows="15" cols="40">
<br />
<input type="submit" name="button" id="button" value="Submit" />

i have then made the php page as below:

$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

mail( "laura.#*$!@hotmail.com", "Feedback Form Results",
$message, "From: $email" );
header( "Location: http://www.example.com/thankyou.html" );

i put it online to test it, and nothing - the submit button does not take the user to the thank you page or send the email

I thought I had it - im sure im close, i must be missing something small, HOPEFULLY?!

[edited by: dreamcatcher at 4:18 am (utc) on May 22, 2010]
[edit reason] Use example.com, thank you [/edit]


 4:40 pm on May 10, 2010 (gmt 0)

Here is your problem:


Your server does not have this path, eliminate the bolded and it should work, given that the script is in the root of the domain.

I know you're working in increments, but before going live, dig around here for cleansing user input, and why not to use $_REQUEST. What you have, so far, is a spam attack in the making. You already have form method="post", use $_POST instead of $_REQUEST.


 7:16 pm on May 10, 2010 (gmt 0)

Hi there laurasamps,

I think you are in need of a localised webserver so that you can test your work on the PC/Laptop you are using rather than uploading it for the world to see.

If you haven't got a webserver running check out WAMPP or XAMPP for testing you files locally, this will save you from constantly uploading to your hosting server, and you can test it without being online too ;-p

As Rocknbil points out, the path as you specify doesn't exist on the server, you need to have everything in one directory, and then use sub directories for structure ie:

root_folder/imgs/(your website pics in here)
root_folder/files/(necessary files in here)
root_folder/style_css/(stylesheets in here)

Hopefully this will point you in the right direction. Another point that Rocknbil makes I stress you need to follow - the use of $_REQUEST, this is potentially can be harmful to your site, use $_POST, this is accessing the form data directly, and is less overhead, as there is less data being requested.

This is the cleansing that Rocknbil is referring to, check it out on php.net to see the benefits of using it when santising form submitted data

$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);

Good luck, and keep going ;-p



 8:35 am on May 11, 2010 (gmt 0)

guys i could cry at how happy youve made me - it works perfectly and i have changed the $_request 's to $_post 's



 10:07 am on May 11, 2010 (gmt 0)

Hi there laurasamps,

Now comes the part where you can adapt the code for other eventualities such as eradicating blank entries!

in the recieving php script you can check the $_POST values by using isset($_POST) to see if there is any value, and if not redirect the user back to the form! This is the pseudo code for that:-

if(isset($_POST['button']) && ($_POST['button'] == "Submit")){//check to see if the button has been clicked
if(isset($_POST['email']) && ($_POST['email'] == "")){//see if the field is empty
//redirect as field is blank
header("Location: your_form_page");
//assign the $_POST var and clean it
$email = strip_tags($_POST['email']);
//rest of your processing script


From that you should be able to play around and see what happens ;)

isset(); just means is the var inside it set/has any value, you can use empty(); to the same effect

I'm glad as you are getting there now, as I said the first script is the hardest, now all you can do is improve. You will look back in a few weeks and think, "I can change this for that, improve this by doing that".

Have fun with the rest of your coding,



 2:26 pm on May 21, 2010 (gmt 0)

hey guys, ive tried to use my php script on another page im doing, and its not working! its absolutly killing me, can anyone help?! the form goes to the php page, and the php page should redirect the user to a thankyou page, it does not redirect even though the thankyou page is online and working. i have the url right so thats not the problem, it must be something in my code but i cant see where?! also the php page does not send the info to my email.

any suggestions greatly appreciated!

$firstname = $_POST['firstname']
$surname = $_POST['surname']
$address = $_POST['address']
$postcode = $_POST['postcode']
$phone = $_POST['phone']
$dob = $_POST['dob']
$education = $_POST['education']
$employment = $_POST['employment']
$nationality = $_POST['nationality']
$right = $_POST['right']

mail( "x@l#*$!.com", "CV uploaded",
"CV uploaded:
first name $firstname
surname $surname
address $address
post code $postcode
telephone number $phone
d.o.b $dob
education $education
employment $employment
nationality $nationality
right $right" );
header( "Location: [#*$!.com...] );


 2:46 pm on May 21, 2010 (gmt 0)

now my index page with the form on is going to sendmail7.php instead of sendmail.php (7 was the old file name) even though my code on the form is as follows:
<form id="form1" name="form1" method="post" action="sendmail.php">


 4:30 pm on May 21, 2010 (gmt 0)

Could be a caching issue, try deleting your temporary internet files.


 4:37 pm on May 21, 2010 (gmt 0)

Hi there laurasamps,

Yes as Readie says, could be caching, alternatively, just do a ctrl+F5 (that's in FF, I think it's the same in IE), this forces a complete refresh - I.e. calls all files referred to in the source code from the server, and not from what's cached.

When you display the webpage in your browser, just check with the page source that the filename as you have specified is actually in the page source - page source never lies ;)

Also, from reading your previous post, your email will be 1 complete line.. You will need to format it correctly with the line breaks & stipulate mail headers to say as it's plain text. ie:-

$messageBody = "This is the first line of text\n\r";
$messageBody .= "second line".$name."\n\r";//this is with a var included in the string, and the .= means to add to the var

From that piece there you should be able to format your email nicely!

$mailheaders = "MIME-version: 1.0\r\n";
$mailheaders .= "content-type: text/plain; charset=UTF-8\r\n";
$mailheaders .= "From: EMail Address<you@yoursite.co.uk>\r\n";
$subject = "CV uploaded";

then in the mail function just do this:-

mail($yourEmail, $subject, $messageBody, $mailheaders);

WRT the $_POST vars too, as you are using them raw, this script is open to abuse - do this to each $_POST:-

$thename = strip_tags($_POST['element_names_in_here']);

Then repeat this for all of the $_POST's as you have listed in your post.

Hope that helps,



 6:01 pm on May 21, 2010 (gmt 0)

^ ^ ^ There's that, it will be much easier to see what's going on following those ideas . . . and . . . .

it does not redirect even though the thankyou page is online and working.....also the php page does not send the info to my email.

What do you get, a white page? This would mean PHP errors are off by default, which is a good thing . . . . Do you have access to server error logs? Do you know if the script is actually working?

I don't know that you can even do this:

$firstname = $_POST['firstname']

without closing semicolons on each statement.

$firstname = $_POST['firstname'];

Maybe you can, it's done that way in Javascript all the time, just sayin', something to look at . . .

header( "Location: [#*$!.com...] );

The location header doesn't need brackets [ or ], and needs a closing " :


(I used https so the board software here doesn't munge up the code, you wouldn't use https . . . .)


 6:12 pm on May 21, 2010 (gmt 0)


header( "Location: [#*$!.com...] );

I think as that is more to do with the way this board software handles anything with a http/www attached, because when you mouseover, the tooltip has the closing quote...

Gah! Can't believe I missed that! Should be this :-

$firstname = strip_tags($_POST['firstname']);
$surname = strip_tags($_POST['surname']);
$address = strip_tags($_POST['address']);

That will definitely cause errors :\

You'll get there in the end ;)



 9:43 pm on May 21, 2010 (gmt 0)

So to clarify I refresh all my temporary files and do the string thing..? Thanks guys, I appreciate the help and I'll give it a go in the morning and let you know how I go!


 9:45 pm on May 21, 2010 (gmt 0)

And by string thing I meant strip_tags :-p


 8:48 am on May 25, 2010 (gmt 0)

hi thanks for your help everyone, it turns out it is probably a problem with my hosting rather than what im doing, because i hosted in on another website i have (for the purposes of what im doing it really doesnt matter) and everything worked fine. so ive sent off an email asking for the problem to be fixed and hopefully all will be well.

thanks for quick replies and help guys, its very much appreciated!


 8:51 am on Jun 9, 2010 (gmt 0)

apprently i need a 'from' line stating the email address that the info is being sent from (one attched to my website) does anyone know where i would put this in?

$email = $_POST['email'];
$message = $_POST['message'];
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$requirements = $_POST['requirements'];

mail( "a@#*$!.com", "Feedback Form Results",
"message from #*$!.com:
name $name
company $company
position $position
phone $phone
email $email
requirments $requirements" );
header( "Location: [#*$!.com...] );



 9:32 am on Jun 9, 2010 (gmt 0)

below is a php script which you can use to send form submissions. You won't need to edit post variables for each form. Try to name the form field in a way so you know what it is like:
<form action="process.php" method="post">
<input type="text" name="email" /> <input type="submit" />

The $to variable is where the email will be sent. You must add some spam checks in this script though.
The $from is where the email will be sent from.

$to= 'some@s.com'; //email where to send form
$from= 'localhost@s.com'; //email sent from
$thanks= 'thanks.html'; //redirect to this page after sending mail

die("Forbidden - You are not authorized to view this page. If you think this is an error, then please check the form.");

// Make sure the form was indeed POST'ed:
// (requires your html form to use: action="post")
die("Forbidden - You are not authorized to view this page. If you think this is an error, then please check the form.");

foreach($_POST as $k=>$kk){ $p[$k]= strip_tags($kk); }

//spam check goes here. You may find some useful checks codes on web search

//check if user has submitted all data you need

//build your message
foreach($p as $k=>$kk){ $m.="$k :\r\n $kk \r\n\r\n"; }
$h= "From: $from\r\n";
mail($to, "email from site", $m, $h);
header("location: $thanks");
echo '<script type="text/javascript"> window.location="'. $thanks. '"; </script>';


 10:04 am on Jun 9, 2010 (gmt 0)

Hi there laurasamps,

I point you to my previous post:-

$mailheaders = "MIME-version: 1.0\r\n";
$mailheaders .= "content-type: text/plain; charset=UTF-8\r\n";
$mailheaders .= "From: EMail Address<you@yoursite.co.uk>\r\n";
$subject = "CV uploaded";

You should be able to see what's what from there, as it the 'from' is included in the $mailheaders var ;)


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved