Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

send string

Status
Not open for further replies.

CompOp

Technical User
Jan 2, 2011
8
AU
Hey guys,

Thanks for reading this, I am lost once again.
I am having trouble understanding how the string is sent in the post method from ajax and how to feed it in to php correctly. I am able to get all my scripts to work but it apears that an incorrect id value is being passed to the server.

Here is the snippet where i am having trouble.

function ajaxFunction() {
var getdate = new Date();
var fpassword = document.getElementById('fpassword').value;
var fusername = document.getElementById('fusername').value;
var queryString = "fpassword=" + fpassword + "fusername=" + fusername;
if(xmlhttp) {
var txtname = document.getElementById("txtname");
xmlhttp.open("POST","checklogin.php",true); xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x- xmlhttp.send(queryString);}}

I think I am doing something wrong when I putt the querystring together.

This is apart of a login script and all the code work however no matter what username and password that are entered access is denied.
 
Hi
[ul]
[li]What is xmlhttp ?[/li]
[li]What is handleServerResponse ?[/li]
[li]How is ajaxFunction() called ?[/li]
[/ul]
Anyway, the obvious errors :
[ul]
[li]No separator between the key-value pairs ( corrected with [highlight]yellow[/highlight] )[/li]
[li]Values are not URL encoded ( corrected with [highlight pink]pink[/highlight] )[/li]
[/ul]
Code:
[b]var[/b] queryString [teal]=[/teal] [green][i]"fpassword="[/i][/green] [teal]+[/teal] [highlight pink][COLOR=darkgoldenrod]encodeURIComponent[/color][teal]([/teal][/highlight]fpassword[highlight pink][teal])[/teal][/highlight] [teal]+[/teal] [green][i]"[highlight]&[/highlight]fusername="[/i][/green] [teal]+[/teal] [highlight pink][COLOR=darkgoldenrod]encodeURIComponent[/color][teal]([/teal][/highlight]fusername[highlight pink][teal])[/teal][/highlight][teal];[/teal]
Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.


Feherke.
 
Sorry feherke,

here is the full ajax function. I will give this a try.
Code:
<script type="text/javascript">
 
function getXMLObject(){
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") 
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") 
     }
     catch (e2) {
       xmlHttp = false 
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest(); 
   }
   return xmlHttp; 
}
 
var xmlhttp = new getXMLObject();

function ajaxFunction() {
var getdate = new Date(); 
var fpassword = document.getElementById('fpassword').value;
var fusername = document.getElementById('fusername').value;
var queryString = "fpassword=" + fpassword + "fusername=" + fusername;
  if(xmlhttp) { 
  	var txtname = document.getElementById("txtname");
    xmlhttp.open("POST","checklogin.php",true); //calling testing.php using POST method
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-[URL unfurl="true"]www-form-urlencoded');[/URL]
    xmlhttp.send(queryString);
  }
}
 
function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById("BodyDiv").innerHTML=xmlhttp.responseText;
     }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}
 
Thanks feherke,

That worked like a dream, had alot of trouble trying to find out how to send that string didnt know that the post method was uriencoded. I am a Novice and didnt even know about uri encoding. Thank you!!!. I guese this is why I should learn DOM. Will study some DOM in future I guese.
 
Hi

CompOp said:
didnt know that the post method was uriencoded.
More exactly the transferred data is encoded. You said it yourself : [tt]xmlhttp[teal].[/teal]setRequestHeader[teal]([/teal][green]'Content-Type'[/green][teal],[/teal] [green]'application/x-www-form-[highlight]urlencoded[/highlight]'[/green][teal]);[/teal][/tt] ;-).
CompOp said:
I am a Novice and didnt even know about uri encoding. [gray](...)[/gray] I guese this is why I should learn DOM.
Wrong. URL encoding is related to HTTP, not the DOM.


Feherke.
 
im so noob.

All makes sence now, kinda through the code together from a few different places. I understood the get method but had alot of trouble finding good explanations on the post ajax method.

Thanks feherke, This has opend up alot of doors for me now, Iv still got alot to learn but its good to know that there are people willing to help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top