Hi I'm trying to pass a variable named url from javascript to a php function called parse all within the same page, Here is my following attempt but it renders nothing. I'm also trying to innerthml the function parse as well. What I'm I doing wrong? Note:the function parse is cut out completely for sake of simplicity, It actually does more not just echo a variable, it echos more html.
here is my AJAX REQUEST inside a javascript script inside a php script.
"<script type=\"text/javascript\">\n".
"\n".
"// create and return an XMLHttpRequest object\n".
"function createRequest() {\n".
" var ajaxRequest; // the xmlrequest object\n".
" try{\n".
" // Opera 8.0+, Firefox, Safari\n".
" ajaxRequest = new XMLHttpRequest();\n".
" } catch (e){\n".
" // Internet Explorer Browsers\n".
" try{\n".
" ajaxRequest = new ActiveXObject(\"Msxml2.XMLHTTP\");\n".
" } catch (e) {\n".
" try{\n".
" ajaxRequest = new ActiveXObject(\"Microsoft.XMLHTTP\");\n".
" } catch (e){\n".
" // Something went wrong\n".
" alert(\"Your browser broke!\");\n".
" return false;\n".
" }\n".
" }\n".
" }\n".
" return ajaxRequest;\n".
"} // end of createRequest\n".
Here is my actual javascript, and the variable url that I want to pass and the innerhtml of the function parse
"var spellcheck = function(data){\n".
" var found=false;var url='';\n".
" var text=data[0];\n".
" if(text!=document.getElementById('spellcheckinput').value)return;\n".
" for(i=0;i<data[1].length;i++){if(text.toLowerCase()==data[1][i].toLowerCase()){found=true;url='http://en.wikipedia.org/wiki/'+text;document.getElementById('spellcheckresult').innerHTML='<b style=\"color:green\">Correct</b> - <a target=\"_top\" href=\"'+url+'\">link</a>';}}\n".
" if(!found)\n".
" document.getElementById('spellcheckresult').innerHTML='<b style=\"color:red\">Incorrect</b>';};\n".
" var getjs= function(value){\n".
" if(!value)return;".
" var url='http://en.wikipedia.org/w/api.php?action=opensearch&search='+value+'&format=json&callback=spellcheck';". // this is the variable I want to pass
"document.getElementById('spellcheckresult').innerHTML='Checking ...';".
"var elem=document.createElement('script');".
"elem.setAttribute('src',url);".
"elem.setAttribute('type','text/javascript');".
"document.getElementsByTagName('head')[0].appendChild(elem);};\n".
"document.getElementById('resultdiv').innerHTML='" . parse() . "';\n". //here wanting to innerhtml my php function
Here is my unsuccessful ajax JavaScript variable passing to my php function down below
" var requestone = createRequest();"
"var variabletosend = url;"
"var vars = "deletenode=" + encodeURIComponent(variabletosend);"
"requestone.open("POST", "parse()", true);"
"requestone.setRequestHeader("Content-type", "application/x-www-form-urlencoded");"
"requestone.onreadystatechange = function(){"
"handleRequest(requestone);"
"}"
"requestone.send(vars);"
"</script>\n".
"\n".
"<form action=\"#\" method=\"get\" onsubmit=\"return false\">\n".
"<p>Enter a species here : <input id=\"spellcheckinput\" onkeyup=\"getjs (this.value);\" type=\"text\"> <span id=\"spellcheckresult\"></span></p>\n".
"</form>\n".
"<div id=resultdiv></div>".
"</body>\n".
"</html>\n".
"\n";
function parse($url){
print $url;
}
?>