Jump to content

AJAX script help, making it dynamic


HaLo2FrEeEk

Recommended Posts

I have an AJAX script that works perfectly...when what you're doing doesn't require any dynamic...ness.  I want to be able to pass parameters to the script that the url being run by the AJAX can recieve.  Right now, it's set up so only one page can really make anything of it.  What I want is to be able to pass different values depending on what the page I'm running is, so I need your guys' expertise, I have one idea in mind but its not very practical, I was hoping you guys could help me out and tell me the what the best way would be to do what I'm trying to do.

 

Here is my script:

 

var http_request = false;
var obj;

function makePOSTRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      // set type accordingly to anticipated content type
      //http_request.overrideMimeType('text/xml');
      http_request.overrideMimeType('text/html');
      }
    } else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
    try {
      http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!http_request) {
    alert('Cannot create XMLHTTP instance');
    return false;
    }

  http_request.onreadystatechange = alertContents;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
  }

function loading() {
  var load;
  load = document.getElementById("loading");
  vis = load.style;
  if(vis.display == '' || vis.display == 'block') {
    vis.display = 'none';
    } else if(vis.display == 'none') {
    vis.display = 'block';
    }
  }

function alertContents() {
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      //alert(http_request.responseText);
      result = http_request.responseText;
      //document.getElementById('status').innerHTML = result;
      obj.innerHTML = result;
      loading();
      } else {
      alert('There was a problem with the request.');
      loading();
      }
    }
  }

function get(url, user_id, div_id) {
  var poststr = "user_id=" + user_id + "&url=" + escape(encodeURI(document.getElementById('url').value));
  obj = document.getElementById(div_id);
  loading();
  makePOSTRequest(url, poststr);
  }

Link to comment
https://forums.phpfreaks.com/topic/61437-ajax-script-help-making-it-dynamic/
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.