email submit button

marshian

New Member
Messages
526
Reaction score
9
Points
0
There's no way to do this in one step, what you'll have to do is send the user input to a php page that then sends the mail.
And you're missing form tags...

replace
HTML:
  <table width="575" border="0" cellpadding="3" cellspacing="2">
    <tr>
      <td width="250"><div align="right"><span class="style6">Name:</span></div></td>
      <td width="307"><label>
        <input name="Name" type="text" id="Name" size="35" maxlength="80">
      </label></td>
    </tr>
    <tr>
      <td><div align="right"><span class="style6">E-mail:</span></div></td>
      <td><input name="e-mail" type="text" id="e-mail" size="35" maxlength="80"></td>
    </tr>

    <tr>
      <td colspan="2"><hr></td>
    </tr>
    <tr>
      <td><div align="right"><span class="style6">What was your favorite part ? </span></div></td>
      <td><label>
        <select name="favorite" id="favorite">
          <option value="pictures">Pictures</option>
          <option value="Bio">Bio</option>
          <option value="school">School</option>
          <option value="trips">Trips</option>
        </select>
      </label></td>
    </tr>
    <tr>
      <td><div align="right"><span class="style6">Any comments? </span> </div></td>
      <td><label>
        <textarea name="comments" cols="35" rows="4" id="comments"></textarea>
      </label></td>
    </tr>
    <tr>
      <td colspan="2"><hr></td>
    </tr>
    <tr>
      <td><input name="email" type="hidden" id="email" value="kate@mahler.x10hosting.com"></td>
      <td><label>
        <input type="submit" name="Submit" value="Submit">
        </label>
        &nbsp;
        <label></label></td>
    </tr>
  </table>

with the following:
HTML:
<form action="guestbookmail.php" method="post">
  <table width="575" border="0" cellpadding="3" cellspacing="2">
    <tr>
      <td width="250"><div align="right"><span class="style6">Name:</span></div></td>
      <td width="307"><label>
        <input name="Name" type="text" id="Name" size="35" maxlength="80">
      </label></td>
    </tr>
    <tr>
      <td><div align="right"><span class="style6">E-mail:</span></div></td>
      <td><input name="e-mail" type="text" id="e-mail" size="35" maxlength="80"></td>
    </tr>

    <tr>
      <td colspan="2"><hr></td>
    </tr>
    <tr>
      <td><div align="right"><span class="style6">What was your favorite part ? </span></div></td>
      <td><label>
        <select name="favorite" id="favorite">
          <option value="pictures">Pictures</option>
          <option value="Bio">Bio</option>
          <option value="school">School</option>
          <option value="trips">Trips</option>
        </select>
      </label></td>
    </tr>
    <tr>
      <td><div align="right"><span class="style6">Any comments? </span> </div></td>
      <td><label>
        <textarea name="comments" cols="35" rows="4" id="comments"></textarea>
      </label></td>
    </tr>
    <tr>
      <td colspan="2"><hr></td>
    </tr>
    <tr>
      <td><input name="email" type="hidden" id="email" value="kate@mahler.x10hosting.com"></td>
      <td><label>
        <input type="submit" name="Submit" value="Submit">
        </label>
        &nbsp;
        <label></label></td>
    </tr>
  </table>
</form>

And add a new file "guestbookmail.php" with the following content:
PHP:
<?php
$name = $_POST["Name"];
$email = $_POST["e-mail"];
$favourite = $_POST["favourite"];
$comments = $_POST["comments"];
$message = "<html><body>";
$message .= "<i>$name</i> has reacted to your site.<br>";
$message .= "Favourite part: $favourite<br>";
$message .= "Comments: <br>$comments";
$message .= "</body></html>";
mail("mahlerwd <kate@mahler.x10hosting.com>","Comment in your guestbook",$message,"From: guestbook <guestbook@mahler.x10hosting.com>");
?>

If something doesn't work, just ask us for help again.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
I just see two problems with that. One, the receiver address in the html source might as well be deleted since it's hardcoded in the script. Leaving it there would only prove useful to spam bots. And two, since you're sending the message in html, you should provide the Content-type header.

