Jump to content

Please tell me what I am doing wrong


interpim

Recommended Posts

If you can't tell from the code below, i am a beginner at Ajax... can someone please explain to me what I am doing wrong.  I've followed a couple of tuts but can't get anything back.

 

I would be happy with getting anything back from the backend php file LOL.

I can adjust the code to give me what I need, but basically all i need is to click a button or link to increment a number and return a url based on that number.

 

this is my html page

<SCRIPT Language="JavaScript">
function getHTTPObject() {
    var xmlhttp;
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}
var http = getHTTPObject();


function ajaxFunction(somevar){
   http.open('GET', 'ajax.php?pic='+pic, true);
   http.send(null);
   http.onreadystatechange=function(){
      if(http.readyState==4){
         document.getElementById('somediv').innerHTML = http.responseText; // this will put the returned code from ajax.php into a div with id "somediv"
      }
   }
}
</script>
<div name='somediv'>

</div>
<p>
<p><a href="javascript: ajaxFunction('somediv','pic')">Click to Test</a></p>
</p>

 

Here is ajax.php

<?php
$pic = $_GET['pic'];
if($pic == ''){
$pic = 1;
}
$pic = $pic + 1;
echo "<img src=" . $pic . ">";
?>

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.