dongyan3853 2011-11-30 18:30
浏览 55
已采纳

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>
  • 写回答

2条回答 默认 最新

  • dongshan3806 2011-11-30 19:05
    关注

    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:

    1. it's not really recommended to have query variable names with spaces

    2. 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)

    3. 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 variable

    4. there is no need to have 3 script tags in order to have 2 function calls

    5. try not to output HTML through PHP (and separate the presentation from logic altogether)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 nrf52810-c三个a 程序
  • ¥15 lego-loam跑出来的roll误差很大
  • ¥50 求一个半透明没有锯齿的圆角窗体的实现例子
  • ¥15 STM32cubeMX里的FreeRTOS无法释放内存
  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对