Also, I think the sender address should be put in some part of the message(or else why ask for it?). It could be used for the 'From' header, however it needs to be sanitized in that case. So I'd change it a little bit to this:

HTML:
<form action="guestbookmail.php" method="post">
  <table width="575" border="0" cellpadding="3" cellspacing="2">
    <tr>
      <td width="250"><div align="right"><span class="style6">Name:</span></div></td>
      <td width="307"><label>
        <input name="Name" type="text" id="Name" size="35" maxlength="80">
      </label></td>
    </tr>
    <tr>
      <td><div align="right"><span class="style6">E-mail:</span></div></td>
      <td><input name="e-mail" type="text" id="e-mail" size="35" maxlength="80"></td>
    </tr>

    <tr>
      <td colspan="2"><hr></td>
    </tr>
    <tr>
      <td><div align="right"><span class="style6">What was your favorite part ? </span></div></td>
      <td><label>
        <select name="favorite" id="favorite">
          <option value="pictures">Pictures</option>
          <option value="Bio">Bio</option>
          <option value="school">School</option>
          <option value="trips">Trips</option>
        </select>
      </label></td>
    </tr>
    <tr>
      <td><div align="right"><span class="style6">Any comments? </span> </div></td>
      <td><label>
        <textarea name="comments" cols="35" rows="4" id="comments"></textarea>
      </label></td>
    </tr>
    <tr>
      <td colspan="2"><hr></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><label>
        <input type="submit" name="Submit" value="Submit">
        </label>
        &nbsp;
        <label></label></td>
    </tr>
  </table>
</form>

PHP:
<?php
$name = $_POST["Name"];
$email = $_POST["e-mail"];
$favourite = $_POST["favourite"];
$comments = $_POST["comments"];
$message = "<html><body>";
$message .= "<i>$name</i> has reacted to your site.<br>";
$message .= "Email: $email<br>";
$message .= "Favourite part: $favourite<br>";
$message .= "Comments: <br>$comments";
$message .= "</body></html>";
$headers = "From: guestbook <guestbook@mahler.x10hosting.com>\r\n";
$headers .= 'Content-type: text/html; charset=utf-8';
mail("mahlerwd <kate@mahler.x10hosting.com>","Comment in your guestbook",$message,$headers);
?>
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
Forgetting stuff is human... Why do I keep forgetting stuff?
 

VPmase

New Member
Messages
914
Reaction score
1
Points
0
PLEASE DON'T PUT THAT ON YOUR SITE. Unless you want an SQL dump done through that code you need to add a simple function to it.
Change
PHP:
$name = $_POST["Name"];
$email = $_POST["e-mail"];
$favourite = $_POST["favourite"];
$comments = $_POST["comments"];
to
PHP:
$name = strip_tags($_POST["Name"]);
$email = strip_tags($_POST["e-mail"]);
$favourite = strip_tags($_POST["favourite"]);
$comments = strip_tags($_POST["comments"]);
 

kbjradmin

New Member
Messages
512
Reaction score
2
Points
0
just do this:

PHP:
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$favorite = $_REQUEST['favorite'] ;
$comment = $_REQUEST['comment'] ;
$message = "$name
$email
$favorite
$comment";
$to = "email@host.com";
$subject = "subject"
$sent = mail($to, $subject, $message);
if($sent)
{
echo "CODE IF SEND IS SUCCESSFUL";
}
else
{
echo "CODE IF SEND IS UNSUCCESSFUL";
}

also, just a note, i seriously suggest against using a mailto link on your page like what you have at the bottom unless you really like reading spam.
 

VPmase

