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
https://forums.phpfreaks.com/topic/131454-please-tell-me-what-i-am-doing-wrong/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.