PHP/AJAX Probelm

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    #1

    PHP/AJAX Probelm

    Maybe this should be moved to the Javascript/AJAX forum!

    I have the below snippets of code, the problem is i can't get status to pass into the changeCaseStatu s.php page, it just gives a blank value all the time???

    show.php
    Code:
    <script type="text/javascript" language="javascript">
    	var loading_img = \'../../images/layout/loading.gif\';
    	var loading_msg = \' Loading Data...\';
    	var xmlhttp_obj = false;//create the XMLHttpRequest
    	
    	function ewd_xmlhttp()
    	{
    		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
    		{
    			xmlhttp_obj = false;
    		}
    
    		return xmlhttp_obj;
    	}   //get content via GET
    
    	function getcontent(url)
    	{
    		var xmlhttp_obj = ewd_xmlhttp();
    		document.getElementById(containerid).innerHTML = \'<img src="\' + loading_img + \'" />\' + loading_msg;
    		xmlhttp_obj.onreadystatechange=function()
    		{
    			loadpage(xmlhttp_obj);
    		}
    		xmlhttp_obj.open(\'GET\', url, true);
    		xmlhttp_obj.send(null);
    		alert(xmlHttp.responseText);
    		window.location.href = \'../../cases/'.$_REQUEST['id'].'/\';
    	}     
    
    	function loadpage(xmlhttp_obj, containerid)
    	{
    		if ( xmlhttp_obj.readyState == 4 && xmlhttp_obj.status == 200 )
    		{
    			document.getElementById(containerid).innerHTML = xmlhttp_obj.responseText;
    		}
    	}
    //]]>
    </script>
    ...
    <select name="status" onchange="getcontent(\'../../sys_config/changeCaseStatus.php?id='.$cid.'&s=\'this.value\');"">'.getCaseStatusList().'</select>
    changeCaseStatu s.php
    Code:
    changeCaseStatus($_GET['id'],$_GET['status']);
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    Your function "loadpage" takes two parameters, the AJAX object and the ID of an element, which you use inside it to print the AJAX result.

    On line #44, you call this function, but you don't pass it the second parameter, which means that your code doesn't have an element to put the AJAX response into.

    And you also do this on line #41. You try to get an element using the variable "containeri d", which I can't find anywhere.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      In addition to that, the URL only has a parameter "s" set, not "status" as expected in the PHP file.

      Comment

      • ziycon
        Contributor
        • Sep 2008
        • 384

        #4
        Ok, I'm trying this now with no luck:

        Code:
        <script type="text/javascript">
        function changeCaseStatus() {
        	var xmlHttp;
        	try {
        		// Firefox, Opera 8.0+, Safari
        		xmlHttp=new XMLHttpRequest();
        	}
        	catch (e) {
        		// Internet Explorer
        		try {
        			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        		}
        		catch (e) {
        			try {
        				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        			}
        			catch (e) {
        				alert("Your browser does not support AJAX!");
        				return false;
        			}
        		}
        	}
        	function getcontent(url)
        	{
        		xmlHttp.onreadystatechange=function() {
        			if(xmlHttp.readyState==4) {
        				xmlHttp.responseText;
        			}
        			window.location.href = \'../../cases/'.$_REQUEST['id'].'/\';
        			xmlHttp.open(\'GET\', url ,true);
        			xmlHttp.send(null);
        		}
        	}
        }
        </script>
        ...
        <select name="status" onchange="getcontent(\'../../sys_config/changeCaseStatus.php?id='.$cid.'&status=\'+this.value);">'.getCaseStatusList().'</select>
        Code:
        changeCaseStatus($_GET['id'],$_GET['status']);

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          I'm not sure what you're trying to do. When you set window.location .href, the page will change to the new location. That will mean the Ajax call will be ignored.

          Comment

          • ziycon
            Contributor
            • Sep 2008
            • 384

            #6
            Bascily when the select box has been changed that it will call the page changeCaseStatu s.php and pass the id and status vairables to it, then it will refresh back to the current page to show the change of status, does that explain it?

            Comment

            • ziycon
              Contributor
              • Sep 2008
              • 384

              #7
              The 'hi' alert is show up now and the page is refreshing but it wont retrieve the details on the changeCaseStatu s.php page??
              Code:
              <script type="text/javascript">
              function getcontent(url)
              	{
              	var xmlHttp;
              	try {
              		// Firefox, Opera 8.0+, Safari
              		xmlHttp=new XMLHttpRequest();
              	}
              	catch (e) {
              		// Internet Explorer
              		try {
              			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
              		}
              		catch (e) {
              			try {
              				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
              			}
              			catch (e) {
              				alert("Your browser does not support AJAX!");
              				return false;
              			}
              		}
              	}
              	
              	alert("hi");
              	xmlHttp.onreadystatechange=function() {
              		if(xmlHttp.readyState==4) {
              			xmlHttp.responseText;
              		}
              	}
              	xmlHttp.open(\'GET\', url ,true);
              	xmlHttp.send(null);
              	window.location.href = \'../../cases/'.$_REQUEST['id'].'/\';
              }
              </script>
              ...
              <select name="status" onchange="getcontent(\'../../sys_config/changeCaseStatus.php?id='.$cid.'&status=\'+this.value);">'.getCaseStatusList().'</select>
              changeCaseStatu s.php
              Code:
              <?php
              session_start();
              include_once('../../database.php');
              include_once('../../sql_query.php');
              
              dbConnect();
              
              changeCaseStatus($_GET['id'],$_GET['status']);
              
              closeConnect();
              ?>

              Comment

              • ziycon
                Contributor
                • Sep 2008
                • 384

                #8
                Okay, got it working fine on my local machine but when i upload it to the next it doesn't work, any ideas?

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  The whole purpose of Ajax is to avoid reloading the page. If that's not a requirement, then there's no need to use Ajax.

                  Comment

                  • ziycon
                    Contributor
                    • Sep 2008
                    • 384

                    #10
                    Yup, i know what i did wrong now! :D Thanks.

                    Comment

                    Working...