Calling Javascript Variable Value in PHP

Calling Javascript Variable Value in PHP

Page 1 of 1

5 Replies - 15269 Views - Last Post: 09 October 2009 - 05:48 AM Rate Topic: -----

#1 Tiny Grasshopper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 53
  • Joined: 26-September 06

Calling Javascript Variable Value in PHP

Posted 09 October 2009 - 01:52 AM

Hi,

I'm having a bit of a problem with exchanging a value from javascript to php.
For some reason if I compare the javascirpt value with one created in php it doesn't match i.e the javascripts returns 1 (string type) but if if run a strcmp() on it with a php string var that also returns 1 it doesn't give me a return value of 0. Does any one know why.

Here's the code I use
javascript
var myphotoid = parseInt(getPhotoID_fromURL());

function getPhotoID_fromURL()
{
	var url = document.location.href;
	if (url.match('#') && url.split('#')[1].match('photoid='))
	{
		url = url.split('#')[1];
		if (url.match('&'))
		{
			url = url.split('&')[0];
		}
		return url.split('=')[1];
	}
}



php
$myphotoid = "<script language='javascript'>document.write(myphotoid);</script>";

$arrayphotos = (array) $photos;

foreach( $arrayphotos as $fieldName => $fieldValue )
		{	
			$id = $fieldValue->id;
print strcmp($id, $myphotoid);
			print "<br />";
if($id == $myphotoid)
				print "going to add img tag here";
			else
				print $id . " not = to " . $myphotoid . "<br />";
		}



Please help have lost almost a whole day of work because of this

P.S
The getPhotoID_fromURL() javascript function gets a #value from a query string eg. index.php?albumid=4#photoid=3

$photos is a std object class and I don't know how else to access the different value other than to cast it to an array

Is This A Good Question/Topic? 0
  • +

Replies To: Calling Javascript Variable Value in PHP

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,384
  • Joined: 10-May 07

Re: Calling Javascript Variable Value in PHP

Posted 09 October 2009 - 01:56 AM

You could always check the values of the variables :

echo "<p>Comparing ".$id." to ".$myphotoid."</p>";
print strcmp($id, $myphotoid);



Then you can see what it's comparing, & not just what it's result is.

Chances are $myphotoid is going to be empty.

$myphotoid = "<script language='javascript'>document.write(myphotoid);</script>";


It's always always always important to remember that Javascript executes on the clients browser, & PHP executes on the server. So myphotoid has no idea what document.write(myphotoid) will be, because that won't execute until it reaches the viewers browser. & just as well, once the viewers browser gets it, PHP is done executing, so it can't hand the output off.

(PHP Server) --> Internet --> ISP --> (Browser)
Was This Post Helpful? 0
  • +
  • -

#3 Tiny Grasshopper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 53
  • Joined: 26-September 06

Re: Calling Javascript Variable Value in PHP

Posted 09 October 2009 - 02:12 AM

When I add the code
echo "<p>Comparing ".$id." to ".$myphotoid."</p>";
print strcmp($id, $myphotoid);



it returns
Comparing 1 to 3
Comparing 3 to 3
Comparing 4 to 3

But I've noticed if I try to cast $myphotoid it automatically returns a value of 0

So it would seem as if the display is correct but the actual value in $myphotoid is 0.

I've read that this is usually done using cookies but I'm not redirecting away from the page as this all happens on the same page.

So if I understand correctly here I basically can't do what I'm trying to ?

This post has been edited by Tiny Grasshopper: 09 October 2009 - 02:13 AM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,384
  • Joined: 10-May 07

Re: Calling Javascript Variable Value in PHP

Posted 09 October 2009 - 02:15 AM

You can use Ajax (which I have no experience with) to act as a man-in-the middle to update your PHP values with the output from Javascript.

Or you could be lazy like me & parse everything through $_GET or $_POST variables.
Was This Post Helpful? 0
  • +
  • -

#5 Tiny Grasshopper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 53
  • Joined: 26-September 06

Re: Calling Javascript Variable Value in PHP

Posted 09 October 2009 - 02:21 AM

Ok thanks I'll give that a try.

The only problem I might have with that is that I'm using the joomla framework for this site (not my idea just inherited it)

Thanks for the help
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,885
  • Joined: 08-August 08

Re: Calling Javascript Variable Value in PHP

Posted 09 October 2009 - 05:48 AM

If you just want to pass data from php to javascript and not back to php, you could use jsonencode:
<?php
$some_complex_array = array('a' => array(1, 2, 3), 'b'=> array('Yes!', 'No','Maybe'), 'c' => array('foo'=>57, 'bar'=>123));

echo '<script type="text/javascript">
function some_function() {
'; 
echo "var my_data = ".json_encode($some_complex_array).";
 "; 
echo "alert(my_data['b'][0]);
}";
echo "some_function();
</script>";
?>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1