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!
I bought a book recently called 'PHP 5 Fast and Easy web development'
on of the chapters shows how to create a form to send info as email,
I am trying it out with their code and I am getting an error. It could be b/c of a typo and I was hoping someone on this list could locate it if they have a good PHP editor.
<?
$form_block = "
<FORM METHOD=\"POST\" ACTION=\"$PHP_SELF\">
<p><strong>Your Name:</strong>< /br>
<INPUT type=\"text"\" NAME=\"senders_name\" SIZE=30></p>
<p><strong><p><Your E-mail Address:</strong>< /br>
<INPUT type=\"text\" NAME="senders_email\" SIZE=30></p>
<p><strong>Message:</strong>< /br>
<TEXTAREA NAME=\"message\" COLS=30 ROWS=5 WRAP=virtual></TEXTAREA></p>
<INPUT type=\"hidden\" name=\"op\" value=\"ds\">
<p><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Send this Form\"></p>
</FORM>";
if ($_POST[op] !="ds") {
// they need to see the form
echo "$form_block";
} else if ($_POST[op] =="ds") {
//check value of $_POST[sender name]
if ($_POST[sender_name] =="") {
$name_err = "<font color=red>Please enter your name!</font>< /br>";
$send ="no";
}
//check value of $_POST[sender_email]
if ($POST[sender_email] =="") {
$email_err ="<font color=red>Please enter your email address!</font>< /br>";
$send= "no";
}
//check value of $_POST[message]
if ($POST[message] =="") {
$message_err = "<font color=red>Please enter a message!</font>< /br>";
$send ="no";
}
if ($send !="no") {
//it's o.k to send, so build the mail
$msg ="E-MAIL SENT FROM WWW SITE\n";
$msg .="Senders Name: $POST[senders_name]\n";
$msg .="Senders E-MAIL: $POST[senders_email]\n";
$msg .="Senders Name: $POST[message]\n\n";
$to ="webguync@gmail.com";
$subject = "There has been a disturbance in the Force";
$mailheaders .="Reply-To: $_POST[sender_email]\n";
//send the mail
mail ($to, $subject, $msg, $mailheaders);
//display confirmation to user
echo "<p>mail has been sent!</p>";
} else if ($send =="no") {
//print error messages
echo "$name_err";
echo "$email_err";
echo "$message_err";
echo "$form_block";
}
}
?>
the error I get is :
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /hsphere/local/home/bruceg/inspired-evolution.com/Contact_Form_test.php on line 38
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /hsphere/local/home/bruceg/inspired-evolution.com/Contact_Form_test.php on line 40
<?
$form_block = "
<FORM METHOD=\"POST\" ACTION=\"$PHP_SELF\">
<p><strong>Your Name:</strong>< /br>
<INPUT type=\"text\" NAME=\"senders_name\" SIZE=30></p>
<p><strong><p><Your E-mail Address:</strong>< /br>
<INPUT type=\"text\" NAME=\"senders_email\" SIZE=30></p>
<p><strong>Message:</strong>< /br>
<TEXTAREA NAME=\"message\" COLS=30 ROWS=5 WRAP=virtual></TEXTAREA></p>
<INPUT type=\"hidden\" name=\"op\" value=\"ds\">
<p><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Send this Form\"></p>
</FORM>";
if ($_POST['op'] !="ds") {
// they need to see the form
echo "$form_block";
} else if ($_POST['op'] =="ds") {
//check value of $_POST['sender name']
if ($_POST['sender_name'] =="") {
$name_err = "<font color=red>Please enter your name!</font>< /br>";
$send ="no";
}
//check value of $_POST['sender_email']
if ($POST['sender_email'] =="") {
$email_err ="<font color=red>Please enter your email address!</font>< /br>";
$send= "no";
}
//check value of $_POST['message']
if ($POST['message'] =="") {
$message_err = "<font color=red>Please enter a message!</font>< /br>";
$send ="no";
}
if ($send !="no") {
//it's o.k to send, so build the mail
$msg ="E-MAIL SENT FROM WWW SITE\n";
$msg .="Senders Name: $POST['senders_name']\n";
$msg .="Senders E-MAIL: $POST['senders_email']\n";
$msg .="Senders Name: $POST['message']\n\n";
$to ="webguync@gmail.com";
$subject = "There has been a disturbance in the Force";
$mailheaders .="Reply-To: $_POST['sender_email']\n";
//send the mail
mail ($to, $subject, $msg, $mailheaders);
//display confirmation to user
echo "<p>mail has been sent!</p>";
} else if ($send =="no") {
//print error messages
echo "$name_err";
echo "$email_err";
echo "$message_err";
echo "$form_block";
}
}
?>
the error I now get is:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /hsphere/local/home/bruceg/inspired-evolution.com/Contact_Form_test.php on line 58
It looks like I am getting closer, but there still is a problem!
On line 38 and 103 you are using a short tag to open a PHP block. When I changed them to <?php it works on my server, but I'm not sure if thats the root problem or not.
<?
$form_block = "
<FORM METHOD=\"POST\" ACTION=\"$PHP_SELF\">
<p><strong>Your Name:</strong></p>
<INPUT type=\"text\" NAME=\"senders_name\" SIZE=30></p>
<p><strong>Your E-mail Address:</strong><p>
<INPUT type=\"text\" NAME=\"senders_email\" SIZE=30></p>
<p><strong>Message:</strong></p>
<TEXTAREA NAME=\"message\" COLS=30 ROWS=5 WRAP=virtual></TEXTAREA></p>
<INPUT type=\"hidden\" name=\"op\" value=\"ds\">
<p><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Send this Form\"></p>
</FORM>";
if ($_POST['op'] !="ds") {
// they need to see the form
echo "$form_block";
} else if ($_POST['op'] =="ds") {
//check value of $_POST['sender name']
if ($_POST['sender_name'] =="") {
$name_err = "<font color=red>Please enter your name!</font><br />";
$send = "no";
}
//check value of $_POST['sender_email']
if ($POST['sender_email'] =="") {
$email_err ="<font color=red>Please enter your email address!</font><br />";
$send= "no";
}
//check value of $_POST['message']
if ($POST['message'] =="") {
$message_err = "<font color=red>Please enter a message!</font><br />";
$send ="no";
}
if ($send !="no") {
//it's o.k to send, so build the mail
$msg ="E-MAIL SENT FROM WWW SITE\n";
$msg .="Senders Name: $POST{['senders_name']}\n";
$msg .="Senders E-MAIL: $POST{['senders_email']}\n";
$msg .="Senders Name: {$POST['message']}\n\n";
$to ="webguync@gmail.com";
$subject = "There has been a disturbance in the Force";
$mailheaders .="Reply-To: {$_POST['sender_email']}\n";
//send the mail
mail ($to, $subject, $msg, $mailheaders);
//display confirmation to user
echo "<p>mail has been sent!</p>";
} else if ($send =="no") {
//print error messages
echo "$name_err";
echo "$email_err";
echo "$message_err";
echo "$form_block";
}
}
?>