FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Php Code/Tutorial Needed.





RuthW
I am creating a site for one of my clients and i usually specialise in html code only. Therefore, i'm not a big php wizard!

I'm sure this is really simple but how can i create a 'contact me' page in php, I just want people to be able to type in their name, e-mail address and a message, then click send and their e-mail address, their name and their message get sent to an e-mail address.

If anyone good post a tutorial, or post the code, this would be particually helpful.

Thanks,

Ruth.
ncwdavid
HTML:
Code:

<form action="support.php" method="post">
Name:<br><input type="text" name="name"><br>
Subject:<br><input type="text" name="subject"><br>
Email Address:<br><input type="text" name="email"><br>
Message:<br>
<textarea name="message" rows="7" cols="50" ></textarea><br><br>
<input type="submit" value="Send">
</form>

Now the PHP:
Code:

$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
if( (!$name)||(!$subject)||(!$email)||(!$message) ){
   echo "Please fix the following errors:<br>";
if(!$name){
   echo "Name is a required field.<br>";
   }
if(!$subject){
   echo "Subject is a required field.<br>";
   }
if(!$email){
   echo "Email address is a required field.<br>";
   }
if(!$message){
   echo "Message is a required field.<br>";
   }
   echo "<form action='support.php' method='post'>
Name:<br><input type='text' name='name'><br>
Subject:<br><input type='text' name='subject'><br>
Email Address:<br><input type='text' name='email'><br>
Message:<br>
<textarea name='message' rows='7' cols='50' ></textarea><br><br>
<input type='submit' value='Send'>
</form>";
}
else{
mail("THE EMAIL YOU WANT IT SENT TO","Subject: $subject",$message,"FROM: $name<$email>");
if(mail){
   echo "Your support form has been successfully sent. Please allow up to 48 hours for a reply. Thank you.";
   }
else{
echo "There was an error while attempting to send your support form.";
}
}

If you have any questions just ask.
hexkid
ncwdavid wrote:
Code:
$name = $_POST['name'];
$email = $_POST['email'];
mail("THE EMAIL YOU WANT IT SENT TO","Subject: $subject",$message,"FROM: $name<$email>");

If you have any questions just ask.


It's possible to abuse this mail form. The only thing it takes is to enter BCCs in the "email"
Code:
hexkid@example.com>
Bcc: <victim1@example.com>, <victim2@example.com>, <victim3@example.com


And, yes! it's possible to enter "ENTERs" in a input field -- though not with a standard browser.

Just change the PHP to
Code:
$tmp_email = explode("\n", $_POST['email']);
$email = $tmp_email[0];
ncwdavid
I knew that and i ment to add this link in:
http://www.w3schools.com/php/php_secure_mail.asp
to secure everything but i forgot.
RuthW
Thanks alot, so what actaully will that do??

And what part do i change to get it to send to my e-mail address?
hexkid
I like to do that like this
Code:
<?php
### This is the script the form gets submitted to, with method="post"
define('MY_EMAIL', 'hexkid@example.com');

mail(MY_EMAIL, 'web mail', print_r($_POST, true));
exit('Thank you.');
?>
RuthW
I must seem really stupid, but when you post code can you actually say what it does and where i put it to make it work..... (like what happens when you clikc the submit button, etc...)

EDIT: I have tryed out this script, and it looks fine, but when you fill it in, and click 'send' it says 'you have chosen toi download 'support.php' Question

Any Ideas?
ncwdavid
re you trying it on your computer at home? like localhost?
hexkid
You have to install and configure PHP for your specific Operating System and web server.

[Edit]: I've seen people talking about XAMP, which apparently installs and configures itself automagically: http://www.google.com/search?q=xamp
RuthW
I'm trying to get a free host from 'frihost' but i havn't rewquested one yet. Gahh, this is so complicated.
Diablosblizz
@ ncwdavid:

Thank you for this script, I just used what you did for a reference and I expanded! Thank you.

One thing though, how would I include the senders IP address with the e-mail?
ncwdavid
Diablosblizz wrote:
@ ncwdavid:

Thank you for this script, I just used what you did for a reference and I expanded! Thank you.

One thing though, how would I include the senders IP address with the e-mail?


Im not to sure but there is definetly a way. incase you do not know this is how to get the ip address:
Code:

$ip_address = $REMOTE_ADDR;
//maybe you could just add it like this in the mail() function next to the $mesage
{$message} \n ip address = $ip_address

Tell me how you get on.
Diablosblizz
Alright Dave, I got something, heres what I get in my mailbox:

Quote:
Subject: sdf

Message: sdfsdfsdfsdf

From: sdfsdf

IP:


