I'm using HTML5 geolocation and Javascript to do a reverse geolocation lookup with Google's API to get the town of the user. This all works fine, and I have the city/town stored in a JavaScript variable named city.short_name.

Now, I'm using wordpress and I'm trying to modify part of the content of my site using:

document.getElementById('contenttochange').innerHTML = <? echo do_shortcode('[<code here>]'); ?>;

Now if the content was just the same all the time, i.e.

<? echo do_shortcode('[just_do_this]'); ?>

it would be fine, but unfortunately I need to add the town as a parameter called search. E.g.

<? echo do_shortcode('[just_do_this search="thetownofuser"]'); ?>

So, essentially, once I've found out the town and have it as a variable, is there any easy way for me to execute the PHP shortcode with that town as a parameter to replace the content I wish to?

share|improve this question
7  
You cannot execute PHP directly from javascript, you'll need to make an AJAX call to send the javascript data to the PHP script. – Austin Sep 5 '12 at 18:59
1  
Right, you are trying to execute php from the client side, which is not possible. – Austin Sep 5 '12 at 19:03
The javascript does not execute the shortcode, the value of <? echo do_shortcode('[<code here>]'); ?> is rendered by PHP and then the document is sent out. – Austin Sep 5 '12 at 19:07
feedback

1 Answer

up vote 1 down vote accepted

PHP is a Server-side Hypertext Preprocessor (actually the roots of the acronym "PHP"). You cannot execute PHP from javascript, because PHP is not "active", to put it in one word.

You send data to the server running PHP as a request, it gets processed through PHP, and out comes XHTML. All those <??> are rendered to their outputs before they are even sent to the client.

The client ONLY deals with XHTML and Javascript (among other things, but for simplicity's sake)

You can echo variables into your javascript, sure, because it is rendered to the document that is sent out the the client. Once it reaches the client, PHP can no longer be used.

To send a variable to your PHP script, you will need to make an AJAX call and send the location variable as a part of your request.

Alternatively, you could create a PHP script that has it's header's content-type set to text/javascript that takes in the variable in $_GET, and include it like so:

<script type="text/javascript" src="setLocation.php?loc=<?= do_shortcode(/* Whatever */) ?>">
share|improve this answer
1  
Okay, I have had a mare. I did know this and for some reason (I'm relatively knew to JS) I was of the mind since I was calling PHP within the JS and JS already had defined the variable I could just <? //something ?> <script>window.something</script> <? finish something ?> Or something similar, if you know what I mean. Anyways yeah it's because it's been a really long day and my mind has been weary lately. Thanks. – Alex Goodger Sep 5 '12 at 19:09
Yep, be sure to click the check to the left if you feel i best answered your question. – Austin Sep 5 '12 at 19:12
Additionally, I would suggest either looking into AJAX, or, better yet, using javascript itself for your loaction app. – Austin Sep 5 '12 at 19:15
As I said in the original post, I'm using Javascript to find the location of the user, entirely Javascript. I have a set of events in a database, and I retrieve them using shortcode as a list of HTML items, I can filter these by location, which is the reason I'm finding out the user's location in the first place. All I need to do is pass the location I've found as a parameter for the shortcode. That's where I've been stuck. – Alex Goodger Sep 5 '12 at 19:54
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.