0

我有这个小测试页面试图弄乱并找出PHP代码,据我所知,在填写表格并点击提交后,应该会发生一些事情。取决于你输入的内容。

<!DOCTYPE html>
<html>
<head>
    <title>PHP Testing Page</title>
</head>
<body>
    <?php
        echo "Testing Page, working great!\n";
        $theDate = date("M-d-Y ");
        echo "Today is " . $theDate;
        $email1 = $_POST['email1'];
        $email2 = $_POST['email2'];
        function checkEmail()
        {
            $email1 = $_POST['email1'];
            $email2 = $_POST['email2'];
            echo $email1;
            echo $email2;
                if($email1==$email2)
                {
                    echo "\nE-Mail Addresses match.";
                }
                else
                {
                    echo "\nCheck to make sure your E-Mails match";
                }
            }
        ?>
        <form name="checkingEmail" action="." method="post">
            E-Mail: <input type="text" name="email1" value="E-Mail Here" />
            <br />
            Retype E-Mail: <input type="text" name="email2" value="Confirm E-Mail" />
            <br />
            <input type="button" value="Submit" onClick="checkEmail()">
        </form>
    </body>
</html>

填写表格(通过访问页面)并单击提交按钮后,没有任何反应。有人可以解释一下吗?

** * ** * **编辑* ** * **已修复** 找到解决方法!没有功能,就像一个魅力。

<!DOCTYPE html>
<html>
<head>
    <title>PHP Testing Page</title>
</head>
<body>
    <?php
        echo "Testing Page, working great!\n";
        $theDate = date("M-d-Y ");
        echo "Today is " . $theDate;
    ?>
    <form name="checkingEmail" action="test.php" method="post">
        E-Mail: <input type="text" name="email1" value="E-Mail Here" />
        <br />
        Retype E-Mail: <input type="text" name="email2" value="Confirm E-Mail" />
        <br />
        <input type="submit" value="Submit">
    </form>
    <?php 
    $email1 = $_POST["email1"];
    $email2 = $_POST["email2"];
    if($email2==null)
    {
/*I believe this just stops it from checking the rest of the conditions, that
        way it won't echo anything until someones enters valid (non-null) input*/
        $email2 = "notnull";
    }
    else if($email1==$email2)
    {
        echo "Good Job.";
    }
    else
    {
        echo "Failure to comply.";
    }
    ?>
</body>

我有一个函数之外的检查,所以我不必调用它或类似的东西。此外,对于第一个 if 语句,如果 $email2 为空(当他们第一次加载它时),它只会将 $email2 更改为“notnull”并停止检查语句,因为它找到了一个有效的语句。(不是 100%)

4

2 回答 2

4

你的提交按钮在哪里?

<input type="button" value="Submit" onClick="checkEmail()">
               ^

类型应该是submit

正如此答案评论中所述,您不能从 javascript 调用 php 函数。

当您这样做时,您将从checkEmail未定义的 javascript 调用。
因此,您将收到以下错误:

Uncaught ReferenceError: checkEmail is not defined
于 2013-01-17T02:12:02.157 回答
0

类型应提交;

如果你把它改成提交,然后运行它,应该没问题。您应该将提交类型与表单元素一起使用。

于 2013-01-17T02:13:14.917 回答