PHP联系表格不起作用

时间:2011-11-30 18:30:10

标签: php html

以下是我的PHP联系表单的代码。但是当我提交它时,我收到以下错误:

“解析错误:语法错误,意外的T_VARIABLE,期待')'在第12行的F:\ wamp \ www \ peter harris \ form.php”

我想知道我的代码有什么问题。我是一个相当初学者。

由于

              <?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"]) . "\r\n" . "Email: " . trim($_POST["EMAIL"]) . "\r\n" . "Phone: " . trim($_POST["phone"]) . "\r\n" . 
        "Event Location: " . trim($_POST["event-location"]) . "\r\n" . "Event Date: " . trim($_POST["event-date"]) . "\r\n" .  "Event Time: " . trim($_POST["event-time"]) . "\r\n" .
        "Message: " . trim($_POST["message"]) . "\r\n" . "Date: " .      trim($_POST["date"]) . "\r\n" ;
                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>";        

  ?>

我的FORM代码的HTML代码:

<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>

2 个答案:

答案 0 :(得分:4)

"\r\n" . ; "Event Time: "你在那里有一个半结肠打破了这条线。

好的,我发现与$_POST['Variable']来电有些不一致。

$subject = $_POST['Contact Form- '];

$strFrom = trim($_POST["Contact Form - name"]);

这两个$ _POST变量都不存在于您发送注释的表单中。并将$ strFrom更改为= $ name。

答案 1 :(得分:-1)

除了语法错误之外,这就是你的代码“看起来像”的样子:

<?php
// Trim all POST strings
$data = array_map(function($value){
    return is_string($value) ? trim($value) : $value;
}, $_POST);

$body = "From {$data['name']}\r\n".
    "Email: {$data['email']}\r\n".
    "Phone: {$data['phone']}\r\n". 
    "Event Location: {$data['event-location']}\r\n".
    "Event Date: {$data['event-date']}\r\n".
    "Event Time: {$data['event-time']}\r\n".
    "Message: {$data['message']}\r\n".
    "Date: {".date('r')."}\r\n";

mail('example@domain.com',$data['name'],'Contact', $body);

?>

<script type="text/javascript">
alert('Your email has been sent.');
window.history.go(-1);
</script>

你应该知道几件事:

  1. 不建议将查询变量名用空格

  2. 您应该验证您获得的数据;如果你在表单中添加了一些字段,那并不意味着你总是会得到它(更不用说正确的格式)

  3. $something$array['something']之间几乎没有区别,因此您无需定义需要用作新变量的每个数组键

  4. 没有必要有3个脚本标签才能进行2次函数调用

  5. 尽量不通过PHP输出HTML(并将演示文稿与逻辑完全分开)