PHP表单在页面加载时自动提交

时间:2017-04-28 20:04:05

标签: php

我的PHP表单在我的页面加载时自动提交。当它自动提交时,我的SQL数据库会注册表单提交但没有重新收集数据。

不确定如何修复它。

这是我的代码。

if hlines is None: #in case HoughLinesP fails to return a set of lines
        #make sure that this is the right shape [[ ]] and ***not*** []
        hlines = [[0,0,0,0]]
    else:
        for l in hlines:
            leftx, boty, rightx, topy = l[0]
            cv2.line(frame, (leftx, boty), (rightx, topy), (0, 255, 0), 2)

2 个答案:

答案 0 :(得分:0)

将所有内容全部放入支票中,如果没有提交任何内容,则无需查询任何内容,如果提交,则无需查询。

<?php 
if(isset($_POST['submit'])){
    $con = mysqli_connect("localhost","subscriber","password","data-Base");

    $emailErr = "";
    $email = "";
    $Email = $_POST['email'];
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (empty($_POST["email"])) {
        $emailErr = "Email is required";
      } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $emailErr = "Please enter a Valid Email"; 
        }
      }
    }
    //form submission
    $sql = "INSERT INTO emails (Email) VALUES ('$Email')";
}
?>
<form action="">
    <input type="text" name="email">
    <input type="submit" name="submit" value="Submit">
</form>

或者,如果您在一个页面上有多个表单,则可以确定推送哪个提交按钮并采取相应措施。在这种情况下,您可能希望将$con单独移出而不是移动到该块中,否则您必须将其添加到每个块中。

<?php 
if($_POST['submit'] == 'email_submit'){
    $con = mysqli_connect("localhost","subscriber","password","data-Base");

    $emailErr = "";
    $email = "";
    $Email = $_POST['email'];
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (empty($_POST["email"])) {
        $emailErr = "Email is required";
      } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $emailErr = "Please enter a Valid Email"; 
        }
      }
    }
    //form submission
    $sql = "INSERT INTO emails (Email) VALUES ('$Email')";
}
?>
<form action="">
    <input type="text" name="email">
    <button type="submit" value="submit" value="email_submit">Submit</button>
</form>

这应该有效,但一如既往,没有承诺:)

答案 1 :(得分:-1)

因为这就是你要告诉它的事情。将其包装在isset提交中。理想情况下,我也会将测试放入其中,我会在之后显示,并给出错误计数,如果错误== 0,则提交。

<?php 
    //connection to database
    $con = mysqli_connect("localhost","subscriber","password","data-Base");
    //if we fail to connect

    $emailErr = "";
    $email = "";
    $Email = $_POST['email'];
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (empty($_POST["email"])) {
        $emailErr = "Email is required";
      } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $emailErr = "Please enter a Valid Email"; 
        }
      }
    }
if(isset($_POST['submit'])){
    //form submission
    $sql = "INSERT INTO emails (Email) VALUES ('$Email')";
}
?>
<?php
    // define variables and set to empty values
    function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }
    mysqli_close($con);
    if($email){
        $sent = "Thank You for Subscribing";
}
?>

<form id="login" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?
>" method="post">

更好的方法,将其全部包装在isset中,然后使用错误计数进行验证

<?php 
    //connection to database
    $con = mysqli_connect("localhost","subscriber","password","data-Base");
    //if we fail to connect

    $emailErr = "";
    $email = "";
    $Email = $_POST['email'];

if(isset($_POST['submit'])){
$errorCount = 0;


      if (empty($_POST["email"])) {
        $emailErr = "Email is required";
        $errorCount++;
      }

      $email = test_input($_POST["email"]);
      // check if e-mail address is well-formed
      if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $emailErr = "Please enter a Valid Email"; 
        $errorCount++;
      }

    if($errorCount == 0){
     //form submission
      $sql = "INSERT INTO emails (Email) VALUES ('$Email')";
    }

}
?>
<?php
    // define variables and set to empty values
    function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }
    mysqli_close($con);
    if($email){
        $sent = "Thank You for Subscribing";
}
?>

<form id="login" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?
>" method="post">