...

View Full Version : Unable to get ajax working



mrenigma
07-29-2010, 11:05 AM
Hi, I am attempting to load content into a DIV using ajax, as well as setting a default page onload however it is not working :confused:

Logically it should work, maybe I have missed a small detail and any help would we greatly appreciated thank you =]

The JS script looks like this:



<script language="javascript" type="text/javascript">
var linkid;
var inputID = -1;

function load()
{
document.getElementById('ajaxpage').innerHTML="../Dummy Test Site/page1.htm";
alert('Done!');
}

function getID(inputID)
{
alert(inputID)
IF (inputID ==-1)
{
alert("empty");
}
else
{
alert("inputed");
}
linkid = inputID;
}

function loadXMLDoc()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("ajaxpage").innerHTML=xmlhttp.responseText;
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET","getlink.php?q="+linkid,true);
xmlhttp.send();
alert(getlink.php?q="+id);
}
</script>


The getlink.php is a very basic php and I know that works, it is just an array of links and throws out the right one. I have been using alerts to show if the code works but i only get the first alert in getID and nothing more.

The DIV looks like this:



<div id="ajaxpage" height="500px" onload="load()">

<!-- end .ajaxpage --></div>


While the links to alter the content of ajaxpage looks like this:



<div class="navbar" align="right";>
<ul class="nav">
<li class="nav"><a id="0" href="#" OnClick="getID(this.id);" >Home</a></li> |
<li><a id="1" href="#" OnClick="getID(this.id);">Products</a></li> |
<li><a id="2" href="#" OnClick="getID(this.id);">Special Offers</a></li> |
<li><a id="3" href="#" OnClick="getID(this.id);">Web Templates</a></li> |
<li><a id="4" href="#" OnClick="getID(this.id);">Contact</a></li> |
<li><a id="5" href="#" OnClick="getID(this.id);">News</a></li> |
<li><a id="6" href="#" OnClick="getID(this.id);">About</a></li> |
<li><a id="7" href="#" OnClick="getID(this.id);">Know-How</a> </li>
</ul>
</div>


When I used an alert in this, it found this.id as well, so I guess it must be my functions which are wrong.

Thank you in advance for any help
Mr.Enigma

Rowsdower!
07-29-2010, 04:19 PM
I'm not sure if it's related to the problem, but your ID's are not compliant with HTML specs:

http://www.w3schools.com/tags/att_standard_id.asp

Also, I don't see anywhere that you actually fire the http request other than onload of the div when the page first builds. You won't get new content unless/until you run your http request function again.

I'm too lazy to build this locally right now. Do you have a test page up that we can work on? :D

mrenigma
07-29-2010, 05:46 PM
Sadly I don't have a domain up atm to let you work on..It is a local build atm :(

I had forgot I removed loadXMLDoc() from the getID :p however I just added it in at the end of getID and it still does not work.

I know my id's aren't exactly guideline but for now I couldn't be bothered to create a switch list for converting the id's to numbers for the php array to compare.

I believe I have everything right in terms of syntax...I must have one little thing missing but I cannot see it.

Rowsdower!
07-29-2010, 05:55 PM
Can you post the full page code that you are working with now?

mrenigma
07-29-2010, 07:56 PM
Sure here is the source code:




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<link href="dwmain.css" rel="stylesheet" type="text/css"><!--[if lte IE 7]>
<style>
.content { margin-right: -1px; } /* this 1px negative margin can be placed on any of the columns in this layout with the same corrective effect. */
ul.nav a { zoom: 1; } /* the zoom property gives IE the hasLayout trigger it needs to correct extra whiltespace between the links */
</style>
<![endif]-->

<script language="javascript" type="text/javascript">
var linkid;
var inputID = -1;

function load()
{
document.getElementById('ajaxpage').innerHTML="../Dummy Test Site/page1.htm";
alert('Done!');
};

function getID(inputID)
{
alert(inputID)
IF (inputID ==-1)
{
alert("empty");
}
else
{
alert("inputed");
}
linkid = inputID;
loadXMLDoc();
};

function loadXMLDoc()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("ajaxpage").innerHTML="../Dummy Test Site/page1.htm";
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET","getlink.php?q="+linkid,true);
xmlhttp.send();
alert(getlink.php?q="+linkid);
};
</script>
</head>

<body class="body">

<div class="container">
<div class="header">
<a href="../Dummy Test Site/indexdw.htm" target="_self">
<img src="../graphics/Logo/logoonly.png" alt="Internect Host logo" name="Internect Host Logo" width="auto" height="auto" id="Insert_logo" style="background: none;" /> <img src="../graphics/Logo/logosidebyside.png" alt="Internect Host" name="Internect Host" width="auto" height="auto" id="Insert_logo_title" style="background:none;" />
</a>
<div class="navbar" align="right";>
<ul class="nav">
<li class="nav"><a id="0" href="#" OnClick="alert(this.id);getID(this.id);" >Home</a></li> |
<li><a id="1" href="#" OnClick="getID(this.id)">Products</a></li> |
<li><a id="2" href="#" OnClick="getID(this.id)">Special Offers</a></li> |
<li><a id="3" href="#" OnClick="getID(this.id)">Web Templates</a></li> |
<li><a id="4" href="#" OnClick="getID(this.id)">Contact</a></li> |
<li><a id="5" href="#" OnClick="getID(this.id)">News</a></li> |
<li><a id="6" href="#" OnClick="getID(this.id)">About</a></li> |
<li><a id="7" href="#" OnClick="getID(this.id)">Know-How</a> </li>
</ul>
</div>
<!-- end .header --></div>

