Forum Moderators: coopster

Message Too Old, No Replies

How can I add to this PHP script?

         

kslnor

4:25 pm on Nov 18, 2007 (gmt 0)

10+ Year Member



Because I don't know much about php (i'm trying...), can someone please tell me how I can add to this php script? Currently, the form has a section for name, subject and message and it is received to my email perfectly. Now, I have added another line to the form that I want to to be part of the message - in addition to the line that I already have. Any suggestions?

Here is the PHP:

<?php
// Contact subject
$subject ="$subject";
// Details
$message="$detail";

// Mail of sender
$mail_from="$customer_mail";
// From
$header="from: $name <$mail_from>";

// Enter your email address
$to ='contact@example.com';

$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email
// display message "Thank you, we have recived your information."
if($send_contact){
echo "<meta http-equiv='refresh' content='0;url=http://www.example.com/thanks.html'>";
}
else {
echo "ERROR";
}

?>

Here is the Form:

<form name="form1" method="post" action="contact.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td width="16%"><font size ="2">Name</f></td>
<td><font size ="2">:</f></td>
<td width="82%"><input name="name" type="text" id="name" size="38"></td>
</tr>
<tr>
<td><font size ="2">Email</f></td>
<td><font size ="2">:</f></td>
<td><input name="customer_mail" type="text" id="customer_mail" size="38"></td>
</tr>
<tr>
<td><font size ="2">Website</f></td>
<td><font size ="2">:</f></td>
<td><input name="subject" type="text" id="subject" size="38"></td>
</tr>

THIS IS THE NEW PART THAT I WANT INCLUDED IN THE MESSAGE
<tr>
<td><font size ="2">Select Service</f></td>
<td><font size ="2">:</f></td>
<td><br><input type="radio" name="service" value="Item1"> Item #1
<br>
<input type="radio" name="service" value="Item2"> Item #2
<br>
<input type="radio" name="service" value="Item3"> Item #3</td>
</tr>

THIS IS THE CURRENT MESSAGE DETAIL
<tr>
<td><font size ="2">Detail</f></td>
<td><font size ="2">:</f></td>
<td><br><textarea name="detail" cols="30" rows="6" id="detail"></textarea></td>
</tr>

<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><center><input type="submit" name="Submit" value="Send to example.com">
<br><br><input type="reset" name="Submit2" value="Reset"></center></td>
</tr>
</table>
</form>
</form>

[edited by: eelixduppy at 8:26 pm (utc) on Nov. 19, 2007]
[edit reason] example.com [/edit]

PHP_Chimp

7:10 pm on Nov 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have global's turned on? As it looks as though you are not using any $_POST variables, but seem to be using globals. There are a lot of security problems with those so it would be better to rewrite the script to use the $_POST array.


<?php
// Contact subject
$subject = $_POST['subject'];
// Details
$message = $_POST['detail'];
$message.= "\nService - $_POST['service']\n";
// Mail of sender
$mail_from = $_POST['customer_mail'];
// From
$header = "from: $name <$mail_from>";
// Enter your email address
$to ='contact@samplewebsite.com';
$send_contact = mail($to, $subject, $message, $header);
// Check, if message sent to your email
// display message "Thank you, we have recived your information."
if($send_contact){
// a posh redirecting script ;)
if (!headers_sent()) {
header ('Location: http://'.$_SERVER['HTTP_HOST'].'/thanks.html');
}
else {
echo "<script type=\"text/javascript\">
window.location.href=\"/thanks.html\";
</script>
<noscript>
<meta http-equiv='refresh' content='0;url=http://www.example.com/thanks.html'>";
</noscript>
}
}
else {
echo "ERROR";
}
?>

<edit>
put in a posh redirection script, as some antispam filters on browsers will stop both meta-refresh and window.location.href redirections, so it is better to do it at the server level. However to use the header function it will only work if no output has been sent, or if you output buffering turned on. So you need to check to see if headers have been sent.

[edited by: PHP_Chimp at 7:17 pm (utc) on Nov. 18, 2007]

[edited by: eelixduppy at 2:55 am (utc) on Nov. 19, 2007]
[edit reason] delinked [/edit]

kslnor

3:51 pm on Nov 19, 2007 (gmt 0)

10+ Year Member



Thank you.

When testing this revised script, I received this error message:

'Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /.../.../.../....php on line 6'

(I replaced what I thought may be secure info. with the ...)

Any clues on how to fix this?

Thanks.

PHP_Chimp

7:03 pm on Nov 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Im assuming $message.= "\nService - $_POST['service']\n"; is line 6.
Try -
$message.= "\nService - ".$_POST['service']."\n";