SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    1337
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question weird AJAX problem..

    I have a weird problem with some AJAX code that I have in one of my web apps.. Basically, I have a pop up window with a form that allows a user to change their account password:

    Code:
    		<h1>Change Password</h1>
    		<form method='post' name='UpdatePass'>
    		<br />Username:<br />
    		<input type='text' name='myusername' />
    		<br />Current Password: <br />
    		<input type='password' name= 'oldpass' />
    		<br />New Password:<br />
    		<input type='password' name='newpass1' />
    		<br />Confirm New Password:<br />
    		<input type='password' name='newpass2' /><br />
    		<input type='submit' value='Change' onclick="javascript:sendtopage('UpdatePass', 'UpdatePass', 'true');" />
    		</form>

    When the form is submitted, it sends it to a javascript function where it formats the form data, and sends it on to an ajax post text function:

    Code:
    function sendtopage(pid, formname, pscript) {
    	datatosend = storeform(formname);
    	if (!pscript) {
    /*if required response isn't javascript*/ 
    		ajaxposttext('?page=' + pid, datatosend, ajaxdesktop);
    	} else {
    /*if it is*/
    		ajaxposttext('?page=' + pid, datatosend, ajaxdesktop, pscript);
    	}
    }
    Code:
    function storeform(formname) {
    	var str = '';
    	var r;
    	var x = document.forms[formname];
    	for (var i = 0; i < x.length; i++) {
    		if ((x.elements[i].name != '') && (x.elements[i].value != '')) {
    			if (i != 0) {
    				str += '&';
    			}
    			str += x.elements[i].name + "=" + encodeURI(x.elements[i].value);
    		}
    	}
    	return str;
    }
    Code:
    function ajaxposttext(url, datasend, callbackfunc, jscript) {
      var XMLHttpRequestObject = false;
    	if (window.XMLHttpRequest) {
    	  XMLHttpRequestObject = new XMLHttpRequest();
    	} else if (window.ActiveXObject) {
    	  XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	if (XMLHttpRequestObject) {
    	  XMLHttpRequestObject.open("POST", url);
    		XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    		XMLHttpRequestObject.onreadystatechange = function(){
    
    /*problems start here!!!*/
    
    			alert("status: " + XMLHttpRequestObject.status);
    			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
    				if (!jscript) {
    					callbackfunc(XMLHttpRequestObject.responseText);
    				} else {
    					var jcontent = XMLHttpRequestObject.responseText;
    					eval(jcontent);
    				}
    				delete XMLHttpRequestObject;
    				XMLHttpRequestObject = null;
    			}
    		}
    		XMLHttpRequestObject.send(datasend);
    	}
    }
    So anyways, everything goes through OK until it reaches the above function where it checks the XMLHttpRequestObject.status. When processing the request, the embedded function runs 3 times and readystate changes from 1, 2, 4 whereas the status never reaches 200, so the responsetext is never processed. As seen above, I put an alert to see what the problem was and check the status, but firefox only throws me back the following error, and not an alert:

    Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://127.0.0.1/*****/cmsadmin/newajax.js :: anonymous :: line 85" data: no]
    Source File: http://127.0.0.1/*****/cmsadmin/newajax.js
    Line: 85

    line 85 corresponds to the alert message.


    The PHP code that AJAX is sending data to, and I need the response from (part of a switch statement):
    Code:
    			case 'UpdatePass':
    				if (array_key_exists('myusername', $_POST)) {
    					$myusername = $_POST['myusername'];
    				} else {
    					$myusername = false;
    				}
    				if (array_key_exists('oldpass', $_POST)) {
    					$oldpass = $_POST['oldpass'];
    				} else {
    					$oldpass = false;
    				}
    				if (array_key_exists('newpass1', $_POST)) {
    					$newpass1 = $_POST['newpass1'];
    				} else {
    					$newpass1 = false;
    				}
    				if (array_key_exists('newpass2', $_POST)) {
    					$newpass2 = $_POST['newpass2'];
    				} else {
    					$newpass2 = false;
    				}
    				if (($myusername) && ($oldpass) && ($newpass1) && ($newpass2)) {
    					if ($newpass1 === $newpass2) {
    						if ($loginuser->updatepass($myusername, $oldpass, $newpass1)) {
    							$extra =  "Password Successfully Updated!";
    						} else {
    							$extra = "Password Change Failed - Probably wrong login information.";
    						}
    					} else {
    						$extra = "New passwords do not match!";
    					}
    				} else {
    					$extra = "One or more fields have been left blank.";
    				}
    				echo "alert('$extra');";
    				break;
    Any ideas?

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    1337
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind, I figured it out on my own.. turns out Ajax post requests don't like it when you submit to it with a submit button.. I simply changed the form element from type='submit' to type='button' and now everything is working!


Bookmarks

Posting Permissions

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