提交按钮不起作用?

时间:2013-01-17 02:10:38

标签: php html forms button

我有这个小测试页试图弄乱并弄清楚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>

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

** * ** * ** EDIT * ** * ** 固定 ** 找到了解决方法!没有功能,就像魅力一样。

<!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为null(当它们第一次加载时),它将会 只需将$ email2更改为“notnull”并停止检查语句,因为它找到了有效的 一。 (不是100%)

2 个答案:

答案 0 :(得分:4)

您的提交按钮在哪里?

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

类型应为submit

正如本答案评论所述,你不能从javascript调用php函数。

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

Uncaught ReferenceError: checkEmail is not defined

答案 1 :(得分:0)

类型应该提交;

如果您将其更改为提交然后运行它,它应该没问题。您应该使用带有表单元素的提交类型。