如何在没有提交按钮的情况下将页面的值传递到其他页面

时间:2018-12-04 07:04:55

标签: javascript php jsp

该页面已通过infoCheck.php进行POST。

首先,我将继续进行如下操作。

<form id="theForm" method = "POST" action = "infoCheck.php"
   <input type = "hidden" name = "name" value = "100">
   <input type = "submit" value = "Input">
</form>

但是,在提交之前,只要打开页面,就只发送与名称相对应的值。一定要走了。即使我搜索了,也只能看到如何使用提交... 我该怎么办?

4 个答案:

答案 0 :(得分:4)

如果使用jQuery,请尝试以下操作:

HTML

<form id="theForm" method="POST" action="infoCheck.php">
  <input type="hidden" name="name" value="100">
  <input type="submit" value="Input">
</form>

jQuery:

<script type="text/javascript">
    $(function() {
        $("#theForm").submit();
    });
</script>

答案 1 :(得分:0)

我的猜测是您希望在页面加载后立即提交表单。这是您可以使用JS(根据您问题中的标记)执行的操作:

HTML

<form id="myForm" method="POST" action="infoCheck.php">
  <input type="hdden" name="name" value="100">
  <input type="submit" value="Input">
</form>

请注意添加的id="myForm"

Javascript

document.addEventListener('DOMContentLoaded', function () {
  document.getElementById('myForm').submit();
});

答案 2 :(得分:0)

尝试此“在窗口加载中”提交表单。

window.onload = function(){
  document.forms['form_name'].submit();
}

您还可以使用SESSION变量来全局使用数据。

我建议您使用PDO防止SQL注入。

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com')";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

这是带有准备好的语句的PDO示例:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) 
    VALUES (:firstname, :lastname, :email)");
    $stmt->bindParam(':firstname', $firstname);
    $stmt->bindParam(':lastname', $lastname);
    $stmt->bindParam(':email', $email);

    // insert a row
    $firstname = "John";
    $lastname = "Doe";
    $email = "john@example.com";
    $stmt->execute();

    // insert another row
    $firstname = "Mary";
    $lastname = "Moe";
    $email = "mary@example.com";
    $stmt->execute();

    // insert another row
    $firstname = "Julie";
    $lastname = "Dooley";
    $email = "julie@example.com";
    $stmt->execute();

    echo "New records created successfully";
    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
$conn = null;
?>

答案 3 :(得分:-1)

根据我对您问题的理解,我将向您解释。您可以使用以下代码来代替使用Submit。

started_at

我认为它可以将“名称”传递给infoCheck.php