sending Ajax POST variables

Advert test
#1
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(url, containerid){
    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_obj, containerid);
    }
    xmlhttp_obj.open('GET', url, true);
    xmlhttp_obj.send(null);
}

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

Forbairt

Portfolio Building
#2
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 ?
 

Forbairt

Portfolio Building
#11
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
 

janu

New Member
#12
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

<script type=\"text/javascript\">

function ajax_page(url, containerid){

var bustcachevar=1
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari or any other
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if internet explorer
try {
page_request = new ActiveXObject(\"Msxml2.XMLHTTP\")
}
catch (e){
try{
page_request = new ActiveXObject(\"Microsoft.XMLHTTP\")
}
catch (e){}
}
}
else
return false

page_request.onreadystatechange=function(){
load_page(page_request, containerid)
}

if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf(\"?\")!=-1)? \"&\"+new Date().getTime() : \"?\"+new Date().getTime()

page_request.open('POST', url+bustcacheparameter, true)
page_request.send(null)


}

function load_page(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf(\"http\")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}
</script>
 
#13
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;
 

janu

New Member
#14
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


echo "<div name=postdiv id=postdiv></div>
<form method=post action=./post.php>
<input type=text name=text>
<input class=bbtn type=\"button\" onClick=\"ajax_page('./post.php', 'postdiv');\" value=\"Back to Inbox\" name=\"button\">
</form>

";
and below is my post.php

<?
print_r($_POST);
$x_text = !empty($_POST['text']) ? $_POST['text'] : "not post made";
echo $x_text;
?>
I am receiving "not post made" in my layer..

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

janu

New Member
#15
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..

:(
 
#18
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);
}
 

janu

New Member
#19
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
 
Top