PDA

View Full Version : Merging php code into js



Burgin
05-24-2012, 02:41 PM
I have this script that works great...



<script language="javascript">
$("#div_weather").putWeather({
location:"London, UK"
});
</script>


It's within a php webpage on an ELGG based website and I want to change the location of the displayed weather forecast to that of the site-users "location". I substituted London, UK with...


<?php echo $User->location;?> but it didn't work. Any help would be fab.

traq
05-24-2012, 04:14 PM
..."it didn't work" is unhelpful. Please be more specific.

Did you receive any php and/or javascript errors?
What did the output javascript look like?
Are you sure $User->location exists, and holds the data you want?
Are you sure $User->location gives the user's location in the proper format for your script?

p.s.,
your thread title, Merging php code into js, might indicate a misunderstanding about how php works. to clear up any possible confusion:
you cannot use php code "with" ("inside", "merged with", etc.) javascript. same goes for html, xml, anything.
PHP writes text.
It might look like the two languages are inter-mixed, but they're not:
PHP happens first.
It has no idea that the text it outputs will later be parsed by a browser as html/js/whatever.
PHP does not interact with these other languages, either: once output is sent, it's gone.

Burgin
05-24-2012, 05:45 PM
Sorry, nooby with php & js.

Not sure about any errors, none reported by IE or Chrome

Here's the output code...

<script language="javascript">
$("#div_weather").putWeather({
location:""
});
</script>

To prove that $User->location exists I put this code...

<?php

$location = elgg_get_logged_in_user_entity()->location;

echo '<div id="location">'.sprintf($location).'</div>';

?>
on this page... http://community.thealbertinn.com/smartlocation and get "Moscow, Russia" which if entered into the script as text makes the script work OK

traq
05-24-2012, 06:29 PM
Not sure about any errors, none reported by IE or Chrome
Have you checked the console directly for javascript errors?
Also, PHP errors might be hidden by various settings. Try adding
error_reporting( -1 );towards the top of your php script.

Here's the output code...

<script language="javascript">
$("#div_weather").putWeather({
location:""
});
</script>
This would seem to indicate that $User->location does not exist (or, is empty).

To prove that $User->location exists I put this code...

<?php

$location = elgg_get_logged_in_user_entity()->location;

echo '<div id="location">'.sprintf($location).'</div>';

?> That code does not seem to involve $User->location in any way
(unless elgg_get_logged_in_user_entity() is supposed to return the $User object, which is unclear).


edit: I checked the link you gave [http://community.thealbertinn.com/smartlocation], and all that exists there is <div id="location"></div>,
which would also seem to indicate that your $location variable is empty.
also, I'm not sure why you are using sprintf() here...?



What do you get if you put this on your page?
<?php
exit( var_dump( $User->location ) );

Burgin
05-25-2012, 07:45 AM
Sorry, I'd forgot, you must be logged in to see that page. This is what you get if logged in...
<div id="location">Moscow, Russia</div>

If I place

<?php
exit( var_dump( $User->location ) );
?>
at the bottom of the file I get...
No location given
NULL

If I place it at the top of the file I just get NULL

djr33
05-25-2012, 08:22 AM
You should approach this as two separate problems:

1. What do you want the final HTML (and Javascript) to be, AFTER the page is generated by PHP? Don't use any PHP and write up a demo version that works.

2. How do you generate THAT HTML/JS using PHP to insert any dynamic information? It looks like it should just be a matter of using "echo" in the right place after you already have all of the Javascript and HTML properly written.

Which of those steps have you completed, and which isn't working?

Burgin
05-25-2012, 10:18 AM
I'm not a coder. I'm trying to develop an ELGG based social network site for my village. I know very little about php but I'm slowly picking it up, even less javascript. I buy scripts and try to incorporate them into the site. I get lots of help and encouragement from the ELGG community (very similar to here) but some of the coders on there are way over my head and I often get PM'd with offers of commercially written solutions for which I have no budget. This index.php file...


<?php

?>
<html>
<head>
<title>Weather Widget Example 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/jquery.weather.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/weather_output.css" type="text/css" />
</head>

<style type="text/css">
body{
margin:4px;
}
.description{
font-size:10px;
}

</style>

<body>

<div id="div_weather">
Loading weather...
</div>

<script language="javascript">
$("#div_weather").putWeather({
location:"Barlborough, Derbyshire, UK"
});
</script>


</body>
</html>

and a few associated files produce a weather forecast via Google that is just what I want for my village but I'd like it to show a forecast for users outside of my village. The "location" field is part of each users profile so I'm attempting to use that to determine the resulting weather data.

The php lines I have used are from other examples I've seen and I'm guessing that they might work but guessing wrong.

djr33
05-25-2012, 11:12 AM
Start with the Javascript, as you have it there. Is that working? If it's not doing that well anyway, then the PHP will be irrelevant. PHP just created automatic HTML and JS (and CSS) code, nothing more. So it won't help you if you can't do this manually. Does that make sense?

Burgin
05-25-2012, 01:45 PM
The js...


<script language="javascript">
$("#div_weather").putWeather({
location:"Barlborough, Derbyshire, UK"
});
</script>

works spot on.
As soon as I add the php (that I'm told should work if user is defined) thus...


<?php $user = elgg_get_page_owner_entity();?>
<script language="javascript">
$("#div_weather").putWeather({
location:"<?php echo $user->location;?>"
});
</script>

I get...
Loading weather... displayed on screen and if I check View Source it gives me this code...



<html>
<head>
<title>Weather Widget Example 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/jquery.weather.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/weather_output.css" type="text/css" />
</head>

<style type="text/css">
body{
margin:4px;
}
.description{
font-size:10px;
}

</style>

<body>

<div id="div_weather">
Loading weather...
</div>

No js present or the closing </body> & </html> tags.

traq
05-25-2012, 01:47 PM
If the var_dump() code is printing NULL, that means your $User->location variable is empty (probably undefined).

If elgg_get_logged_in_user_entity()->location prints what you want, try using that. Otherwise, tis might be a better question for the elgg community, since it's elgg-specific functions you're having trouble with. (I've used elgg, but not in years).



Just read you above post - be aware that $User is not the same as $user. If you're using one instead of the other (you've used both in your code samples), that could be the problem.