title: Passing a page number from one PHP file to another using a JavaScript function
body: <p>I'm working on a website with the following URL structure:</p>
  
  <ul>
  <li><a href="http://www.mywebsite.com/" rel="nofollow">http://www.mywebsite.com/</a> is the default home page (same as /index.php?page=1)</li>
  <li><a href="http://www.mywebsite.com/index.php?page=2" rel="nofollow">http://www.mywebsite.com/index.php?page=2</a></li>
  <li><a href="http://www.mywebsite.com/index.php?page=3" rel="nofollow">http://www.mywebsite.com/index.php?page=3</a></li>
  <li>and so on...</li>
  </ul>
  
  <p>I'm trying to pass the current page number from <code>index.php</code> to <code>myphpscript.php</code> using a javascript function:</p>
  
  <pre><code>&lt;script&gt;
  function swapContent(filterchoice){
      var PageNum = &lt;?php echo json_encode($_GET["page"]); ?&gt;;
      var url = "myphpscript.php";
      $.post(url, {contentVar: filterchoice, page : PageNum}, function(data){
          $("#myDiv").html(data).show();
      });
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>On <code>myphpscript.php</code>, when adding the following condition for the <a href="http://www.mywebsite.com/" rel="nofollow">http://www.mywebsite.com/</a> case to get <code>$page_number=1</code>:</p>
  
  <pre><code>if (isset($_POST['page'])) {
      $page_number = $_POST["page"];
  }
  else {
      $page_number = 1;
  }
  echo $page_number;
  </code></pre>
  
  <p>It doesn't seem to <code>echo</code> anything for the default home page but echo the proper page number for all the other page with <code>page=</code> in the URL. However, the <code>isset</code> condition seems to work properly on <code>index.php</code> even for the default home page with:</p>
  
  <pre><code>if (isset($_GET["page"])) {
      $page_number = $_GET["page"];
  }
  else {
      $page_number = 1;
  }
  </code></pre>
  
  <p>Could you explain me why it works for the second case and not the first one and how I can set <code>$page_number=1</code> in <code>myphpscript.php</code> if I'm on the default home page.</p>