The IP is missing, everything else works but the IP. But I was able to fix the IP problem with this:

Code:
$ip = $_SERVER['REMOTE_ADDR']


And it works. Okay so who ever wants to be able to get the IP with extra stuff here is support.php:

Code:

<?php
//MAJORLY EDITED BY DIABLOSBLIZZ :D
$name = $_POST['nu'];
$sub = $_POST['sub'];
$address = $_POST['email'];
$msg = $_POST['msg'];
$ip = $_SERVER['REMOTE_ADDR'];
$mess = "
Subject: $sub \n
Message: $msg \n
From: $address \n
IP: $ip \n
";
//If you don't know what your doing, don't mess around!

if ( (!$name)||(!$sub)||(!$msg) ){
echo "Please fix the following:<BR>";

if(!$name){
echo "Please fill in your name<BR>";
}
if(!$sub){
echo "Please fill in the subject<BR>";
}
if(!$msg){
echo "Please fill in the message<BR><BR>";
}
echo "<form action='emailus.php' method='post'>
Name / Username: <input type='text' name='nu'><BR>
Subject: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' name='sub'><BR>
E-mail address: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' name='email'> (if you want a reply)<BR>
Message:<BR>
<textarea name='msg' rows='7' cols='50'></textarea>
<input type='submit' value='Send'>
</form>";
} else {

mail("diablosblizzard@gmail.com", "Subject: $sub", $mess, "From: $name <$address>");
if(mail) {
echo "Thank you! Please wait up to 48 hours for a reply.";
}  else  {
echo "The message could not be sent";
}
}
//Actully if you don't know what your doing don't edit anything, ask on frihost first!
?>


Its very very long, and I hope you can enjoy it!

EDIT I didn't use the same names in the forms as David did, so if you want to change the post to your input name then just change whats in the [' and '] and it will work.
RuthW
Basically, I would like to use that code on my site, which will be hosted by frihost. Will it send an e-mail straight to my inbox when i click 'send'? and how do i change the e-mail address to mine 'rwarlow@tinyonline.co.uk'?

When I use the code now (on a localhost) I fill in the form and click 'send' and a window comes up saying "you have chosen to download 'support.php'" (the name of the code file), why does this happen?

Could someone please inform me how this all works?

Thanks,

Ruth.
pckid2
At the moment, I'm part way through making a contact form generator for windows, I'll see if I can finish it now.
pckid2
TA DA! Contact Form Generator

I hope it works, I haven't had a chance to test it.
ncwdavid
RuthW wrote:
Basically, I would like to use that code on my site, which will be hosted by frihost. Will it send an e-mail straight to my inbox when i click 'send'? and how do i change the e-mail address to mine 'rwarlow@tinyonline.co.uk'?

When I use the code now (on a localhost) I fill in the form and click 'send' and a window comes up saying "you have chosen to download 'support.php'" (the name of the code file), why does this happen?

Could someone please inform me how this all works?

Thanks,

Ruth.


I seriously think you should go and learn the basics of php somewhere. Its very simple. Just google something like free php tutorial and that should get alot of results.
RuthW
Okay, thanks!

Thanks pckid2!

That worked but when i click on the 'send' button, nothing happens...??

this is the code:

Code:
<html>
<body>

<?php
if (isset($_REQUEST['email']))
  {
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  $email = $_REQUEST['email'] ;
  mail( "rwarlow@tinyonline.co.uk
, 'Subject: $subject',$message, 'From: $email' );
  echo 'Message Sent';
  }
else
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>

</body>
</html>


anything wrong?
pckid2
Oh! you have to call it mailform.php
Diablosblizz
Quote:
Basically, I would like to use that code on my site, which will be hosted by frihost. Will it send an e-mail straight to my inbox when i click 'send'? and how do i change the e-mail address to mine 'rwarlow@tinyonline.co.uk'?

When I use the code now (on a localhost) I fill in the form and click 'send' and a window comes up saying "you have chosen to download 'support.php'" (the name of the code file), why does this happen?

Could someone please inform me how this all works?

Thanks,

Ruth.


Okay, if you would still like to use the code I edited, then look below.

To change your email address find

Code:
mail("diablosblizzard@gmail.com", "Subject: $sub", $mess, "From: $name <$address>");


Change "diablosblizzard@gmail.com" to your email address you want the mail to go to.


On localhost, you need to have php enabled, for example use Xampp, its powerful, or use it on frihost when you get your account. Also, you will need to change

Code:
echo "<form action='emailus.php' method='post'>


change the emailus.php to the page where the script (mine) is going to be (remember to do it on both pages that you created).

------
I hope this helped, sorry if its confuzing but I'm sick, and I wanted to help.

Hope this works.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.