sending Ajax POST variables

Discussion in 'Coding Help' started by Ciarán Mc Cann, Sep 26, 2007.

  1. Ciarán Mc Cann

    Ciarán Mc Cann New Member

    hey every1, just one small question I have this AJAX script (which I think some1 here wrote a while back...not sure) but anyway, yea it handles $_GET, but I need it to work with $_POST, I have tryed to modify it myself but cant get it to work. any1 know how?

    PHP:
    function ewd_getcontent(urlcontainerid){
        var 
    xmlhttp_obj false;
        if (
    window.XMLHttpRequest// if Mozilla, Safari etc
            
    xmlhttp_obj = new XMLHttpRequest();
        else if (
    window.ActiveXObject){ // if IE
            
    try {
            
    xmlhttp_obj = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch (
    e){
                try{
                    
    xmlhttp_obj = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (
    e){}
            }
        }
        else
        return 
    false;
        
    document.getElementById(containerid).innerHTML="<img src='images/loading.gif' /> <b>Loading Data...</b>" ;
        
    xmlhttp_obj.onreadystatechange=function(){
            
    ewd_loadpage(xmlhttp_objcontainerid);
        }
        
    xmlhttp_obj.open('GET'urltrue);
        
    xmlhttp_obj.send(null);
    }

    function 
    ewd_loadpage(xmlhttp_objcontainerid){
        if ( 
    xmlhttp_obj.readyState == && xmlhttp_obj.status==200 )
            
    document.getElementById(containerid).innerHTML=xmlhttp_obj.responseText;
    }
     
  2. Forbairt

    Forbairt Portfolio Building

    Just having a quick glance at it ...


    Code:
    xmlhttp_obj[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]open[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]'GET'[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]url[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]true[/COLOR][COLOR=#007700]);[/COLOR]
    


    I presume changing that to POST ... don't do the trick for you ?
     
  3. Ciarán Mc Cann

    Ciarán Mc Cann New Member

    no it isnt as simple as that, I have been googling around and found solutions but I cannot find one that I can use in the script I am using.

    Thanks anyway
     
  4. Forbairt

    Forbairt Portfolio Building

    The script you're sending it to is set to handle post items as well ... ?
     
  5. Ciarán Mc Cann

    Ciarán Mc Cann New Member

    oh right hmm..ok I will try it again, thanks.

    Edit: Got to work, thanks very much
     
  6. Forbairt

    Forbairt Portfolio Building

    no problem ... its what I'm here for ... that and increasing my post count :D
     
  7. louie

    louie New Member

    that looks like one of my scripts.
    Where did you get it from?
     
  8. Ciarán Mc Cann

    Ciarán Mc Cann New Member

    yea i think it might be yours alright, thanks again
     
  9. Forbairt

    Forbairt Portfolio Building

    :)

    I'm expecting an exit stage left .. to go with that post :D
     
  10. louie

    louie New Member

    The script is available on my website.
    I put it up for someone here in the forum: Ajax Sample
     
  11. Forbairt

    Forbairt Portfolio Building

    Was just joking anyways :)

    I'm using Head Rush - Ajax and .. Ajax in Action ... as my reference material as well in case anyone is interested ... I'd strongly recommend both books mainly for beginners though with a good understanding of html
     
  12. janu

    janu New Member

    can you please post the solution that you found for POST variables, I have the following code and I also want this to work with POST, how will I use it on <input submit> as well.. thanks

     
  13. louie

    louie New Member

    the solution is on the page called by the script.

    e.g. if you page that is meant to be returned is called: receiver.php and you pass an input named name='text' then on the receiver page you do this:

    PHP:
    $x_text = !empty($_POST['text']) ? $_POST['text'] : "not post made";
    echo 
    $x_text;
     
  14. janu

    janu New Member

    Thanks for the quick reply..

    but unfortunately I cannot get it work, have you seen my script, it has GET variable somewhere on some lines, is tha tthe reason I am not receiving any post variables ? here is my form code, please take a look


    and below is my post.php

    I am receiving "not post made" in my layer..

    It would be great if you can help me here, thakns
     
  15. janu

    janu New Member

    te line below that I have changed it
    page_request.open('POST', url+bustcacheparameter, true)

    originally its GET thats working for GET variables, but I have tried both changing it to GET or POST, but it doesnt work..

    :(
     
  16. louie

    louie New Member

  17. janu

    janu New Member

    thanks, I was waiting or your reply, I downloade this page

    Ajax get content sample - Eire Web Design Ireland

    but this was not working, I am going to check the new link you send,

    thanks again and I wil tell you the status of my work..

    regards
     
  18. louie

    louie New Member

    change your form to:
    PHP:
    echo "<div name=postdiv id=postdiv></div>
    <form method=post action=./post.php>
    <input type='text' name='test' id='test' value='' onchange=\"ewd_getcontent_post(./post.php?test=',this.value, 'postdiv');\" size='100' />
    <input class=bbtn type=\"button\" onClick=\"ewd_getcontent_post(./post.php?test=',test.value, 'postdiv');\" value=\"Back to Inbox\" name=\"button\">
    </form>
    change your receiver page from $_POST to $_GET
    PHP:
    <?
    print_r($_GET);
    $x_text = !empty($_GET['test']) ? $_GET['test'] : "not post made";
    echo 
    $x_text
    ?> 
    and add this javascript to your code:
    Code:
    function ewd_getcontent_post(url,input, containerid){
     var xinput = input;
     var xmlhttp_obj = false;
     if (window.XMLHttpRequest) // if Mozilla, Safari etc
      xmlhttp_obj = new XMLHttpRequest();
     else if (window.ActiveXObject){ // if IE
      try {
      xmlhttp_obj = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e){
       try{
        xmlhttp_obj = new ActiveXObject("Microsoft.XMLHTTP");
       }
       catch (e){}
      }
     }
     else
     return false;
     document.getElementById(containerid).innerHTML="<img src='http://www.eire-webdesign.ie/images/loading.gif' /> <b>Loading Data...</b>" ;
     xmlhttp_obj.onreadystatechange=function(){
      ewd_loadpage(xmlhttp_obj, containerid);
     }
     var params = '&test=' + encodeURIComponent(xinput);
     url = url+params;
     xmlhttp_obj.open('GET', url, true);
     xmlhttp_obj.send(null);
    }
    
     
  19. janu

    janu New Member

    thanks a lot lous, the code from you page worked like a charme, I didnt know that I have to get those POST variables as GET..

    THANKS THANKS and a big THANKS...

    cheers
     
  20. louie

    louie New Member

    you don't but that's the quickest solution I could come up with to sort out your problem.
     

Share This Page