title: Pass id to php using JQuery
body: <p><strong>The general problem:</strong><br>
  I want to pass an id of a div element to php using the ajax function from jQuery. The whole code is in one file so the ajax function should pass the id to the same file.</p>
  
  <p><strong>What I did:</strong><br>
   1. I took the id of the clicked element (#iamaid) and saved it<br>
   2. I tried to send it to the php code in the same file using the ajax function from jQuery<br>
   3. I tried to echo it with the post method.</p>
  
  <p><strong>What happend:</strong><br>
  The developer console gave me an output and said that the data has been sent successfully but php said it hasn't been sent and said it is an undefined index.</p>
  
  <p>I also used two different ajax functions from jQuery to pass the code but both didn't seem to work.  </p>
  
  <p>Here is my full code (it's all in one file): </p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Check POST/GET-Method to same file&lt;/title&gt;
      &lt;script src="//code.jquery.com/jquery-1.11.2.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
     &lt;h1&gt;Page to check the POST/GET-Method to same file&lt;/h1&gt;
     &lt;div id="iamaid"&gt;Click me&lt;/div&gt;
     &lt;h1&gt;The output&lt;/h1&gt;
  &lt;?php 
  if (isset($_POST["id"])) {
      echo "&lt;p&gt;The id is: " . $_POST["id"] . "&lt;/p&gt;";
  }
  else {
      echo "&lt;p&gt;No id found, edit code&lt;/p&gt;";
  };
  ?&gt;
  &lt;/body&gt;
  &lt;script type="text/javascript"&gt;
  // try it with post function
      $("#iamaid").on("click", function() {
          var the_id = $(this).attr("id");
          $.post("", {id:the_id})
      });
  // try it with ajax function 
      $("#iamaid").on("click", function() {
          var the_ajax = $(this).attr("id");
          $.ajax({
              url:"",
              method: "POST",
              data: {id:the_ajax}
          });
      });
  &lt;/script&gt;
  &lt;style&gt;
      body {
          font-family:Verdana; 
      }
      #iamaid {
          width:100px;
          height:50px;
          background:black;
          color:white;
      }
      #iamaid:hover {
          cursor:pointer;
      }
  &lt;/style&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>If any more information is needed than feel free to comment I will give you all informations you need to help me. </p>
  
  <p>I looked for hours in the internet but I couldn't find any solution.<br>
  Please help me.</p>
  
  <p><strong>EDIT</strong><br>
  I got myself an answer. It was a general misunderstanding of how php works.
  Passing an id with ajax to the same file results in the same output because the file gets called again.<br><br>
  The solution is very simple:<br>
  Just put the output code or php code in another file then pass the id with an ajax call to the file and echo the wanted html code back and then paste it to an container<br><br>
  Here an example of what I mean:<br>
  <strong>HTML CODE WITH SCRIPT</strong></p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Check POST/GET-Method to same file&lt;/title&gt;
      &lt;script src="//code.jquery.com/jquery-1.11.2.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
     &lt;h1&gt;Page to check the POST/GET-Method to same file&lt;/h1&gt;
     &lt;div id="iamaid"&gt;Click me&lt;/div&gt;
     &lt;h1&gt;The output&lt;/h1&gt;
     &lt;p id="output"&gt;&lt;/p&gt;
  &lt;/body&gt;
  &lt;script type="text/javascript"&gt;
  /* the id gets posted to the php file
     the php file echos one line and the
     echo output gets pasted into the #output container
     this also works when you echo html code you just need
     one main container to paste it in
  */
      $("#iamaid").on("click", function() {
          $.post("output.php", {id:$(this).attr("id")}, function(output) {
             $("#output").html(output);
          });
      });
  &lt;/script&gt;
  &lt;style&gt;
      body {
          font-family:Verdana; 
      }
      #iamaid {
          width:100px;
          height:50px;
          background:black;
          color:white;
      }
      #iamaid:hover {
          cursor:pointer;
      }
  &lt;/style&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><br>
  <strong>PHP CODE (output.php)</strong><br></p>
  
  <pre><code>&lt;?php
      echo "The id is: " . $_POST["id"];
  ?&gt;
  </code></pre>
  
  <p><br>
  As you can see moving the php code to another file makes it much more simple and you don't need to load the whole page again because the output container is the only object that gets new content.<br></p>
  
  <p>I hope it helped some people who got stuck with ajax and the problem to use the same page and not changing it.</p>