title: Passing Variable From JavaScript to PHP without Page Refresh
body: <p>I'm writing some code that has a variable in JavaScript that must be passed into the PHP script in the same document. The user input will be used to be scraped from some external site.</p>
  
  <p>The JavaScript variable is <code>HtmlLink</code>, and it needs to be passed to the PHP code where it says <code>INSERT HTMLLINK VARIABLE HERE</code> without reloading the page.</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;HEAD&gt;
     &lt;TITLE&gt;Test Input&lt;/TITLE&gt;
     &lt;SCRIPT&gt;
        type = "text/javascript"
        function testResults (form) {
           var TestVar = form.inputbox.value + ".html";
           var HtmlLink = "www.mp3skull.com/mp3/" + TestVar;
           document.write(HtmlLink);
        }
     &lt;/SCRIPT&gt;
     &lt;?php
        $contents = file_get_contents('INSERT HTMLLINK VARIABLE HERE');
        $dom = new DOMDocument();
        libxml_use_internal_errors(true);
        $dom-&gt;loadHTML($contents);
        libxml_clear_errors();
        $xpath = new DOMXpath($dom);
  
        $element = $xpath-&gt;query('//div[@id="right_song"]/div[3]/div[1]/div[1]/a')-&gt;item(0)-                    
        echo $element;  
     ?&gt;  
  &lt;/HEAD&gt;
  &lt;BODY&gt;
     &lt;FORM NAME="myform" ACTION="" METHOD="GET"&gt; Song Name &lt;BR&gt;
        &lt;INPUT TYPE="text" NAME="inputbox" VALUE=""&gt;&lt;P&gt;
        &lt;INPUT TYPE="button" NAME="button" Value="Search" onClick="testResults(this.form)"&gt;
     &lt;/FORM&gt;
  &lt;/BODY&gt;
  &lt;/HTML&gt;
  </code></pre>