【问题标题】:syntax error,unexpected T_else online 81语法错误,意外的 T_else 在线 81
【发布时间】:2012-01-06 16:50:06
【问题描述】:

我正在尝试使用 html 和 php 构建联系表单,但我收到了一个消息解析错误:,语法错误,第 81 行 c:\xampp\htdocs\myfolder\form_process.php 中的意外 T_ELSE,我真的这样做了不知道我哪里做错了,这是我下面的代码

<?php

    session_start();

      if($_POST['submit'])
      {
       $num = 0;

       if(strlen($_POST['name']) > 0)
        $num = $num;
         $_SESSION['name'] = $_POST['name'];
         unset($_SESSION['error_name']);
      }
      else 
      {
       $num = $num + 1;
       $_SESSION['error_name'] = "You have not filled out a Name";

      }

       if(strlen($_POST['surname']) > 0){
        $num = $num;
         $_SESSION['surname'] = $_POST['surname'];
         unset($_SESSION['error_surname']);
      }

      else 
      {
       $num = $num + 1;
       $_SESSION['error_surname'] = "You have not filled out a Surname";

      }

       if(strlen($_POST['phone']) > 0){
        $num = $num;
         $_SESSION['phone'] = $_POST['phone'];
         unset($_SESSION['error_phone']);
      }
      else 
      {
       $num = $num + 1;
       $_SESSION['error_phone'] = "You have not filled out a Phone Number";

      }

       if(strlen($_POST['email']) > 0){
        $num = $num;
         $_SESSION['email'] = $_POST['email'];
         unset($_SESSION['error_email']);
      }
      else 
      {
       $num = $num + 1;
       $_SESSION['error_email'] = "You have not filled out a Email Address";

      }

       if(strlen($_POST['comments']) > 0){
        $num = $num;
         $_SESSION['comments'] = $_POST['comments'];
         unset($_SESSION['error_comments']);
      }
      else 
      {
       $num = $num + 1;
       $_SESSION['error_comments'] = "You have not filled out a Comment";

      }

         if ($num == 0)
          {
            //process form
             echo "success";
            }
            else
            {
            header("Location: inter.php");      
            }


     else{

       header("location: inter.php");
      }

    ?>

有人帮忙

【问题讨论】:

  • 如果你正确缩进你的代码,你会立即看到这种错误。
  • 这个问题似乎是题外话,因为它是关于调试代码的。

标签: php apache


【解决方案1】:

您的代码底部有一个未关闭的 if 语句。签入您的代码。

if{  

...

}else{
    header("location: inter.php");
} 

【讨论】:

  • 非常感谢,但我收到另一个错误,表单无法按照预期的方式工作,如果我点击提交按钮,它会将我重定向到主页,如果我返回联系人,那就是当它显示我没有输入任何内容时,如果我单击重新设置按钮,它没有做任何事情
  • 这是一个不同的问题。如果您仍然没有收到相同的错误,请接受其中一个答案并询问另一个答案。我无法真正回答您的问题,因为我不知道您处理表单的代码。
【解决方案2】:

您的 else { header("location: inter.php"); } 与其他任何东西都不平衡 :)

您绝对应该使用一致的缩进并一致地放置花括号。

这是一个更新,带有(缺少?)花括号:

<?php
  session_start();

  if($_POST['submit']) {
    $num = 0;

    if(strlen($_POST['name']) > 0) {
        $num = $num;
         $_SESSION['name'] = $_POST['name'];
         unset($_SESSION['error_name']);
    }
    else {
       $num = $num + 1;
       $_SESSION['error_name'] = "You have not filled out a Name";

    }

    if(strlen($_POST['surname']) > 0){
        $num = $num;
         $_SESSION['surname'] = $_POST['surname'];
         unset($_SESSION['error_surname']);
    }
    else {
       $num = $num + 1;
       $_SESSION['error_surname'] = "You have not filled out a Surname";

    }

    if(strlen($_POST['phone']) > 0){
        $num = $num;
         $_SESSION['phone'] = $_POST['phone'];
         unset($_SESSION['error_phone']);
    }
    else {
       $num = $num + 1;
       $_SESSION['error_phone'] = "You have not filled out a Phone Number";
    }

    if(strlen($_POST['email']) > 0){
        $num = $num;
         $_SESSION['email'] = $_POST['email'];
         unset($_SESSION['error_email']);
    }
    else {
       $num = $num + 1;
       $_SESSION['error_email'] = "You have not filled out a Email Address";
    }

    if(strlen($_POST['comments']) > 0){
        $num = $num;
         $_SESSION['comments'] = $_POST['comments'];
         unset($_SESSION['error_comments']);
    }
    else {
       $num = $num + 1;
       $_SESSION['error_comments'] = "You have not filled out a Comment";
      }

    if ($num == 0) {
            //process form
             echo "success";
    }
    else {
            header("Location: inter.php");      
    }
  }
  else {
       header("location: inter.php");
  }
  ?>

【讨论】:

    【解决方案3】:

    我认为您缺少括号

            header("Location: inter.php");      
        }
    
    } // This one is missing
    else {
    

    【讨论】:

      【解决方案4】:

      你在if(strlen($_POST['name']) &gt; 0) 行缺少一个左大括号,它应该开始:

      <?php
      
      session_start();
      
      if($_POST['submit'])
      {
          $num = 0;
      
          if(strlen($_POST['name']) > 0) 
          { // added a brace here
              $num = $num;
              $_SESSION['name'] = $_POST['name'];
              unset($_SESSION['error_name']);
          }
          else 
      

      通过确保您的代码有条不紊地布局,或者使用显示匹配大括号的编辑器,您可以更轻松地找到类似的内容。

      【讨论】:

      • 非常感谢,但我收到另一个错误,表单无法按照预期的方式工作,如果我点击提交按钮,它会将我重定向到主页,如果我返回联系人,那就是当它显示我没有输入任何内容时,如果我单击重新设置按钮,它没有做任何事情
      • 也许您的表单没有通过 POST (method="post") 提交,或者您没有提交 $_POST['submit'] (&lt;input type="submit" name="submit" value="Submit"&gt;) 的值。
      • 我用同样的方法可以吗,如果你能给我你的电子邮件地址,如果你不介意我把整个发送给你吗?我的电子邮件是 elgin19@gmail.com 我有点新php 和我尝试建立一个简单的网站
      【解决方案5】:

      还有两个相同的 if

      if ($num == 0)
                {
                  //process form
                   echo "success";
                  }
                  else
                  {
                  header("Location: inter.php");      
                  }
      
      
           else{
      
             header("location: inter.php");
            }
      
          ?>
      

      【讨论】:

      • 非常感谢,但我收到另一个错误,表单无法按照预期的方式工作,如果我点击提交按钮,它会将我重定向到主页,如果我返回联系人,那就是当它显示我没有输入任何内容时,如果我单击重新设置按钮,它没有做任何事情
      猜你喜欢
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多