简单的语法PHP错误

时间:2016-03-07 04:43:25

标签: php syntax-error

我收到语法错误

  

解析错误:语法错误,意外'文字' (T_STRING)在C:......第18行。

我不知道为什么会收到此错误。响应越快越好。非常感谢你。

<?php
session_start();
?>
<!doctype html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Login</title>
</head>
    <body>
        <div align="center">
            <img src="logo.png" alt="school logo">
            <h2>login</h2>
            <?php
            $form="<form action='./login.php'  method='post'>
            <table>
            <tr>
                <td>Email:</td>
                <td>input type="text" name:"Email"/></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="text" name:"Password"/></td>
            </tr>
            <tr>
                <td><</td>
                <td><input type="submit" name="loginbtn" value="login"/></td>
            </tr>
            </table>
            </form>";

            if ($_POST['loginbtn']){
                $email=$_POST["email"];
                $Password=$_POST["Password"];

                if($email){
                    if($Password){
                    }
                    else
                        echo"you must enter your password .$form";
                }
                else
                    echo "you must enter your email .$form";

            }
            else
                echo"";
            ?>
        </div>
    </body>
</html>

4 个答案:

答案 0 :(得分:1)

试试这个

<?php
session_start();
?>
<!doctype html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Login</title>
</head>
    <body>
        <div align="center">
            <img src="logo.png" alt="school logo">
            <h2>login</h2>
            <?php
            $form="<form action='./login.php'  method='post'>
            <table>
            <tr>
                <td>Email:</td>
                <td>input type='text' name:'Email'/></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type='text' name:'Password'/></td>
            </tr>
            <tr>
                <td><</td>
                <td><input type='submit' name='loginbtn' value='login'/></td>
            </tr>
            </table>
            </form>";

            if ($_POST['loginbtn']){
                $email=$_POST["email"];
                $Password=$_POST["Password"];

                if($email){
                    if($Password){
                    }
                    else
                        echo"you must enter your password .$form";
                }
                else
                    echo "you must enter your email .$form";

            }
            else
                echo"";
            ?>
        </div>
    </body>
</html>

将双列更改为单列

答案 1 :(得分:1)

您需要转义HTML中的"

$form="<form action='./login.php'  method='post'>
            <table>
            <tr>
                <td>Email:</td>
                <td>input type=\"text\" name:\"Email\"/></td>
            ...
            </form>";

答案 2 :(得分:0)

最简单的方法是:

<?php
session_start();
?>
<!doctype html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Login</title>
</head>
    <body>
        <div align="center">
            <img src="logo.png" alt="school logo">
            <h2>login</h2>
            <?php
            if (isset($_POST['loginbtn'])){ //Use isset otherwise you may get undefined variable errors
                $email=$_POST["email"];
                $Password=$_POST["Password"];
                if(!empty($email) || !empty($Password)) //If you use !$email and the variable isn't set, you get a warning.
                {
                    if(!empty($email)) //Empty checks if it exists + has a value
                        echo "you must enter your password<br />"; //Must add a new line with <br />
                    if(!empty($Password))
                        echo "you must enter your email <br />";
                    ?>
                    <form action='./login.php'  method='post'>
                        <table>
                            <tr>
                                <td>Email:</td>
                                <td>input type="text" name:"Email"/></td>
                            </tr>
                            <tr>
                                <td>Password:</td>
                                <td><input type="text" name:"Password"/></td>
                            </tr>
                            <tr>
                                <td><</td>
                                <td><input type="submit" name="loginbtn" value="login"/></td>
                            </tr>
                        </table>
                    </form>
                    <?php
                }
            }
            ?>
        </div>
    </body>
</html>

它解决了以下问题:

  • 您的语法问题
  • 不需要在字符串中放入大量HTML
  • 使用issetempty
  • 修复任何可能的未定义索引
  • 压缩代码并使其更易于阅读。

答案 3 :(得分:0)

您使用了double-qoute(&#34;&#34;)为$ form分配值。因此,理想情况下,你应该在侧面使用单qoute.Like:

    $form="<form action='./login.php'  method='post'>
                    <table>
                    <tr>
                        <td>Email:</td>
                        <td>input type='text' name:'Email'/></td>
                    </tr>
                    <tr>
                        <td>Password:</td>
                        <td><input type='text' name:'Password'/></td>
                    </tr>
                    <tr>
                        <td><</td>
                        <td><input type='submit' name='loginbtn' value='login'/></td>
                    </tr>
                    </table>
                    </form>";

或者你可以使用转义字符串使用双引号,如:

    $form="<form action='./login.php'  method='post'>
        <table>
        <tr>
            <td>Email:</td>
            <td>input type=\"text\" name:\"Email\"/></td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type=\"text\" name:\"Password\"/></td>
        </tr>
        <tr>
            <td><</td>
            <td><input type=\"submit\" name=\"loginbtn\" value=\"login\"/></td>
        </tr>
        </table>
        </form>";