Jump to content

Ajax is not being co-operative


Recommended Posts

I am trying to load the source of a page into a text area.(I'm making an online file editor) I have searched all over the web using about 3+ different methods and still unable to get this to work. I need to have a function like so

function page2Load(pageName){...}

that gets called when clicking on a page name I have listed on the page. (I have that part down pack). From there i need to use Ajax to load the SOURCE of a local phpfile, as if i were to load the page into a browser and right-click and select view source. Below is some of what I have tried

$.get('processPage.php?fileName='+pageName, function(data){	 document.getElementById('htmlContent').innerText = data;});...var xmlhttp;	 if (window.XMLHttpRequest) {		  xmlhttp=new XMLHttpRequest();	 } else {		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");	 }	 xmlhttp.onreadystatechange=function(){		  if (xmlhttp.readyState==4 && xmlhttp.status==200){			   document.getElementById('htmlContent').innerText = xmlhttp.responseText;		  }else{			   alert('ERROR: ' + xmlhttp.status + ' -> ' + xmlhttp.statusText);		  }}xmlhttp.open("GET", "processPage.php?fileName="+pageName, true);xmlhttp.send();...$('<iframe id="tempFrame" />').attr('src', 'processPage.php?fileName='+pageName);fileSource = $('#tempFrame').attr('src');$('#htmlContent').val(fileSource);$('#tempFrame').remove(); 

None of those worked. The only one that kind of came close was the second one, but it kept saying it could not find the file alerting error 404. but the files are there. Any help on this matter is greatly appriciated EDIT: Forgot to add I'm calling JQuery like so and it is located above all the above mentioned code on my page.

<script src="http://code.jquery.com/jquery-latest.min.js" ></script>

Edited by dzhax
Link to comment
Share on other sites

While waiting for some help I started poking around other posts and found someone trying something remotely similar to what I am trying to acheive http://w3schools.invisionzone.com/index.php?showtopic=42311 So I messed around with the code provided to them and tweaked it for my needs and I sort of got it working.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script><script type="text/javascript">	 function page2Load(pageName){		  document.getElementById('currentFileName').innerHTML = 'File Name: '+pageName;		  output = document.getElementById("htmlEditorForm");		  var url = "processPage.php";		  $.post(url,{ fileName: pageName }  ,  function(data) {			   output.innerHTML = '<textarea id="htmlContent" name="content" cols="100" rows="15" >'+data+'</textarea>';		  });	 }  </script> ... <div id="currentFileName" style="text-align: left;">	 File Name: <?= $defaultPage ?></div><form id="htmlEditorForm">	 <textarea id="htmlContent" name="content" cols="100" rows="15" ><?= file_get_contents('../../../lib/pages/'.$defaultPage) ?></textarea></form>

The only problem I am having now is the code will run and update in IE9 just fine. But open it in Firefox 11 and the only thing that runs updating the FIle Name listed above the textbox. Is the code deprecated or not supported in firefox? If so what do i need to change to make it cross-browser friendly.

Link to comment
Share on other sites

function GetPage(url){if(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}else{xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');}xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4&&xmlhttp.status==200) {document.getElementById('htmlContent').innerHTML=xmlhttp.responseText;}}xmlhttp.open('GET',url+'processPage.php?fileName='+url,true);xmlhttp.send();}this ajax works. I hope processPage.php looks like file_get_contents('../../../lib/pages/'+$_GET['fileName']); output file <input id="currentFileName" type="text" value="index.php" /><button value="load page" onclick="GetPage(document.getElementById('currentFileName').innerHTML);" /><textarea id="htmlContent" name="content" cols="100" rows="15" ></textarea>

Edited by es131245
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...