将Javascript输入值传递到PHP文件以发布到SQlite

时间:2019-02-07 01:01:48

标签: javascript php

我想问一下如何获取Javascript Input的值并将其存储到php值中,以便可以将此数据发布到Sqlite3中。我从Javascript提示接收用户输入。还有另一种方法可以做到这一点。任何帮助将不胜感激。

function myFunc(){
    var code = prompt("Please enter authorized code twice for security purposes: ");
    var email = prompt("Please enter email twice to continue: ");

    if(code==""||code==null||code!="1234"){
         //Handle Error
         window.location.href="error.html";
    }
}

document.onreadystatechange = () => {
    document.addEventListener('readystatechange', event => {
        if (event.target.readyState === "complete") {
               myFunc();
           }
        });
}

2 个答案:

答案 0 :(得分:1)

使用jquery可以使用$.post方法:

function myFunc() {
  var code = prompt("Please enter authorized code twice for security purposes: ");
  var email = prompt("Please enter email twice to continue: ");

  var url = "phpToGetInputs.php";
  var data = {
    code: code,
    email: email
  }

  $.post(url, data); // "send the data to the php file specified in url"

  // code...
}

document.onreadystatechange = () => {
  // code...
}

然后,在您的PHP文件(您指定为url)中

phpToGetInputs.php

<?php
  if(isset($_POST['email'])) {
    $email = $_POST['email']; // get the email input (posted in data variable)
    $code = $_POST['code']; // get the code input (posted in data variable)

    // do code that requires email and code inputs
  }
?>

答案 1 :(得分:0)

使用jQuery发布请求将变量从javascript发送到php。

$.post([url], { "data" : text });

有关更多信息,请访问此网站:https://api.jquery.com/jquery.post/