Ajax , php Question

Discussion in 'PHP' started by mohajer, Jan 2, 2008.

  1. #1
    I use Ajax to Update And Then show info
    language is PHP , ajax access content.php?id=$id
    to do something ,

    this line is contain id :
    url = "content.php?id=" + url;
    PHP:
    and this line
    document.getElementById("content").innerHTML=http_request.responseText;
    PHP:
    content is name of a div that will a place for show result.
    but i want to add id to ("content")
    for example ("content+$id")
    then if content.php?id=23 then we have :
    document.getElementById("content23").innerHTML=http_request.responseText;
    PHP:
    function menuRequest(url)
    {
    var arg = url;
    url = "content.php?id=" + url;
    var http_request = false;
    
    if (window.XMLHttpRequest)
    { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    
    if (http_request.overrideMimeType)
    {
    http_request.overrideMimeType('text/xml');
    // See note below about this line
    }
    } 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('Giving up :( Cannot create an XMLHTTP instance');
    return false;
    }
    
    http_request.onreadystatechange = function() { alertContents(http_request); };
    http_request.open('GET', url, true);
    http_request.send(null);
    
    }
    
    
    function alertContents(http_request)
    {
    if (http_request.readyState == 4)
    {
    if (http_request.status == 200)
    {
    document.getElementById("content").innerHTML=http_request.responseText;
    }
    else
    {
    alert('There was a problem with the request.');
    }
    }
    }
    PHP:

     
    mohajer, Jan 2, 2008 IP
  2. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #2
    It may help.I did some changes.
    function menuRequest(id)
    {
    
    url = "content.php?id=" + id;
    var http_request = false;
    
    if (window.XMLHttpRequest)
    { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    
    if (http_request.overrideMimeType)
    {
    http_request.overrideMimeType('text/xml');
    // See note below about this line
    }
    } 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('Giving up :( Cannot create an XMLHTTP instance');
    return false;
    }
    
    http_request.onreadystatechange = function() { alertContents(http_request); };
    http_request.open('GET', url, true);
    http_request.send(null);
    
    }
    
    
    function alertContents(http_request)
    {
    if (http_request.readyState == 4)
    {
    if (http_request.status == 200)
    {
    document.getElementById("content"+id).innerHTML=http_request.responseText;
    }
    else
    {
    alert('There was a problem with the request.');
    }
    }
    }
    PHP:
     
    coderbari, Jan 2, 2008 IP
  3. mohajer

    mohajer Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for reply but seems not work.
     
    mohajer, Jan 2, 2008 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    Simplify things abit, don't need curly braces if only 1 argument.
    
    function alertContents(http_request) {
    if (http_request.readyState == 4 && http_request.status == 200)
    document.getElementById("content"+id).innerHTML=http_request.responseText;
    else
    alert('There was a problem with the request.');
    }
    
    Code (markup):
     
    Kaizoku, Jan 2, 2008 IP
  5. mohajer

    mohajer Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    not work ,
    when i use manual edit it work fine for example ("content19") will work (result will show in div id=23)
    but "content"+id) not work.

    however also when i try document.getElementById(id) again not work.
     
    mohajer, Jan 3, 2008 IP
  6. mohajer

    mohajer Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    any suggestion ?
     
    mohajer, Jan 3, 2008 IP
  7. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This should work but I didn't try it. Remember, the less code there is the less chance of bugs.

    function menuRequest(id, handler){
    	var url = "content.php?id=" + id;
    	var xhr = (window.ActiveX ? new ActiveXObject("MSXML2.XMLHTTP") : new XMLHttpRequest()) || alert('Giving up :( Cannot create an XMLHTTP instance');
    
    	xhr.onreadystatechange = function() {
    		if (xhr.readyState == 4 && xhr.statusText == 'OK')
    			handler(xhr.responseText);
    		else
    			alert('There was a problem with the request.');
    	};
    	xhr.open('GET', url, true);
    	xhr.send(null);
    }
    
    menuRequest(19, function(response){
    	document.getElementById("content"+id).innerHTML=response;
    });
    PHP:
     
    MMJ, Jan 3, 2008 IP
  8. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Oops, small error, the last three lines should look like this:

    menuRequest(19, function(response){
        document.getElementById("content"+19).innerHTML=response;
    });
    PHP:
    Also on line 3 replace window.ActiveX with window.ActiveXObject.
     
    MMJ, Jan 4, 2008 IP
  9. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ugh, I don't know whats wrong with me. :eek:

    Anyways, use this instead of the above:

    function menuRequest(id, handler){
        var url = "content.php?id=" + id;
        var xhr = (window.ActiveXObject ? new ActiveXObject("MSXML2.XMLHTTP") : new XMLHttpRequest()) || alert('Giving up :( Cannot create an XMLHTTP instance');
    
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4)
    			if (xhr.statusText == 'OK')
    				handler(xhr.responseText, id);
    			else
    				alert('There was a problem with the request.');
        };
        xhr.open('GET', url, true);
        xhr.send(null);
    }
    
    menuRequest(19, function(response, id){
        document.getElementById("content"+id).innerHTML=response;
    });
    PHP:
     
    MMJ, Jan 4, 2008 IP