0

I wanted to ask how can i get the values of the Javascript Input and store it into a php value so i can post this data into Sqlite3. Im receiving user inputs from the Javascript Prompts. Is there another way to accomplish this also. Any help would be greatly appreciated.

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();
           }
        });
}
Ross
  • 15
  • 2
  • 1
    Possible duplicate of [How to pass data from Javascript to PHP and vice versa?](https://stackoverflow.com/questions/406316/how-to-pass-data-from-javascript-to-php-and-vice-versa) – Nick Parsons Feb 07 '19 at 01:04
  • The reference doesnt explain passing inputs. – Ross Feb 07 '19 at 01:07
  • Well, it sort of does. It shows how to pass any data. So, just add your inputs into the form of `params` that they use in their example (`"code=" +code +"&email=" +email` would be your input to the `callPHP` method). Then in your PHP file that you specified in the `url` variable you can use `$_POST["code"]` to get the code and `$_POST["email"]` to get the email – Nick Parsons Feb 07 '19 at 01:12
  • could you please provide an example of the code i would need to write. would this be a separate function – Ross Feb 07 '19 at 01:15
  • can you use jQuery? it would make it more straight forward – Nick Parsons Feb 07 '19 at 01:17
  • yes i just needed an example as reference. – Ross Feb 07 '19 at 01:18

2 Answers2

1

Using jquery you can use the $.post method:

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...
}

Then, in your PHP file (that you specified as the 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
  }
?>
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

Use a jQuery post request to send the variable from javascript to php.

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

Look at this website for more information: https://api.jquery.com/jquery.post/

Aniket G
  • 3,471
  • 1
  • 13
  • 39