0

I dont know if this is possible but i want to ask if its possible to save a users input from a javascript pop up form into a php variable. For example in this code provided by w3schools the user input is displayed on the screen, instead of this how do i save it to a variable in php. The source code of the example is the following

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Prompt</h2>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var txt;
    var person = prompt("Please enter your name:", "Harry Potter");
    if (person == null || person == "") {
        txt = "User cancelled the prompt.";
    } else {
        txt = "Hello " + person + "! How are you today?";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

What i have to learn in order to be able to do that? Thanks in regards

  • 1
    what exactly you want to achieve? – Suresh Kamrushi Jun 01 '18 at 07:53
  • 3
    Possible duplicate of [How can I store JavaScript variable output into a PHP variable?](https://stackoverflow.com/questions/14662927/how-can-i-store-javascript-variable-output-into-a-php-variable) – Loek Jun 01 '18 at 07:55
  • i want to save the this javascript variable in a php variable so i can pass it in a php function – Georgio Petr Jun 01 '18 at 07:57
  • I tried the following link that you provided but it seems that it works differently if the js variavle is in a function – Georgio Petr Jun 01 '18 at 07:58

3 Answers3

0

function myFunction() {
    var txt;
    var person = prompt("Please enter your name:", "Harry Potter");
    if (person == null || person == "") {
        txt = "User cancelled the prompt.";
    } else {
        txt = "Hello " + person + "! How are you today?";
    }
    // Prepare object with data
    var data = {data: txt};
    var url = 'submit.php'
    xhttp.open('POST', url, true);
    xhttp.onreadystatechange = function() {
    if(xhttp.readyState == 4 && xhttp.status == 200) {
       console.log(xhttp.responseText);
    }
    }
    xhttp.send(JSON.stringify(data));
    document.getElementById("demo").innerHTML = txt;
}

And in your submit.php you can capture post data using following snippet

$postData = file_get_contents('php://input');
$jsonData = json_decode($postData);
$key = 'data';
$data = $jsonData->$key;

You will have data in $data variable

nerding_it
  • 704
  • 7
  • 17
0

Basicaly, you would like to push a variable from JS to PHP ? The only way to achieve this is by using http request.

Here is a link that may help you :

https://www.w3schools.com/js/js_ajax_php.asp

B.T
  • 521
  • 1
  • 7
  • 26
0

JavaScript is a client-side language and PHP is a server-side language. By using a HTTP form you can instruct HTML to send the input to a PHP file (form data).

You can also transport data between front-end and back-end by using AJAX. Using AJAX you can choose to store you data in a JSON file structure.

Learn topics in following order.

  • HTML form and command set to send to PHP.
  • PHP - How to receive form input data & handling PHP variables.
  • AJAX
  • JSON (optional).
Toolbox
  • 2,333
  • 12
  • 26