Simple form - can anyone help please

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
chiltonwilliams
Forum Newbie
Posts: 2
Joined: Fri Feb 06, 2009 7:35 am

Simple form - can anyone help please

Post by chiltonwilliams »

Hi, All I want to do is create a simple form but its turning into a total nightmare. Even a simple comments box would do but I just can get it to work! :banghead:

i create a form in Dreamweaver and get this
[b]<form id="form1" name="form1" method="post" action="sendmail.php">
<label for="textfield">Comments</label>
<p>
<input type="text" name="textfield" id="textfield" />
</p>
<p>
<label for="Submit"></label>
<input type="submit" name="Submit" value="Submit" id="Submit" />
</p>
</form>

then create a .php called sendmail.php and thats where the headache starts. I have been given a help file from streamline.net with code such as

ini_set("sendmail_from", " user@yourdomain.com ");
mail($email_to, $email_subject, $email_message, $headers, '-fuser@yourdomain.com');
?>

after changing the domains it doesnt work. If anyone would review the full helpfile and let me know where im going wrong or point me to a tutorial (that i havent already read!) that is will be great. im so frustrated!

helpfile
To prevent spam being sent through our webservers, there are certain conditions that must be met before our SMTP servers will send the email.
1) Email must be sent to, or from, an email address hosted by Streamline.net. An email address hosted by Streamline.net is not the same as a domain name hosted by Streamline.net. If your domain's MX record points to another email provider, it will not count as being hosted by Streamline.net.

2) To stop misuse of your form by third parties the sendmail_from variable should be set to your Streamline.net hosted email address. While access to the php.ini file is restricted on our shared environment, you can set this variable using the ini_set() command, shown below.

3) A fifth parameter -f should be added to the sendmail function. This will set the name of the from email address.

In its basic form, a simple sendmail script will look like this:

ini_set("sendmail_from", " user@yourdomain.com ");
mail($email_to, $email_subject, $email_message, $headers, '-fuser@yourdomain.com');
?>

Provided that you set the sendmail variable, before attempting to send the email. Specify the from address as a fifth parameter in the sendmail function, and the email is either to, or from, a Streamline.net hosted email address you should have no problems.


Example
This example will take the information from a feedback form, send it to you in an email message, then forward the customer to a "thank you for your comments" page. In this example we will use bobsdomain.co.uk as the domain name. There is a mailbox hosted with Streamline.net called bob@bobsdomain.co.uk.
A simple feedback form

First of all we need to create a feedback form that will receive the data. We will call this form feedback.html. In its most basic form, it can look like this:


Email address:

Name:

Message:

<br />




example feedback form

Not the prettiest form, but it can be tidied up, and validation can be added at a later date. This form has three fields (email address, name and message) that users can fill in. Once the user click the Submit button, it will collect the information contained within the fields, tag the information as "email, name and message", then send the information to sendmail.php.

The sendmail script

Now we have a form that sends information to a script, we need to create a script to send the email. In this example, we will name the script sendmail.php as this is the address the our form is submitting the data to. In order to send an email, we need certain information (variables), so lets set them first.

ini_set('sendmail_from', $email_from);
$email_to = "bob@bobsdomain.co.uk";
$name =$_POST['name'];
$email_from =$_POST['email'];
$email_subject = "Feedback from website";
$comments = $_POST['message'];
$headers =
"From: $email_from .\n";
"Reply-To: $email_from .\n";

Now lets build the message of the email with the users name and comments.

$message= "Name: ". $name . "\nComments: " . $comments;

Finally, let's send the email. If the email is sent we will go to a thank you page, if there is an error we will display a brief message.

$sent = mail($email_to, $email_subject, $message, $headers, '-f .$email_from');
if ($sent)
{
header( "Location: http://www.bobsdomain.co.uk/thankyou.html" );
} else {
echo 'There has been an error sending your comments. Please try later.';
}
?>

aaaarrrgggg :banghead:
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Simple form - can anyone help please

Post by susrisha »

can you post the full code for the form that you are using??

I dont see any fields that you are getting the data from in the form that you have posted.

Code: Select all

 
 $comments = $_POST['textfield']; 
//these fields are not present in the form that was designed.. hence the trouble
$name =$_POST['name'];
$email_from =$_POST['email'];
$email_subject = "Feedback from website";
$comments = $_POST['message'];
//
 
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Simple form - can anyone help please

Post by ben.artiss »

Hi,

Here's a simple version of what you need. You may also want to take a look at validating what the user enters.

HTML file:

Code: Select all

<form method="post" action="sendmail.php">
    <label for="name">Name:</label> <input type="text" id="name" name="name" /> <br />
    <label for="email">Email:</label> <input type="text" id="email" name="email" /> <br />
    <textarea id="message" name="message"></textarea> <br />
    <input type="submit" id="sendmsg" name="sendmsg" value="Send Message" />
</form>
sendmail.php

Code: Select all

<?php
if (isset($_POST['sendmsg'])) {
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $message = trim($_POST['message']);
 
    ini_set('sendmail_from', $email);
 
    $to = "Bob <bob@bobsdomain.co.uk>";
    $headers = "From: $email\r\nReply-To: $email\r\n";
    $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; 
    $subject = "Feedback from website";
 
    $message = "Name: ".$name."\nComments: ".$message;
    $mail_sent = @mail($to, $subject, $message, $headers);
 
    if ($mail_sent) {
        header("Location: thankyou.html"); exit;
    } else {
        header("Location: failed.html"); exit;
    }
} else {
    header("Location: index.html"); exit;
}
?>
As I said this doesn't check what the user puts in, but it will send the email if everything's entered correctly. I personally find it easier to post the form to the same file (using action="" in the <form> tag) and doing all validation and sending at the top of the page before the opening <html> tag - but it's up to you! Hope that points you in the right direction though.
chiltonwilliams
Forum Newbie
Posts: 2
Joined: Fri Feb 06, 2009 7:35 am

Re: Simple form - can anyone help please

Post by chiltonwilliams »

Thanks so much, i'll give it a go. I have now got coffeecup form builder which makes things easier but I can get it to run through my isp but it does work to my gmail account so thats a start.
cheers
Post Reply