New Member
Messages
914
Reaction score
1
Points
0
Don't use kbjardmin's script. The message will all be one line and he is missing a ;
marshian's script is good if you add the strip_tags() function to the inputs.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Mind explaining how an sql dump could be achieved through that? I could see that it is possible for someone to enter somewhat harmful html, but an sql dump seems unfathomable. In fact, I think she doesn't even have a db set up. Furthermore, she's just making the page for a class which probably doesn't even touch on php, so I don't think it's necessary to include validation really. If we were to start doing that, there's a lot more we should do than strip_tags(). But there's no harm in including that function I suppose ;-)
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
So if no more faults are discovered, the code is now:
HTML:
<form action="guestbookmail.php" method="post">
  <table width="575" border="0" cellpadding="3" cellspacing="2">
    <tr>
      <td width="250"><div align="right"><span class="style6">Name:</span></div></td>
      <td width="307"><label>
        <input name="Name" type="text" id="Name" size="35" maxlength="80">
      </label></td>
    </tr>
    <tr>
      <td><div align="right"><span class="style6">E-mail:</span></div></td>
      <td><input name="e-mail" type="text" id="e-mail" size="35" maxlength="80"></td>
    </tr>

    <tr>
      <td colspan="2"><hr></td>
    </tr>
    <tr>
      <td><div align="right"><span class="style6">What was your favorite part ? </span></div></td>
      <td><label>
        <select name="favorite" id="favorite">
          <option value="pictures">Pictures</option>
          <option value="Bio">Bio</option>
          <option value="school">School</option>
          <option value="trips">Trips</option>
        </select>
      </label></td>
    </tr>
    <tr>
      <td><div align="right"><span class="style6">Any comments? </span> </div></td>
      <td><label>
        <textarea name="comments" cols="35" rows="4" id="comments"></textarea>
      </label></td>
    </tr>
    <tr>
      <td colspan="2"><hr></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><label>
        <input type="submit" name="Submit" value="Submit">
        </label>
        &nbsp;
        <label></label></td>
    </tr>
  </table>
</form>

and
PHP:
<?php
$name = strip_tags($_POST["Name"]);
$email = strip_tags($_POST["e-mail"]);
$favourite = strip_tags($_POST["favourite"]);
$comments = strip_tags($_POST["comments"]);
$message = "<html><body>";
$message .= "<i>$name</i> has reacted to your site.<br>";
$message .= "Email: $email<br>";
$message .= "Favourite part: $favourite<br>";
$message .= "Comments: <br>$comments";
$message .= "</body></html>";
$headers = "From: guestbook <guestbook@mahler.x10hosting.com>\r\n";
$headers .= 'Content-type: text/html; charset=utf-8';
mail("mahlerwd <kate@mahler.x10hosting.com>","Comment in your guestbook",$message,$headers);
?>
 

mahlerwd

New Member
Messages
13
Reaction score
0
Points
0
thank you guys this is really great.
but I'm completely new on all of this so i dont really get the php thing. i made a php page and i put that code in, and when i open it its blank. I'm sure I'm supposed to put some other stuff in, but i really just dont know what it is....i put the html code for the rest of my page on, but i dont even know if I'm supposed to put html on a php page...

more help please? sorry for the inconvenience
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
I'll try to explain this...
In the file "guestbookmail.php", there must be the code we've already mentionned. (You could add html below the php.)
If you don't add any html below the php, the file is empty as far as the user knows. The php code gets executed, and in this case it doesn't output anything, it just sends a mail. If you add html below the php code, the php gets executed, sends a mail while not outputting anything, and then the html gets transferred to the client. (Anything between <?php and ?> disappears in this file.)

I don't think you get the point of what happens to your form data either, so I'll explain that too.
When you click the submit button, the client requests the page "guestbookmail.php" and adds POST information. (The stuff you entered in the form.) The file "guestbookmail.php" is to be send out by the server now, but the server won't send it, as it knows it has to be executed first.
The server executes the file and sends the result to the cient. When the server executes the file, everything between <?php and ?> dissappears and gets replaced by the output of that code. In this case the code doesn't output anything.


Examples:

This code:
PHP:
<?php
$var = "Text";
?>
There's no text.
outputs "There's no text.", since everything between <?php and ?> dissappears and gets replaced by the output, which is empty.

PHP:
<?php
echo "Text";
?>
There's text.
outputs "TextThere's text.", since this time, the output is "Text".


I hope this made it any more clear...

Marshian
 

bonzo meier

Member
Messages
47
Reaction score
0
Points
6
hi,

the php script is not designed to generate any output but the mail with the form data. so if you want visitors, who filled in the form, to be directed back to the form page after submitting, you can add the line

header('Location: http://www.myformsite.com/');

at the end of the php.

