完成javascript函数后提交表单[PHP]

时间:2016-12-25 12:40:20

标签: javascript php html css

大家圣诞快乐。我正在构建一个小型登录页面,其中包含一个表单。我已经通过CPA优惠将此表单货币化,我希望发生的是在内容锁定小部件关闭后获取用户输入。 我尝试了很多方法,但我有错误,一旦你点击按钮,表单提交自己,用户没有必要完成优惠。 我必须调用的javascript函数是call_locker();

如何在call_locker();功能完成后提交表单?

的index.php

<html>
<head>
<meta charset="UTF-8">
<title>Complete a survey to become part of our team.</title>

<!-- Start of content locker code -->

<noscript><meta http-equiv="refresh" content="0;url=https://www.appcaptcha.com/contentlockers/noscript.php" /></noscript>

<script type="text/javascript">var ogblock=true;</script>

<script type="text/javascript" src="https://www.appcaptcha.com/contentlockers/load.php?id=76db12dda6691911c8a119fe7043facd"></script>

<script type="text/javascript">if(ogblock) window.location.href = "https://www.appcaptcha.com/contentlockers/adblock.php";</script>

<!-- End of content locker code -->

</head>
<body>
      <?php
        $userErr ="";
        $emailErr ="";

        $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        if (strpos($url, 'error=user-empty') !== false) {
            $userErr ="Please enter your username!";

        }

        if (strpos($url, 'error=email-empty') !== false) {
            $emailErr ="Please enter your email!";
                echo $emailErr;
        }

        if (strpos($url, 'error=email-incorrect') !== false) {
            $emailErr ="Please enter a valid email!";
                echo $emailErr;
        }
        if (strpos($url, 'error=succes') !== false) {
            $entry = 'You have entered succesfully!';       
        }

      ?>
<h1> Please enter the following info: </h1>

<form method="post" action="enter.php">

Username: <input type="text" name="username" placeholder="Username" /> <br>

<span class="error"><?php echo $userErr ?></span><br>
E-mail: <input type="text" name="email" placeholder="E-mail" /><br>
<span class="error"><?php echo $emailErr ?></span><br>
Social Media: <input type="text" name="smedia" placeholder="Enter your Facebook, twitter, Skype, profile URL" /> (Optional)<br>
<input type="submit" value="Enter" />
<?php echo $entry ?>
</form>
</body>

</html>

enter.php

<?php

include 'connect-mysql.php';

    $username = $_POST['username'];
    $email = $_POST['email'];
    $smedia = $_POST['smedia'];

    if(empty($username)) {
            header("Location: index.php?error=user-empty");
            exit();

    }
    if(empty($email)) {
            header("Location: index.php?error=email-empty");
            exit();

    }
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            header("Location: index.php?error=email-incorrect");
            exit();
    }
    else {

    $sql = "INSERT INTO user (username, email, socialmedia) VALUES ('$username', '$email', '$smedia')";

    $result = mysqli_query($dbcon, $sql);
    header("Location: index.php?error=succes");


    };
?>

2 个答案:

答案 0 :(得分:0)

将id属性设置为&#34; myForm&#34;对于您的表单,并在您的javascript函数结束时使用以下内容。

document.getElementById("myForm").submit();

编辑:点击按钮调用该功能,而不是使用提交按钮。

答案 1 :(得分:0)

您需要阻止提交按钮提交表单,因此使用: “event.preventDefault();”或“返回假”; event.preventDefault() vs. return false

然后在脚本结束时,您可以使用以下方式提交表单: document.getElementsByTagname( “形式”)[0] .submit();