Jump to content

Instead of loading into a div my function is linking to the site


tinman486

Recommended Posts

I am trying to load an external file into a div on my site. The file is on my server.

 

My function

 

function ahah(url, target) {
  document.getElementById(target).innerHTML = ' Fetching data...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target);};
    req.open("GET", url, true);
    req.send("");
  }
}  

function ahahDone(url, target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(reportspace).innerHTML = req.responseText;
    } else {
      document.getElementById(reportspace).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;
    }
  }
}

function load(name, div) {
ahah(name,div);
return false;
}

 

should grab the url from the link on click and put it into the Div at the bottom of this page

 

<html>
<head>
<Meta author="Justin England">
<script src="ajax.js"></script>

</head>
<body>
<form> 
Select A Report:
<a href="page.html" onclick="load('file1.html','content');return false;">File 1</a>
</select>
</form><p>
<div id="reportspace"><b>Report info will be listed here.</b></div>
</p></body>
</html>

 

however it simply links to the page I have a feeling my code isn't being executed which to be honest is the only real reason for this issue. Any suggestions on changes? What am i missing? Also If anyone could tell me how to do this same operation but with a drop down menu where the div displays pages based on the choices selected from the menu if that is even possible.

Link to comment
Share on other sites

HTML/Javascript

 

<script type="text/javascript">
function getfile() {
var req;
req = new XMLHTTPRequest();
req.onreadystatechange = function() {
	if (req.readyState==4) {
		document.getElementById('div').innerHTML=req.responseText;
	}
}
req.open("GET","file.php?file="+document.getElementById('file').value,true);
req.send(null);
}
</script>
File: <input type="text" id="file" /><br />
<div id="div"></div>

 

PHP

<?php
header( 'Cache-Control: no-cache, must-revalidate' );
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
print nl2br( htmlentities( file_get_contents( $_GET['file'] ), ENT_QUOTES ) );
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.