Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-01-2009, 04:09 AM   PM User | #1
masipay
New Coder

 
Join Date: Jan 2009
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
masipay is an unknown quantity at this point
After makeHttpRequest how to run update javascript vars?

By making httpRequest to php script, I am able to print the output to target container. But, i need to update javascript variable after this which i am not alble to do, any suggestion?

<script..>
var rate = 3.3;
</script.

<div id="outputDiv">

<!-- httpResponseText -->
<div onMouseover="myfunc();" onclick="getNewText to replacealllthis();"> this is output by ajax http request</div>
<!-- -->

</div>

function myfunc()
{
alert(rate); // rate was initially 3.3

//after onclick event the value of rate has changed to say 4.4
// but onMouseover event will still alert 3.3 so how to update this rate var
// 4.4 ??

}

simple solution is to create output text as
<div onMouseover="myfunc(4.4);" onclick="getNewText to replacealllthis();"> this is output by ajax http request</div>

->> and myfunc alert the val
However I don't want to pass param to myfunc() but use single var rate to do this event.

1. Can I ie is there a way to receive a return value from php script as well as the responseText?

2. I tried outputting
<script type="...">
var rate = 4.4;
</script> ... and rest of response text
but all the response text willl simply be printed in the output div and the updated var rate is not run . so is there a way to make this assignment of new value to rate run?

thanks
masipay is offline   Reply With Quote
Old 03-01-2009, 10:54 PM   PM User | #2
A1ien51
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
A1ien51 will become famous soon enough
JavaScript does not execute script tags with innerHTML, you need to eval() it. Most JavaScript libraries will do this for you.

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 03-08-2009, 01:08 AM   PM User | #3
masipay
New Coder

 
Join Date: Jan 2009
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
masipay is an unknown quantity at this point
Quote:
Originally Posted by A1ien51 View Post
JavaScript does not execute script tags with innerHTML, you need to eval() it. Most JavaScript libraries will do this for you.

Eric

Well Alien if you could be little more specific on how to use eval , would be much more help.

You see the initially javascript var rating = 10 was printed

after making http ajax request this value might have been changed, lets just say I need to change this var to 20,
so if i print the var again like var rating = 20 from output handler function, how do I run this script?

I am forced to use a function update(20) after getting the httpReq out put through javascript function which i want to avoid.
Thanks.
masipay is offline   Reply With Quote
Old 03-08-2009, 09:23 AM   PM User | #4
itsallkizza
Senior Coder

 
Join Date: Oct 2008
Location: Long Beach
Posts: 1,196
Thanks: 36
Thanked 164 Times in 164 Posts
itsallkizza will become famous soon enough
If I understand you correctly you want something like this concept:

example: http://buildyourownbagel.com/test/test.html

test.html:
Code:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
<style type="text/css">
</style>
<script type="text/javascript">
// <![CDATA[

function XMLHTTP_Util()
	{
	this.sendRequest = function(url,method,params,async,callback,callbackargs)
		{
		method = method ? method.toUpperCase() : "GET";
		if (typeof(callback) == "string") callback = new Function(callback);
		callback = callback || function(){};
		params = params ? params.join("&") : "";
		if (method == "GET" && params) url += "?"+params;
		var req = this.createXMLHTTPObject();
		if (!req) callback.call(this,false,callbackargs);
		req.ref = this;
		req.open(method,url,async);
		req.setRequestHeader('User-Agent','XMLHTTP/1.0');
		if (method == "POST")
			{
			req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			req.setRequestHeader("Content-length",params.length);
			req.setRequestHeader("Connection","close");
			}
		req.onreadystatechange = function()
			{
			if (this.readyState != 4) return;
			if (this.status != 200 && this.status != 304)
				{
				//alert("HTTP error " + req.status);
				callback.call(this.ref,false,callbackargs);
				}
			else
				{
				callback.call(this.ref,req,callbackargs);
				}
			}
		if (req.readyState == 4) return;
		req.send(method=="GET"?null:params);
		}
	this.createXMLHTTPObject = function()
		{
		var xmlhttp = false;
		for (var i=0;i<this.XMLHttpFactories.length;i++)
			{
			try
				{
				xmlhttp = this.XMLHttpFactories[i]();
				}
			catch(e)
				{
				continue;
				}
			break;
			}
		return xmlhttp;
		}
	this.XMLHttpFactories = [
	function() {return new XMLHttpRequest()},
	function() {return new ActiveXObject("Msxml2.XMLHTTP")},
	function() {return new ActiveXObject("Msxml3.XMLHTTP")},
	function() {return new ActiveXObject("Microsoft.XMLHTTP")}
	];
	}
var xhu = new XMLHTTP_Util();

var rate = 10;

function calcRate(form_element)
	{
	xhu.sendRequest("get_rate.php","get",["rate_input="+form_element.rate_input.value],true,changeRateAndDisplay);
	return false;
	}

function changeRateAndDisplay(req)
	{
	rate = req.responseText;
	displayRate();
	}

function displayRate()
	{
	document.getElementById("output").innerHTML = "rate = "+rate;
	}

window.onload = function()
	{
	displayRate();
	}

// ]]>
</script>
</head>
<body>

<form onsubmit="return calcRate(this)">
	<input type="text" name="rate_input" value="32" />
	<input type="submit" value="send xmlhttprequest" />
</form>

<br />
<div id="output"></div>

</body>
</html>
get_rate.php:
Code:
<?php
$i = (int)$_GET["rate_input"];
echo($i*0.26);
?>
__________________
Feel free to e-mail me if I forget to respond ;)
ohsosexybrit@gmail.com
itsallkizza is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:33 PM.


Advertisement
Log in to turn off these ads.