Topic: Need help in "Contact Form" code

Hello, I am making a contact form that takes input from the contact form and sends it to you in an email message.

The HTMl is:

<form method="post" action="contactform.php">
                   
    Email: <input name="email" type="text" size="30" maxlength="40" /><br />
    Message: <br />
      <textarea name="message" rows="15" cols="40"></textarea><br />
      <input type="submit" />
      <input type="reset" name="reset" value="Reset" />

</form>

The PHP of "contactform" is:

<?php

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

mail("email@yahoo.com", "Message from the Website",
    $message, "From: $email" );
header( "Location: http://www.website.com/thanks.html" );

?>

I need to add more fields to the form such as Name, Address and Telephone but being this my first experience with PHP I am unable to do that. Upon adding additional fields the form seems to work fine as the form moves to thanks.html page but there is no email sent.

Please help me add multiple fields to the form above that actually sends the data in the email. Thanks!

Vote up Vote down

Re: Need help in "Contact Form" code

Here's a quick example with one additional form element added:

<form method="post" action="contactform.php">
                   
    Email: <input name="email" type="text" size="30" maxlength="40" /><br />
    Name: <input name="name" type="text" size="30" maxlength="40" /><br />
    Message: <br />
      <textarea name="message" rows="15" cols="40"></textarea><br />
      <input type="submit" />
      <input type="reset" name="reset" value="Reset" />

</form>

PHP:

<?php

$email = $_REQUEST['email'];
$message = "Name: " . $_REQUEST['name'] . "/n" . "Message: " .  $_REQUEST['message'];

mail("email@yahoo.com", "Message from the Website",
    $message, "From: $email" );
header( "Location: http://www.website.com/thanks.html" );

?>

The "name" element of the <input> in the form needs to match the $_REQUEST[] in the PHP. So, since I added name="name" to the input, I have to use $_REQUEST['name'].

This line:

$message = "Name: " . $_REQUEST['name'] . "/n" . "Message: " .  $_REQUEST['message'];

creates a string that should look like this:

Name: [name]
Message: [message]

The "\n" portion creates a new line.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Need help in "Contact Form" code

Since you are using $_POST in your form, you might be better served to use it in your PHP script to capture the user input instead of  $_REQUEST.

Ben example looks good to me, but I'm curious about one item in the mail().

[mail("email@yahoo.com", "Message from the Website", $message, "From: $email" );]  Does ["From: $email"] need to be
["From:" . $email]?

Vote up Vote down

Re: Need help in "Contact Form" code

@dms

Since you are using $_POST in your form, you might be better served to use it in your PHP script to capture the user input instead of  $_REQUEST.

Probably true. Using $_POST is a bit more secure than $_REQUEST, though both will work.

"Note: The variables in $_REQUEST are provided to the script via the GET, POST, and COOKIE input mechanisms and therefore could be modified by the remote user and cannot be trusted. The presence and order of variables listed in this array is defined according to the PHP variables_order  configuration directive. "
http://ca.php.net/manual/en/reserved.va … equest.php

[mail("email@yahoo.com", "Message from the Website", $message, "From: $email" );]  Does ["From: $email"] need to be
["From:" . $email]?

Either will work. The first example will work as long as double quotes are used.  This, however, won't work (using single quotes):

mail("email@yahoo.com", "Message from the Website", $message, 'From: $email');

If double quotes are used, PHP understands to check inside the string for variables. If single quotes, it assumes that it is a simple string that includes no variables.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Need help in "Contact Form" code

Just wanted to add that using the single quote method actually parses the form "somewhat" quicker.
Not a big deal for a small script like this, but it makes a difference an a larger script.
Might as well develop some good programming skills now as a matter of habit.
Then you be a better scripter in the long run.

My signature goes here --> X

Vote up Vote down

Re: Need help in "Contact Form" code

jlhaslip wrote:

Just wanted to add that using the single quote method actually parses the form "somewhat" quicker.
Not a big deal for a small script like this, but it makes a difference an a larger script.
Might as well develop some good programming skills now as a matter of habit.
Then you be a better scripter in the long run.

Agreed. I just didn't want to throw too much at a beginner all at once. hmm

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Need help in "Contact Form" code

Hello & Thanks to falkencreative, dms, jlhaslip for relpying to my query. I did try this code:

$message = "Name: " . $_REQUEST['name'] . "/n" . "Message: " .  $_REQUEST['message'];   

The email is sent with the info but there is one problem. Instead of another line, the message is displayed in the same line as the name is with \n. Something like this:

Name: TWyPGn/nMessage:      hello         

Would you please help me solve this problem? Thanks!

Vote up Vote down

Re: Need help in "Contact Form" code

Oops. I noticed this issue earlier, but I thought I edited my post... Instead of "/n" try "\n". That should fix it.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Need help in "Contact Form" code

Thanks falkencreative! By changing "/n" to "\n" has created another problem: The email is not sent at all sad I don't know why because I just changed this, nothing else. I am so sorry to bother you with such little problems but being a novice in PHP I am unable to fix such problems. I'll appreciate if you could please help me resolve the issue. Thanks!

Vote up Vote down

Re: Need help in "Contact Form" code

I'd suggest waiting a little longer... it may just be that the server is just taking a while to send it. I'm using "\n" in a quick test script, and it works fine.

If you are still having issues, and you've waited about half an hour, post the PHP code you are using -- perhaps there is something else up with it.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Need help in "Contact Form" code

Hi, thanks for your help so far!

The code I am using is as follows:

<?php
 
 
  $email = $_REQUEST['email'] ;
   

    $to = "email@yahoo.com";
    $subject = "Message from website";

    $message = "Name: " . $_REQUEST['name'] . "\n" .
                         "Address: " . $_REQUEST['address'] . "\n" .
                          "Telephone: " . $_REQUEST['telephone'] . "\n" .
                               "Message: " .  $_REQUEST['message'];   

  mail( $to, $subject, $message,"From: $email" );
  header( "Location: http://www.website.com/thanks.html" );
?>

I just need to have Name, Address, Telephone and Message displayed in new lines in the body of the email message. I don't know if there is anything else that can take care of that. I would appreciate if you could please help me in making this form/code work fine. Thanks!

Vote up Vote down

Re: Need help in "Contact Form" code

That code works fine for me... I receive the email about a minute after completing the form, and it has the correct new lines.

Perhaps a silly question, but you've changed the $email value from "email@yahoo.com" to your own email address, correct?

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Need help in "Contact Form" code

Yes, I have my own email address in the   $to field:). I receive the email when I use "/n", of course it displays all entries in one line with /n between them. When I change "/n" to "\n" I do not get any email. Don't know what's the problem. May be the code doesn't want to work today:/

Vote up Vote down

Re: Need help in "Contact Form" code

Ummm... a bit of a guess on my part... you could try using "\r\n" instead, in case the server is having issues with the new line character.

Otherwise, you could use create an HTML mail (rather than plain text, as you are doing now) by looking at the guide here:
http://www.w3schools.com/PHP/func_mail_mail.asp (example 3)

That would allow you to use a line break (<br/>) rather than a new line character.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Need help in "Contact Form" code

Ahhhh.... It finally works fine! Thank you so much falkencreative for your help and time. I really appreciate it. Thanks!

Vote up Vote down

Re: Need help in "Contact Form" code

No problem... glad you got it working. smile

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down