SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    Salem, OR
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Easy example of OOP issue with SAJAX & PHP

    This is actually an evolution of an earlier post that I will be deleting. This example though is much simplier and exhibits the same strange behavour.

    I have a php page

    Code:
    <? require_once('/Component.class.php'); ?>
    <? require_once('/sajax-0.12/php/Sajax.php'); ?>
    <? echo("<?xml version=\"1.0\" encoding=\"utf-8\""."?>\n") ?>
    <?
    	$thisComponent = new Component;
    	//
    	function setId($id){
    		global $thisComponent;
    		$thisComponent->setId($id);
    		return $thisComponent->getId();
    	}
    	//
    	function getId(){
    		global $thisComponent;
    		return $thisComponent->getId();
    	}
    	//
    	$sajax_request_type = "GET";
      	sajax_init();
      	sajax_export("setId");
    	sajax_export("getId");
    	 sajax_handle_client_request();
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    	<head>
    		<title></title>
    		<meta http-equiv="content-type" content="text/html;charset=utf-8" />
    		<meta http-equiv="Content-Style-Type" content="text/css" />
    		<script language="JavaScript1.2" type="text/javascript">
    		<?
    			sajax_show_javascript();
    		?>
    		
    		function handleSetIdRequest(id){
    			x_setId(parseInt(id),handleSetIdResponse);
    		}
    		function handleGetIdRequest(){
    			x_getId(handleGetIdResponse);
    		}
    		function handleGetIdResponse(val){
    			alert("The Id is: "+val);
    		}
    		function handleSetIdResponse(val){
    			alert("The Id is: "+val);
    		}
    		</script>
    	</head>
    	<body>
    		<form action="" method="post">
    			<input id="test" name="test" type="text" size="1" maxlength="1" />
    			<input name="" type="button" value="set" onclick="handleSetIdRequest(parseInt(document.getElementById('test').value));" />
    			<input name="" type="button" value="get" onclick="handleGetIdRequest();" />
    		</form>
    	</body>
    </html>
    Please note the following special areas, the includes are correct

    Code:
    <? require_once('/Component.class.php'); ?>
    <? require_once('/sajax-0.12/php/Sajax.php'); ?>
    The PHP processing code, especially the $thisComponent ref

    Code:
    <?
    	$thisComponent = new Component;
    	//
    	function setId($id){
    		global $thisComponent;
    		$thisComponent->setId($id);
    		return $thisComponent->getId();
    	}
    	//
    	function getId(){
    		global $thisComponent;
    		return $thisComponent->getId();
    	}
    	//
    	$sajax_request_type = "GET";
      	sajax_init();
      	sajax_export("setId");
    	sajax_export("getId");
    	 sajax_handle_client_request();
    ?>
    The neccessary JavaScript and response pop-ups

    Code:
    <script language="JavaScript1.2" type="text/javascript">
    		<?
    			sajax_show_javascript();
    		?>
    		
    		function handleSetIdRequest(id){
    			x_setId(parseInt(id),handleSetIdResponse);
    		}
    		function handleGetIdRequest(){
    			x_getId(handleGetIdResponse);
    		}
    		function handleGetIdResponse(val){
    			alert("The Id is: "+val);
    		}
    		function handleSetIdResponse(val){
    			alert("The Id is: "+val);
    		}
    		</script>
    The form which takes the user input

    Code:
    <form action="" method="post">
    			<input id="test" name="test" type="text" size="1" maxlength="1" />
    			<input name="" type="button" value="set" onclick="handleSetIdRequest(parseInt(document.getElementById('test').value));" />
    			<input name="" type="button" value="get" onclick="handleGetIdRequest();" />
    		</form>
    Simple enough so far.

    The Component.class.php file looks like this

    Code:
    <?
    	class Component {
    		var $id;
    		function Component(){
    			$this->id = -1;
    		}
    		function getId(){
    			return $this->id;
    		}
    		function setId($id){
    			$this->id = $id;
    		}
    	}
    ?>
    The strange behaviour is this. If you type in a number, say 8 into the text field and click set. It sends the info to the $thisComponent object, sets the id to 8, calls getId in that object and returns the value. The alert in the JavaScript handler pops-up with 8. If you then click get immediately afterwards, the pop-up appears with the default -1.

    Why or why or why?
    Last edited by affordablemagic; Feb 23, 2007 at 22:52. Reason: The example now is less complex


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
  •