PHP联系表格不起作用
Below is the code for my PHP contact form. But when I submit it I get the following error:
" Parse error: syntax error, unexpected T_VARIABLE, expecting ')' in F:\wamp\www\peter harris\form.php on line 12 "
I want to know what is wrong with my code. I am fairly a beginner.
Thanks
<?php
$subject = $_POST['Contact Form'];
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$event_location = $_POST['event-location'];
$event_date = $_POST['event-date'];
$event_time = $_POST['event-time'];
$message = $_POST['message'];
$date = date(r);
$list=array($name,$email,$phone,$event_location,$event_date,$event_time,$date);
$strTo = "example@domain.com";
$strSubject = " Contact Form -";
$strFrom = trim($_POST["Contact Form - name"]);
$str_content = "From". trim($_POST["name"]) . "
" . "Email: " . trim($_POST["EMAIL"]) . "
" . "Phone: " . trim($_POST["phone"]) . "
" .
"Event Location: " . trim($_POST["event-location"]) . "
" . "Event Date: " . trim($_POST["event-date"]) . "
" . "Event Time: " . trim($_POST["event-time"]) . "
" .
"Message: " . trim($_POST["message"]) . "
" . "Date: " . trim($_POST["date"]) . "
" ;
mail($strTo,$strFrom,$strSubject,$str_content);
echo ("<SCRIPT LANGUAGE='JavaScript'></SCRIPT>");
echo "<script>alert(' YOUR EMAIL HAS BEEN SENT. ')</script>";
echo "<script>window.history.go(-1)</script>";
?>
My HTML code for the FORM:
<form id="contact-form" name="contact-form" action="form.php" method="post"
onsubmit="return validateForm()">
<input id="name" type="name" name="name" value="NAME"
onfocus="if (this.value=='NAME') this.value='';" onblur="if (this.value=='')
this.value='NAME';"/><br />
<input id="email" type="email" name="email" value="EMAIL" onfocus="if
(this.value=='EMAIL') this.value='';" onblur="if (this.value=='')
this.value='EMAIL';" /><br />
<input id="phone" type="text" name="phone" value="PHONE" class="phone" onfocus="if
(this.value=='PHONE') this.value='';" onblur="if (this.value=='')
this.value='PHONE';" /><br />
<input type="text" name="event-location" value="EVENT LOCATION" onfocus="if
(this.value=='EVENT LOCATION') this.value='';" onblur="if (this.value=='')
this.value='EVENT LOCATION';" /><br />
<input type="text" name="event-date" value="EVENT DATE" onfocus="if
(this.value=='EVENT DATE') this.value='';" onblur="if (this.value=='')
this.value='EVENT DATE';" /><br />
<input type="text" name="event-time" value="EVENT TIME" onfocus="if
(this.value=='EVENT TIME') this.value='';" onblur="if (this.value=='')
this.value='EVENT TIME';" /><br />
<input type="text" name="message" value="MESSAGE" class="message" onfocus="if
(this.value=='MESSAGE') this.value='';" onblur="if (this.value=='')
this.value='MESSAGE';" /><br />
<input type="submit" name="send" value="SEND" />
</form>
Besides the syntax errors, this is how your code "should look like":
<?php
// Trim all POST strings
$data = array_map(function($value){
return is_string($value) ? trim($value) : $value;
}, $_POST);
$body = "From {$data['name']}
".
"Email: {$data['email']}
".
"Phone: {$data['phone']}
".
"Event Location: {$data['event-location']}
".
"Event Date: {$data['event-date']}
".
"Event Time: {$data['event-time']}
".
"Message: {$data['message']}
".
"Date: {".date('r')."}
";
mail('example@domain.com',$data['name'],'Contact', $body);
?>
<script type="text/javascript">
alert('Your email has been sent.');
window.history.go(-1);
</script>
Few things you should know:
it's not really recommended to have query variable names with spaces
you should validate the data you're getting; if you've put some field in your form it doesn't mean you'll always get it at all (not to mention the right format)
there is practically no difference between
$something
and$array['something']
so you don't need to define each array key you need to use as new variablethere is no need to have 3 script tags in order to have 2 function calls
try not to output HTML through PHP (and separate the presentation from logic altogether)
"
" . ; "Event Time: "
you have a semi colon there that breaks the line.
ok I see some inconsistencies with the $_POST['Variable']
calls.
$subject = $_POST['Contact Form- '];
$strFrom = trim($_POST["Contact Form - name"]);
Both of those $_POST variables do not exist in the form you are sending comment them out. and change $strFrom to = $name.