I rarely if ever use PHP so this is tough for me, but since I can't load a URL's contents in JavaScript I have to use some server-side code to do what I want to.
Here is my relevant JavaScript code:
function makeRequest (url, urlString) {
var formData = new FormData();
formData.append("urlString",urlString);
var xmlRequest = new XMLHttpRequest();
xmlRequest.open("POST", url, true);
xmlRequest.send(formData);
xmlRequest.onreadystatechange=function()
{
if (xmlRequest.readyState==4 && xmlRequest.status==200) {
document.getElementById("results").innerHTML=xmlRequest.responseText;
} else {
document.getElementById("results").innerHTML=xmlRequest.readyState;
}
}
}
function search (event) {
if( event.which === 13 || event.keyCode === 13 ) {
var searchString = "&text=" + document.getElementById("searchFlickr").value;
var URLtoUse = combineString(searchString);
alert(URLtoUse);
makeRequest("FlickrSearch.php", URLtoUse);
//prevent page refresh
return false;
}
}
And the PHP:
<?php
$URLtoUse = $_POST["urlString"];
$XML = $file_get_contents($URLtoUse);
echo $XML;
?>
And the HTML:
<form>
<label id="searchFlickrLabel" for="searchFlickr">Type your search query and hit enter!</label> <br />
<input type="text" id="searchFlickr" onkeypress="return search(event);"/>
</form>
<div id="results">
</div>
I've tried using the PHP embedded on the same page as the JavaScript, but that doesn't do anything at all. Even the most basic thing I'm attempting to do, namely prevent the form submission from refreshing the page, is failing.
What am I doing wrong?
EDIT: Update!
I've implemented all the helpful changes to at least get the code doing something. The problem now is that I don't seem to get any responseText. Odd.
If I change the PHP to return a string of "something" it does display.
EDIT2: It works! Thank you so much both of you for being so patient. The answer I'm accepting helped me the most, but both helped me a great deal, and the comments have been fantastic.