<div class="content">
<img src="../graphics/cornertl.png" style="left:0%; top:0%" />
<div id="ajaxpage" height="500px" onload="load()">
BLAH BLAH BLAH
<!-- end .ajaxpage --></div>
<!-- end .content --></div>
<div class="footer">
<p>This .footer contains the declaration position:relative; to give Internet Explorer 6 hasLayout for the .footer and cause it to clear correctly. If you're not required to support IE6, you may remove it.</p>
<!-- end .footer --></div>
<!-- end .container --></div>
</body>
</html>



As you can see I have been using alerts to try and find the source of the problem..

The getlink.php looks like this:




<?php
// Fill up array with names
$a[]="../Dummy Test Site/index.htm";
$a[]="../Dummy Test Site/products.htm";
$a[]="../Dummy Test Site/specialoffers.htm";
$a[]="../Dummy Test Site/webtemplates.htm";
$a[]="../Dummy Test Site/contact.htm";
$a[]="../Dummy Test Site/news.htm";
$a[]="../Dummy Test Site/about.htm";
$a[]="../Dummy Test Site/know-how.htm";


//get the q parameter from URL
$q=$_GET["q"];

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}

//output the response
echo $response;

?>



and the page1.htm which is a tester looks like this:




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<h1>Instructions</h1>
<p>Be aware that the CSS for these layouts is heavily commented. If you do most of your work in Design view, have a peek at the code to get tips on working with the CSS for the liquid layouts. You can remove these comments before you launch your site. To learn more about the techniques used in these CSS Layouts, read this article at Adobe's Developer Center - <a href="http://www.adobe.com/go/adc_css_layouts">http://www.adobe.com/go/adc_css_layouts</a>.</p>
<h2>Clearing Method</h2>
<p>Because all the columns are floated, this layout uses a clear:both declaration in the .footer rule. This clearing technique forces the .container to understand where the columns end in order to show any borders or background colors you place on the .container. If your design requires you to remove the .footer from the .container, you'll need to use a different clearing method. The most reliable will be to add a &lt;br class=&quot;clearfloat&quot; /&gt; or &lt;div class=&quot;clearfloat&quot;&gt;&lt;/div&gt; after your final floated column (but before the .container closes). This will have the same clearing effect.</p>
<h3>Logo Replacement</h3>
<p>An image placeholder was used in this layout in the .header where you'll likely want to place a logo. It is recommended that you remove the placeholder and replace it with your own linked logo. </p>
<p> Be aware that if you use the Property inspector to navigate to your logo image using the SRC field (instead of removing and replacing the placeholder), you should remove the inline background and display properties. These inline styles are only used to make the logo placeholder show up in browsers for demonstration purposes. </p>
<p>To remove the inline styles, make sure your CSS Styles panel is set to Current. Select the image, and in the Properties pane of the CSS Styles panel, right click and delete the display and background properties. (Of course, you can always go directly into the code and delete the inline styles from the image or placeholder there.)</p>
<h4>Internet Explorer Conditional Comments</h4>
<p>These liquid layouts contain an Internet Explorer Conditional Comment (IECC) to correct two issues. </p>
<ol>
<li>Browsers are inconsistent in the way they round div sizes in percent-based layouts. If the browser must render a number like 144.5px or 564.5px, they have to round it to the nearest whole number. Safari and Opera round down, Internet Explorer rounds up and Firefox rounds one column up and one down filling the container completely. These rounding issues can cause inconsistencies in some layouts. In this IECC there is a 1px negative margin to fix IE. You may move it to any of the columns (and on either the left or right) to suit your layout needs.</li>
<li>The zoom property was added to the anchor within the navigation list since, in some cases, extra white space will be rendered in IE6 and IE7. Zoom gives IE its proprietary hasLayout property to fix this issue.</li>
</ol>
<h4>Backgrounds</h4>
<p>By nature, the background color on any div will only show for the length of the content. This means if you're using a background color or border to create the look of a side column, it won't extend all the way to the footer but will stop when the content ends. If the .content div will always contain more content, you can place a border on the .content div to divide it from the column.</p></iframe>

</body>
</html>



It is just text dreamweaver made, thought id use it to test the ajax.

Hope that helps :)

hgs
07-30-2010, 07:26 AM
This might help too

http://www.hgsweb.de (http://www.hgsweb.de/pframe)

Follow the link AJAX mimics Iframes

Regards
Heinz

mrenigma
07-30-2010, 07:13 PM
Hi hgs, thank you for the help :) however I keep getting one little problem, I keep getting this error:

TypeError: xmlDoc.getElementsByTagName("pfid")[0] is undefined

I haven't changed anything other than the div name and link to page on the onclick.

I am getting confused why everything I do doesn't work lol

mrenigma
08-03-2010, 10:56 PM
I have figured it out, after abit of tweaking :) thank you for all your help :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum