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

4
  • 1
    what exactly you want to achieve? Commented Jun 1, 2018 at 7:53
  • 3
    Possible duplicate of How can I store JavaScript variable output into a PHP variable? Commented Jun 1, 2018 at 7:55
  • i want to save the this javascript variable in a php variable so i can pass it in a php function Commented Jun 1, 2018 at 7:57
  • I tried the following link that you provided but it seems that it works differently if the js variavle is in a function Commented Jun 1, 2018 at 7:58

3 Answers 3

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).
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

I used this source code and then i try to echo the variable data but i am not getting anything
Are you getting anything when you echo $postData
No mate , i am not getting anything from this either
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.