2

I have a table where it displays the ID and on click I get the ID. I then used Json and sent it to page editReport.php where i want to get to the variable to view the report I wish to edit. Can I get it to a php variable on the editReport page. I have used JSON to populate a javascript variable with PHP but cant find a way to get javascript to PHP.

   //Reports.PHP
   function myFunction() {
   $(".reportId").click(function () {

            var selectedReportId = $(this).text();

            //alert to test if gets ID from click
            alert(selectedReportId);

            selectedReportId = JSON.stringify(selectedReportId);
            localStorage.setItem('selectedReportId', selectedReportId);

             window.location = "editReport.php";
        });
  }

this here is the page the var is sent to. Any possible way to get that variable into a PHP var

   //editReports
   <script type="text/javascript">
   function myFunction() {
alert("hi");

var selectedReportId = localStorage.getItem('selectedReportId');
if (selectedReportId) selectedReportId = JSON.parse(selectedReportId);


alert(selectedReportId);


 $.post('editReport.php', {variable: selectedReportId});
 }



<?php
//make this variable populated from the javascript
$varID = $_POST['variable'];
echo "$varID";
?>
0

2 Answers 2

1

You could use a query string like so:

window.location = "editReport.php?selectedReportId="+selectedReportId

Then in php retrieve that variable using:

$selectedReportId = $_GET['selectedReportId'];

Sign up to request clarification or add additional context in comments.

Comments

0

If you don't want to use POST or GET, you could also do the following in editReport.php:

<?php
ob_start();
?>

<script type="text/javascript">
var selectedReportId = localStorage.getItem('selectedReportId');
document.write(selectedReportId);
</script>

<?php
$offset  = ob_get_clean();
$cl      =  strip_tags($offset);
$pattern = "/document\.write\('([^)]*)'\)/";
preg_match($pattern, $cl, $match);
print_r($match[1]);
?>

This should pull it into PHP via the screen output.

Hope this helps!

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.