Forum Moderators: coopster
I am trying to do a little php script to send a form from the .htm
On hitting the submit button, I'm getting this:
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/content/f/i/r/firstodd/html/contact.php on line 3
Parse error: parse error, unexpected T_VARIABLE in /home/content/f/i/r/firstodd/html/contact.php on line 4
--
I'm don't know anything about this stuff, I'm just modifying a guide showing how to do it. Here is the .php down to the 4th line.. modified to not reveal information:
<?php
$to = "email@example.net";
$subject = "Subject";
$applicant first name = $_REQUEST['applicant first name'] ;
and there is about 50-100 lines similar before it ends, one for each <input .
Any insight?
[edited by: eelixduppy at 7:02 pm (utc) on June 19, 2007]
[edit reason] example.net [/edit]
how do they appear on the email as one name? is it possible to do spaces, %20 or something similiar?
You mixed two things here. One thing is variable name, and one thing is variable value:
here name and value are the same:
$a = "a";
And here aren't
$name = "John Smith";
So you cannot place a space (nor % sign) in the name of the variable:
incorrect:
$n ame = "John Smith";
$n'ame = "John Smith";
$n%ame = "John Smith";
$n!ame = "John Smith";
$n@ame = "John Smith";
only underline and maybe some other special character is correct:
$n_ame = "John Smith"; // this is correct
Hope this help you to understand error you made.
Regards
Michal
PS. And welcome to Webmasterworld!
PSS. Yes, request needs to be one word as well, as it is an array reference.
Moreover better not to use $_REQUEST.
Use $_POST, $_GET, $_COOKIE or $_SESSION instead. Why? Because while using request you don't have any control on where the data comes from.
So I replaced both the <input> tags and the php spaces with underscores. The setup now looks like this:
<?php
$to = "email@email.net";
$subject = "Subject";
$applicant_first_name = $_REQUEST['applicant_first_name'] ;
coming off of
Applicant First Name: <input name="applicant_first_name" type="text" size="14">
in the html
I'm still getting :
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/content/f/i/r/firstodd/html/contact.php on line 3
Parse error: parse error, unexpected T_VARIABLE in /home/content/f/i/r/firstodd/html/contact.php on line 4
when I press submit.
More help?
And instead of $_REQUEST, you should be using either $_POST or $_GET unless you absolutely need both, which I do not think is the case here.
I'm just following the guide here:
[php.about.com...]
So the Post and Gets also function for what I'm trying to do? No need to try to explain to me what they actually mean, like I said I'm just going off of that little guide.
That is what it seems like, that it's not knowing where to get the info or something. I'll replace the REQUESTS on monday and get back to you.
I also noticed mcibor's examples don't have a space between the end of the line and the semi colon, would that be a cause?
Thanks again
nope, should still work the same without error.
A script like this should work - this would be using two files, a html for the form (form.html) and a php mail script (mail.php), you could have them all on the same page, but I prefer to separate them as I find it easier for securing th mail script from SPAM.
form.html -
<form id="form1" name="form1" method="post" action="mail.php">
<label>First Name :
<input type="text" name="fname" />
</label>
<input type="submit" />
</form>
mail.php -
<?PHP
$to = "email@example.net";
$subject = "Subject";
$fName = $_POST['fName'];
$headers = "From: yourfromemail@yourdomain.com";
$message = "your message here";
mail($to, $subject, $message, $headers);
header(location: form.html);
?>
You really should look at some basic tutorials into this, and to passing variables in php, there are various good ones around, W3Schools has good introductory tutorials. You should also look into methods of protecting your mail script from SPAM.
you need to be looing for a stray on unproperly formated / and the unexpected T_variable parse error is normally when the line is not formatted properly.
You need to paste your ENTIRE code for us to see where the problem lies. i'ts no good showing part.
form.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Title</title>
</head>
<body>
<form method="post" action="contact.php">
Email: <input name="email" type="text"><br>
Message:<br>
<textarea name="message" rows="15" cols="40"></textarea><br>
<input type="submit">
</form>
</body>
</html>
contact.php:
<?php
\$to = "email@email.net";
\$subject = "Contact Us";
\$email = $_REQUEST['email'] ;
\$message = $_REQUEST['message'] ;
\$headers = "From: $email";
\$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>
I've done this before on the same host and it worked fine, I don't know what is up. In this try, the only thing I even changed was the email address (which I changed again to post).
This is what your code should be
<?php
$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers);
if($sent){
print "Your mail was sent successfully";
} else {
print "We encountered an error sending your mail";
}
?>
$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers);
Shouldn't the full headers be written, return path and that sort of thing. Email without the complete headers never works on my server. Am I missing something?
Habtom
this one
$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];
$headers .= "From: $email\n";
$headers .= "Return-Path: $email\n";
$headers .= "Reply-To: $email\n";
$sent = mail($to, $subject, $message, $headers);
and if not then try adding \r to those
$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];
$headers .= "From: $email\r\n";
$headers .= "Return-Path: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$sent = mail($to, $subject, $message, $headers);
see if those extras make any difference. You should have at least From, Reply-To and Return-Path when sending messages.
There was backslashes in the front because searching around for the error I found a forum that mentioned doing that solved it.
----
<?php
$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers);
if($sent){
print "Your mail was sent successfully";
} else {
print "We encountered an error sending your mail";
}
?>
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/content/f/i/r/firstodd/html/contact.php on line 2
Parse error: parse error, unexpected T_STRING in /home/content/f/i/r/firstodd/html/contact.php on line 2
$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];$headers .= "From: $email\n";
$headers .= "Return-Path: $email\n";
$headers .= "Reply-To: $email\n";$sent = mail($to, $subject, $message, $headers);
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/content/f/i/r/firstodd/html/contact.php on line 2
Parse error: parse error, unexpected T_STRING in /home/content/f/i/r/firstodd/html/contact.php on line 2
$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];$headers .= "From: $email\r\n";
$headers .= "Return-Path: $email\r\n";
$headers .= "Reply-To: $email\r\n";$sent = mail($to, $subject, $message, $headers);
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/content/f/i/r/firstodd/html/contact.php on line 2
Parse error: parse error, unexpected T_STRING in /home/content/f/i/r/firstodd/html/contact.php on line 2
-----
I think something larger here is wrong, maybe the server or host doesn't support it or something? None of these are returning any difference in error, except that it's on line 2 now, and I haven't changed line 1 or 2 at all from when it was returning in on line 3 and 4.
=)
I'll try to to figure out another way to do this, I think it's just not supported or something. Thanks for all the help!
Tyler
I have seen things similar to this and fixed it by opening the old file in a pure text editor, copying all content, pasting into a new file, saving with a new name, deleting old files from the server and uploading the new file.
That worked. Not sure if it was making the new file or switching from Wordpad to Notepad.
Now, script above works great w/ email & message, my only other question is how to add more inputs.
I tried adding more $x = $_POST['x']; lines, and then adding them to the mail( , but it's returning at error on that specific line, so I assume this isn't how you do it.
Sorry to take up so much of your times, I really appreciate it.
you can loop through the POST array and append it to your message var if you like, simple, and not overly secure but it will work
$message = '';
foreach ($_POST as $key => $value) {
$message .= $key . ': ' . $value . "\n";
}
that would put all your $_POST values into the message
but I understand that, the mail( function is just setup wrong
---
On the POST loop, how will the formatting by when the email is sent? I'm looking at around 50 input fields, sent to not me but the company I'm doing the site for, so I don't want them to come clumped together.
send it to yourself and then you can look at formatting
I seldom do the loop way any more as I need to validate fields and usually styling the message needs more individual treatment. I build it field by field to lay the whole thing out. If you are building it for a company then you should go the same route.
I'm not sure what you mean by field by field. It should work fine as it is, it comes out like this:
email: email@email.net
fname: firstodd
lname: ladast
otherinfo: amsfmksafmlk
asdfkasf
asfa
sf
asdf
saf
how would I go about making the email fname lname entrys bold? Or if thats not possible, the other text smaller?
Sorry, as I said, I don't know the code that well.
Thanks!
$message = '';
foreach ($_POST as $key => $value) {
$message .= $key . ': ' . $value . "\n";
}
if you wanted to bold it then it would look like this
$message = '';
foreach ($_POST as $key => $value) {
$message .= '<b>' . $key . '</b>: ' . $value . "\n";
}
though this is adding html so it would have to be html which means you may need to adjust headers and also add an html line break
$message = '';
foreach ($_POST as $key => $value) {
$message .= '<b>' . $key . '</b>: ' . $value . "<br>\n";
}
if you're sending plain text then it's just, well, plain
You can manually format the $message like this, as jatar_k says you will need to set html headers for the email client to recognise the html used to format the message. But if you include html, it is wise to have a plain text alternative for users without html email. The example below would send a html message and a plain text alternative, the email client will choose which one to show.
<?PHP
//Capture POST data from form
$fname = $_POST['fname'];
$lname = $_POST['lame'];
$email = $_POST['email'];
$enquiry = $_POST['enquiry'];
//Create unique boundary id
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//Set Headers
$mailto = "youremail@yourdomain.com";
$subject = "Enquiry from site";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/alternative;\n" .
" boundary=\"{$mime_boundary}\"";
//Set formatted message with plain text and html alternatives
$message = "
$mime_boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
First Name: $fname
Last Name: $lname
Email: $email
Enquiry: $enquiry
$mime_boundary
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
<html>
<body>
<strong>First Name:</strong> $fname
<strong>Last Name:</strong> $lname
<strong>Email:</strong> $email
<strong>Message:</strong> $enquiry
</body>
</html>
$mime_boundary
";
mail($mailto, $subject, $message, $headers);
?>