Advanced Search

Results 1 to 10 of 10

Thread: Merging php code into js

  1. #1
    Join Date
    Jan 2006
    Location
    Derbyshire, UK
    Posts
    74
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Merging php code into js

    I have this script that works great...

    Code:
    <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...

    Code:
    <?php echo $User->location;?>
    but it didn't work. Any help would be fab.
    Last edited by Burgin; 05-24-2012 at 03:02 PM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,629
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    ..."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.
    Last edited by traq; 05-24-2012 at 04:20 PM.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  3. #3
    Join Date
    Jan 2006
    Location
    Derbyshire, UK
    Posts
    74
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    Sorry, nooby with php & js.

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

    Here's the output code...
    Code:
    <script language="javascript">		
    			$("#div_weather").putWeather({
    				location:""
    			});
    		</script>
    To prove that $User->location exists I put this code...
    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

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,629
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by Burgin View Post
    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
    PHP Code:
    error_reporting( -); 
    towards the top of your php script.
    Quote Originally Posted by Burgin View Post
    Here's the output code...
    Code:
    <script language="javascript">		
    			$("#div_weather").putWeather({
    				location:""
    			});
    		</script>
    This would seem to indicate that $User->location does not exist (or, is empty).
    Quote Originally Posted by Burgin View Post
    To prove that $User->location exists I put this code...
    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:

    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 Code:
    <?php
    exit( var_dump$User->location ) );
    Last edited by traq; 05-24-2012 at 06:36 PM.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  5. #5
    Join Date
    Jan 2006
    Location
    Derbyshire, UK
    Posts
    74
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    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
    Code:
    <?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

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    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?
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Jan 2006
    Location
    Derbyshire, UK
    Posts
    74
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    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...
    Code:
    <?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.

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    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?
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Jan 2006
    Location
    Derbyshire, UK
    Posts
    74
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    The js...
    Code:
    <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...
    Code:
    <?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...
    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.

  10. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,629
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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).

    Edit:

    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.

    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

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
  •