3 Replies - 15957 Views - Last Post: 19 August 2009 - 06:25 AM

#1 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Element by id reload?

Post icon  Posted 18 August 2009 - 01:49 PM

SUMMARY: User clicks on a link, link sends data to a script, that script tells the page to output different data.

(I have a separate script that makes xmlhttp the ajax variable to use)
AJAX
function Item(id, action) {	
	xmlhttp.onreadystatechange=function() {
	
		if (xmlhttp.readyState==4) {
			document.getElementById("inventory").reload();
		}
	
	};
	
	xmlhttp.open("GET", ".inc/command/inv.action.php?item_id="+id+"&action="+action, "TRUE");
	xmlhttp.send(null);
}


The only issue I have is returning the values from inv.action.php properly. I figured to do so would be to reload the master div around the output... This is an AJAX/PHP combo, and the output page is below...

<?php
while ($data=mysql_fetch_array($query)) {
		if ($data['is_worn']==1) {
			$data['item_qty']--;
			$types.=$data['item_type']." ";
			$items.=ucfirst($data['item_name'])."-";
			$descs.=$data['description']."^";
			$ids.=$data['item_id']." ";
			
			$ac+=$data['ac'];
			$wc+=$data['wc'];
		}
		
		if ($data['item_qty']>0) {
		$output['inv'].="
		<tr>
			<td>
				<a class=\"help\" title=\"".$data['description']."\" href=\"java script:Item('".base64_encode($data['item_id'])."', 'add');\">".ucfirst($data['item_name'])."</a>
			</td>
			<td>
				".$data['item_qty']."
			</td>
		</tr>";
		}
	}

$type=explode(" ", $types);
	$item=explode("-", $items);
	$desc=explode("^", $descs);
	$id=explode(" ", $ids);
	
	for ($x=0; $x<count($type); $x++) {
		$wear[$type[$x]]=$item[$x];
		
		$description[$type[$x]]=$desc[$x];
		
		$item_id[$type[$x]]=$id[$x];
	}
	
	$body=array('head', 'neck', 'shoulders', 'body', 'arms', 'wrists', 'hands', 'waist', 'legs', 'feet', 'lear', 'rear', 'lfing', 'rfing', 'weapon', 'shield');
	
	for ($x=0; $x<count($body); $x++) {
		if ($wear[$body[$x]]) {
			$output['item']="<a class=\"help\" title=\"".$description[$body[$x]]."\" href=\"java script:Item('".base64_encode($item_id[$body[$x]])."', 'remove');\">".$wear[$body[$x]]."</a>";
		} else {
			$output['item']="None";
		}
		
		switch ($body[$x]) {
			case "rfing":
				$output['body']="right Finger";
				break;
			
			case "lfing":
				$output['body']="left Finger";
				break;
			
			case "lear":
				$output['body']="left Ear";
				break;
			
			case "rear":
				$output['body']="right Ear";
				break;
			
			default:
				$output['body']=$body[$x];
				break;
		}
		
		if ($body[$x]=="weapon") {
			$output['wear'].="
	<tr>
		<td colspan=\"2\">
			<hr />
		</td>
	</tr>";
		}
		
		$output['wear'].="
	<tr>
		<td>
			".ucfirst($output['body']).":
		</td>
		<td id=\"".$body[$x]."\">
			".$output['item']."
		</td>
	</tr>";
		
	}

?>
<script language="javascript" type="text/javascript" src="js/inventory.js"></script>

<table id="inventory" class="body" align="center" width="90%" style="margin-top: 75px;">
	<tr>
		<td colspan="2">
			<h2>Your Inventory</h2>
		</td>
	</tr>
	<tr>
		<td>
<table width="75%" style="text-align: center;" cellspacing="0">
	<tr>
		<td>
			<h3>Items</h3>
		</td>
		<td>
			<h3>Quantity</h3>
		</td>
	</tr>
<?php echo $output['inv']; ?>
</table>

		</td>
		<td>
<table>
	<?php echo $output['wear']; ?>
	<tr>
		<td colspan="2">
			<hr />
		</td>
	</tr>
	<tr>
		<td>
			Current Armor Class:
		</td>
		<td>
			<?php echo $ac; ?>
		</td>
	</tr>
	<tr>
		<td>
			Current Weapon Class:
		</td>
		<td>
			<?php echo $wc; ?>
		</td>
	</tr>
</table>
		</td>
	</tr>
</table>
<br />


Is This A Good Question/Topic? 0
  • +

Replies To: Element by id reload?

#2 arthurakay  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 17-February 09

Re: Element by id reload?

Posted 18 August 2009 - 02:48 PM

I don't understand the problem. Does the code not work? What actually happens? You need to explain the problem in more detail, though the background info was helpful.
Was This Post Helpful? 0
  • +
  • -

#3 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Re: Element by id reload?

Posted 18 August 2009 - 03:38 PM

View Postarthurakay, on 18 Aug, 2009 - 01:48 PM, said:

I don't understand the problem. Does the code not work? What actually happens? You need to explain the problem in more detail, though the background info was helpful.


It's not a problem with the code I've written so far, more or less the appropriate code that I'd need...

For example, I know responseText won't really work? Or will it?

If not, how can I re-output data without reloading the page?
Was This Post Helpful? 0
  • +
  • -

#4 arthurakay  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 17-February 09

Re: Element by id reload?

Posted 19 August 2009 - 06:25 AM

The response.responseText value ought to be fine. I'm still a bit fuzzy on the problem, but here are my generic thoughts:

1) The client makes a request from the server
2) The server (your PHP script) echoes some content (formatted as HTML, XML, JSON or whatever you need)
3) The client recognizes the state change (ie- the server's response) and modifies your existing DOM elements

Taking ExtJS as an example, their framework utilizes the responseText attribute. Scroll down to their ComboBox example - it sounds like that might be similar to what you're doing.

View Postthe1corrupted, on 18 Aug, 2009 - 03:38 PM, said:

how can I re-output data without reloading the page?


When the client sees a response from the server, have Javascript manipulate your DOM elements. You might write some custom logic, but methods like getElementById() will be very useful. It completely depends on what data you're trying to insert into the page.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1