【问题标题】:Submit button not working?提交按钮不起作用?
【发布时间】:2013-01-17 02:10:38
【问题描述】:

我有这个小测试页面试图弄乱并找出 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%)

【问题讨论】:

    标签: php html forms button


    【解决方案1】:

    您的提交按钮在哪里?

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

    类型应该是submit

    正如这个答案 cmets 中提到的,您不能从 javascript 调用 php 函数。

    执行此操作时,您将从未定义的 javascript 调用 checkEmail
    因此,您将收到以下错误:

    Uncaught ReferenceError: checkEmail is not defined
    

    【讨论】:

    • 你没有提到他试图从javascript调用一个php函数?
    • @SamDufel 让我们一步一步来;)
    • 哦,我真傻!谢谢你。此外,单击时,我将被发送到我的主页。我不喜欢那样......现在这个功能怎么样?
    • @TheWalkingPacifist 您必须创建一个 javascript 函数来验证它。你可以简单地google一下。有很多演示展示了如何做到这一点。
    • 呃,这一天太多了。我明天去找找。如果您能在我不在的时候发布有用的链接或其他内容,那就太好了。
    【解决方案2】:

    类型应该是提交;

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

    【讨论】:

      猜你喜欢
      • 2013-04-09
      • 2017-03-13
      • 2015-03-27
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多