peace,
bonzo
 

devongovett

New Member
Messages
33
Reaction score
0
Points
0
As far as I can tell, you are still missing
Code:
<form  action="guestbookmail.php" method="post">
although you can change guestbookmail.php to whatever you named your php file. If you go to the php file directly in a browser, you will no receive anything back - you have to access it by clicking on the submit button (which will only work after you add the form tags). You should also change the php code so that it shows something in the browser after users submit the form.

Code:
<?php 
$name = strip_tags($_POST["Name"]); 
$email = strip_tags($_POST["e-mail"]); 
$favourite = strip_tags($_POST["favourite"]); 
$comments = strip_tags($_POST["comments"]); 
$message = "<html><body>"; 
$message .= "<i>$name</i> has reacted to your site.<br>"; 
$message .= "Email: $email<br>"; 
$message .= "Favourite part: $favourite<br>"; 
$message .= "Comments: <br>$comments"; 
$message .= "</body></html>"; 
$headers = "From: guestbook <guestbook@mahler.x10hosting.com>\r\n"; 
$headers .= 'Content-type: text/html; charset=utf-8'; 
mail("mahlerwd <kate@mahler.x10hosting.com>","Comment in your guestbook",$message,$headers);
[B]echo "Your form has been submitted!";[/B] //or...
[B]header( "Location: submitted.html" );[/B] //loads an html page
?>
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
it could be easyer too:

PHP:
<?php 
$name = strip_tags($_POST["Name"]); 
$email = strip_tags($_POST["e-mail"]); 
$favourite = strip_tags($_POST["favourite"]); 
$comments = strip_tags($_POST["comments"]); 
$message = "<html><body>"; 
$message .= "<i>$name</i> has reacted to your site.<br>"; 
$message .= "Email: $email<br>"; 
$message .= "Favourite part: $favourite<br>"; 
$message .= "Comments: <br>$comments"; 
$message .= "</body></html>"; 
$headers = "From: guestbook <guestbook@mahler.x10hosting.com>\r\n"; 
$headers .= 'Content-type: text/html; charset=utf-8'; 
mail("mahlerwd <kate@mahler.x10hosting.com>","Comment in your guestbook",$message,$headers);
?>
<!-- Add html here. //-->
 

mahlerwd

New Member
Messages
13
Reaction score
0
Points
0
it works flawlessly.
thank y'all for your time. i really appreciate it

just one more quick question.
is there any way i can make it so when someone presses submit a auto response email is automatically sent to the email account they put in the "email" section?
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
Of course, I think this is the right code:

PHP:
<?php  
$name = strip_tags($_POST["Name"]);  
$email = strip_tags($_POST["e-mail"]);  
$favourite = strip_tags($_POST["favourite"]);  
$comments = strip_tags($_POST["comments"]);  
$message = "<html><body>";  
$message .= "<i>$name</i> has reacted to your site.<br>";  
$message .= "Email: $email<br>";  
$message .= "Favourite part: $favourite<br>";  
$message .= "Comments: <br>$comments";  
$message .= "</body></html>";  
$headers = "From: guestbook <guestbook@mahler.x10hosting.com>\r\n";  
$headers .= 'Content-type: text/html; charset=utf-8';  
mail("mahlerwd <kate@mahler.x10hosting.com>","Comment in your guestbook",$message,$headers); 

if(eregi("^[a-z\-_.0-9]+@[a-z\-_0-9]+(.[a-z\-_0-9]+)*.[a-z]+$", $email)) {
	$thankyou = "<!-- Html for thank-you goes here. //-->";
	@mail("$name <$email>", "Thank you", $message, $headers);
}
?> 
<!-- Add html here. //-->

EDIT:
this checks wether the inputted mail is real, as far as it depends on syntax and sends a mail if it is a real mail adress, without displaying errors (if any)
 
Last edited:

mahlerwd

New Member
Messages
13
Reaction score
0
Points
0
i changed the code and then i went to test it to see if it worked and i got an error message

"Parse error: syntax error, unexpected '@' in /home/mahlerwd/public_html/katemahler/guestbookmail.php on line 12"
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
That's strange, I don't have any problems when I run that code :s
 
Top