0

I am storing the result of some calculations in a HTML div element using the innerHTML attribute:

var result = document.getElementById('duration').innerHTML+
               obj1+"    "+obj+"    "+
               km.toFixed(1)+" Km     "+Date

document.getElementById('duration').innerHTML = result

Now I want to store the div's content/data in a PHP variable.

3
  • I am not sure if you can do that! Javascript, if you are not using Node.js, lives on the browser side, you need to interact with the server side, so you need to make a request to store the data somehow. Commented Feb 19, 2016 at 9:42
  • Why don't you separate view data from calculation data, submit the calculated data via ajax and then create the div needed surrounding your calculated data. Commented Feb 19, 2016 at 9:46
  • using AJAX is the better option. You need to pass all the values from calculations to the server page and there you need to echo the html part and catch that in AJAX response. Commented Feb 19, 2016 at 9:48

2 Answers 2

1

After set dic content, use jQuery to send data to PHP:

var url = 'myPHPFile.php';

$.post(url, {data: document.getElementById('duration').innerHTML});

In your PHP , get your variables from $_POST['data']:

<?php
    $data = $_POST['data'];
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Providing all you are doing is submitting data, you should be using POST. GET is for requesting data which we aren't doing. They can also be cached which might not be desirable in this case.
0

JavaScript is on client side, so you can try to send JS variable (div content) back via URL and then obtain it via PHP. JS like this:

window.location.href=”index.php?divContent=" + document.getElementById('duration').innerHTML;

And then in PHP obtain it from GET, like this:

$somevar = $_GET["divContent"];

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.