This question already has an answer here:
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.
The JavaScript variable is HtmlLink
, and it needs to be passed to the PHP code where it says INSERT HTMLLINK VARIABLE HERE
without reloading the page.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Test Input</TITLE>
<SCRIPT>
type = "text/javascript"
function testResults (form) {
var TestVar = form.inputbox.value + ".html";
var HtmlLink = "www.mp3skull.com/mp3/" + TestVar;
document.write(HtmlLink);
}
</SCRIPT>
<?php
$contents = file_get_contents('INSERT HTMLLINK VARIABLE HERE');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($contents);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$element = $xpath->query('//div[@id="right_song"]/div[3]/div[1]/div[1]/a')->item(0)-
echo $element;
?>
</HEAD>
<BODY>
<FORM NAME="myform" ACTION="" METHOD="GET"> Song Name <BR>
<INPUT TYPE="text" NAME="inputbox" VALUE=""><P>
<INPUT TYPE="button" NAME="button" Value="Search" onClick="testResults(this.form)">
</FORM>
</BODY>
</HTML>
</div>