0

I know this question was asked before. I checked most of the old threads but I still can't make it work.

I am passing a variable from a javascript in one page to another, it works fine then I want to pass the variable on this second page from the javascript to a php variable. Here is my code.

page1 :

<!DOCTYPE html>
<html>
 <head>

  <title>Tutorial</title> 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> 
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css"> 
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>
  <script>
  function openInNewTab(url) {
  var win = window.open(url, '_blank');
  win.focus();}
  </script>

 </head>

 <body>
  <br /><br />
  <div class="container" style="margin-left:150px;">
   <br />
   <h4>Select Your columns</h4>
   <br />
   <div class="col-md-4" style="margin-left:-80px;">
    <form method="post" id="multiple_select_form">
     <select name="framework" id="framework" class="form-control selectpicker"  data-live-search="true" multiple>
      <option value="Laravel">Laravel</option>
      <option value="Symfony">Symfony</option>
      <option value="Codeigniter">Codeigniter</option>
      <option value="CakePHP">CakePHP</option>
      <option value="Zend">Zend</option>
      <option value="Yii">Yii</option>
      <option value="Slim">Slim</option>
     </select>
     <br /><br />
     <input type="hidden" name="hidden_framework" id="hidden_framework" />
     <input type="submit" name="submit" class="btn btn-info" value="Submit" />
    </form>
    <br />   
   </div>
  </div>
 </body>
</html>

<script>
$(document).ready(function(){
 $('.selectpicker').selectpicker();

 $('#framework').change(function(){
  $('#hidden_framework').val($('#framework').val());
 });

  $('#multiple_select_form').on('submit', function(event){
  event.preventDefault();
  if($('#framework').val() != '')
  { 
window.localStorage.setItem('test', $('#framework').val());
openInNewTab('showD.php');
$('.selectpicker').selectpicker('val', '');
  }
  else
  {
   alert("Please select framework");
   return false;
  }
 });
});
</script>

The variable 'test' is getting the value of $('#framework').val() then I am passing this variable to the javascript of the second page

showD.php :

<script>
var someNewVariableToStoreItIn = window.localStorage.getItem('test');
alert (someNewVariableToStoreItIn);
</script>

I can see that the variable is passed correctly then i want to add a php part where a new variable will get the value of "someNewVariableToStoreItIn" so when I run

<?php   
echo $PHPvar; 
?> 

so it will show me the same value as "someNewVariableToStoreItIn". I found many solutions with ajax, I am trying from few days but I can't make it work. Any help much appreciated. Thank you.

JuniorDev
  • 433
  • 3
  • 14
  • 30
  • @Nawin Hi Nawin, i checked this thread before but unfortunately I couldn't resolve my problem. I added already that I tried several solutions on different threads here. – JuniorDev Aug 08 '17 at 12:38
  • `` – Machavity Aug 08 '17 at 12:39
  • Please tell us why you couldn't resolve your problem. Like the other thread shows: you can't directly, since JS is clientside and PHP is serverside; PHP runs _before_ the page is shown, and then the clientside kicks in. You either need to set a cookie with JS that you later read in PHP, or you initiate a request to the server (through AJAX, f.i.) to some PHP script that then does whatever you want with that information. – Tom De Roo Aug 08 '17 at 12:41
  • @TomDeRoo Thanks for your answer. I tried both but without success. My problem is I couldn't know how to write the code properly. I prefer using the Ajax solution but as I am new so I couldn't apply the examples to my code – JuniorDev Aug 08 '17 at 12:45

0 Answers0