Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cannot get Ajax POST working

    I do not know where I went astray, if someone could point me in the right direction or point out where I have messed up I would be grateful.

    Code:
    <script language="JavaScript">
    <!--
    function ImageDelete() {
    if (confirm("Are you sure you want to delete?") == false) {
    return;
    }
    document.forms[0].ClearImage.value = '1';
    document.forms[0].TransCondition.value = '4';
    document.forms[0].submit();
    return;
    }
    function ClickCancel() {
    self.location = "http://68.157.251.100/CgiImageTransfer?Page=0&Language=0";
    }
    function ONStatus(){
    document.forms[0].ConditionSave.value = '1';
    document.forms[0].TaskEnable.checked = true
    {
    var xmlHttp;
    	{
    	// Firefox, Opera 8.0+, Safari
    	xmlHttp=new XMLHttpRequest();
    	}
    }
    xmlHttp.onreadystatechange=function()
    {
    if (xmlHttp.ready&state==4)
    	{
    	document.myForm.time.value=xmlHttp.responseText;
    	}
    }
    xmlHttp.open("POST","http://oneadvent.viewnetcam.com:50050/CgiImageTransfer",true);
    xmlHttp.send()
    }
    
    
    
    function OFFStatus(){
    document.forms[0].ConditionSave.value = '1';
    document.forms[0].TaskEnable.checked = false
    {
    var xmlHttp;
    	{
    	// Firefox, Opera 8.0+, Safari
    	xmlHttp=new XMLHttpRequest();
    	}
    }
    xmlHttp.onreadystatechange=function()
    {
    if (xmlHttp.ready&state==4)
    	{
    	document.myForm.time.value=xmlHttp.responseText;
    	}
    }
    xmlHttp.open('POST','http://oneadvent.viewnetcam.com:50050/CgiImageTransfer',false);
    xmlHttp.send()
    }
    function ajaxFunction()
    // -->
    </script>
    Thanks in advance!

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    the xmlhttprequest object does not send the form data automatically, you need to specify it.

    http://en.wikipedia.org/wiki/XMLHttpRequest

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So I should have:

    Code:
    var poststr = "Language=" + encodeURI( document.getElementById("Language").value ) +
                  "Page=" + encodeURI( document.getElementById("Page").value ) +
    	      "TaskId=" + encodeURI( document.getElementById("TaskId").value );
    xmlHttp.open('POST','http://XXXXXXXXXX.com:50050/CgiImageTransfer',false);
    When my codes forms look like this:

    Code:
    <form method="post" action="http://XXXXXXXX.com:50050/CgiImageTransfer">
    <input name="Language" value="0" type="hidden">
    <input name="Page" value="1" type="hidden">
    <input name="TaskId" value="2" type="hidden">
    Of course, I shortened it considerably, but I'll do the grunt work, lol

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Or you could write a function that walks the form and builds the string instead of listing all of the elements. I believe people already posted that somewhere here on the forum.

    OR

    You can use a library that has it built in with all of the correct headers being set too.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I like that walking thing if you have the code available,

    Is that the right format for it to call the HttpRequest though?

  • #6
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    if you're interested in a library as another option...

    http://www.prototypejs.org/api/form/serialize

  • #7
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Code:
    //*******************************************************
    // Builds a querystring off of the child form elements of the
    // object passed to the function.
    //*******************************************************
    function formPost(top_element){
    	var inputs=top_element.getElementsByTagName('*');
    	var qstring=new Array();
    	for(var i=0;i<inputs.length;i++){
    		if(!inputs[i].disabled&&inputs[i].getAttribute('name')!=""&&inputs[i].getAttribute('name')){
    			qs_str=inputs[i].getAttribute('name')+"="+encodeURIComponent(inputs[i].value);
    			switch(inputs[i].tagName.toLowerCase()){
    				case "select":
    					if(inputs[i].getAttribute("multiple")){
    						var len2=inputs[i].length;
    						for(var j=0;j<len2;j++){
    							if(inputs[i].options[j].selected){
    								var targ=(inputs[i].options[j].value) ? inputs[i].options[j].value : inputs[i].options[j].text;
    								qstring[qstring.length]=inputs[i].getAttribute('name')+"="+encodeURIComponent(targ);
    							}
    						}
    					}
    					else{
    						var targ=(inputs[i].options[inputs[i].selectedIndex].value) ? inputs[i].options[inputs[i].selectedIndex].value : inputs[i].options[inputs[i].selectedIndex].text
    						qstring[qstring.length]=inputs[i].getAttribute('name')+"="+encodeURIComponent(targ);
    					}
    				break;
    				case "textarea":
    					qstring[qstring.length]=qs_str;
    				break;
    				case "input":
    					switch(inputs[i].getAttribute("type").toLowerCase()){
    						case "radio":
    							if(inputs[i].checked){
    								qstring[qstring.length]=qs_str;
    							}
    						break;
    						case "checkbox":
    							if(inputs[i].value!=""){
    								if(inputs[i].checked){
    									qstring[qstring.length]=qs_str;
    								}
    							}
    							else{
    								if(inputs[i].checked){
    								    qstring[qstring.length]=inputs[i].name+"=ON";
    								}
    							}
    						break;
    						default:
    							qstring[qstring.length]=qs_str;
    						break;
    					}
    				break;
    			}
    		}
    	}
    	return qstring.join("&");
    }
    Helping to build a bigger box. - Adam Matthews

  • #8
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for that bit of code, I call it for the Post by:
    Code:
    document.forms[0].ConditionSave.value = '1';
    
    document.forms[0].TaskEnable.checked = true
    xmlhttp=new XMLHttpRequest();
    
    xmlhttp.open("Post", 68.157.251.100/CgiImageTransfer, false);
    xmlhttp.send(inputs);
    
    document.forms[0].submit();
    Thanks again for all your help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •