Call PHP variables with JavaScript
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I'm looking to call variables from the class.gaparse.php file referenced on this GitHub.

I want to call/require/initialize the PHP file before the tag and then write specific variables using JavaScript in the body of the HTML.

I'm going to use the data from the Google Analytics PHP cookie parser to submit hidden data on the visitor when he/she submits a form.

This is for a Joomla website so I will be using the Custom HTML Advanced module.

awarded to Wikimedia

Crowdsource coding tasks.

2 Solutions


Why do you want to send the data to the client and then the client to send the data back to you?

You could get the data you want when processing the form data in PHP.

Edit

<?php
    require("class.gaparse.php");

    $ga = new GA_Parse($_COOKIE);
?>

<form>  
    <input type="hidden" name="campaign-source" value="<?php echo $ga->campaign_source; ?>">
    <input type="hidden" name="campaign-name" value="<?php echo $ga->campaign_name; ?>">
    <input type="hidden" name="campaign-medium" value="<?php echo $ga->campaign_medium; ?>">
    <input type="hidden" name="campaign-content" value="<?php echo $ga->campaign_content; ?>">
    <input type="hidden" name="campaign-term" value="<?php echo $ga->campaign_term; ?>">
    <input type="hidden" name="date-of-first-visit" value="<?php echo $ga->first_visit; ?>">
    <input type="hidden" name="date-of-previous-visit" value="<?php echo $ga->previous_visit; ?>">
    <input type="hidden" name="date-of-current-visit" value="<?php echo $ga->current_visit_started; ?>">
    <input type="hidden" name="times-visited" value="<?php echo $ga->times_visited; ?>">
    <input type="hidden" name="pages-viewed-current-visit" value="<?php echo $ga->pages_viewed; ?>">
</form>
I'm sorry for the confusion. I essentially want to pull the data from the cookie into the hidden fields on the webform, which will be submitted with the other data the user enters after he/she presses submit.
jlevey over 5 years ago
That's what I understood. Does the form post data to the same domain?
tomtoump over 5 years ago
No sir. The form submits to SalesForce.
jlevey over 5 years ago
Check my edit. I don't think you need javascript. You just insert some hidden input fields with the desired values from the cookie.
tomtoump over 5 years ago
The custom HTML module in Joomla does not allow PHP. However, I may be able to call a PHP variable using Javascript. It possible to pass a PHP variable to a JavaScript function? Thank you.
jlevey over 5 years ago
You could have JavaScript to make an ajax request to a PHP file which would return the variables you want. But I would suggest to use a module that allows PHP code.
tomtoump over 5 years ago
Check Flexi Custom Code. It allows you to run PHP code in a module. So the code I posted earlier should work. I think that's the simplest solution.
tomtoump over 5 years ago
Unfortunately, I cannot add extensions to the site. Thank you for suggesting Flexi Custom Code.
jlevey over 5 years ago
Can you add a PHP file on the server?
tomtoump over 5 years ago
Unfortunately I don't have FTP access.
jlevey over 5 years ago
So you can't use PHP. Can you tell me which variables, extracted from the google cookie, are you interested in?
tomtoump over 5 years ago
I'm interested in all the variables from the tracking cookie that are in the class.gaparse.php file.
jlevey over 5 years ago

Hi, jlevey should be pretty straight forward by echoing out js with your php, so if you have this in your php file you would get all the values in global javascript vars to do with them whatever you want. Certainly not the best solution but it should answer you question.

<script type="text/javascript">
<?

require("class.gaparse.php");

$aux = new GA_Parse($_COOKIE);

echo "var campaingsource='".$aux->campaign_source."';";

echo "var campaingname='".$aux->campaign_name."';";

echo "var campaingmedium='".$aux->campaign_medium."';";

echo "var campaingcontent='".$aux->campaign_content."';";

echo "var campaingterm='".$aux->campaign_term."';";

echo "var firstvisited='".$aux->first_visit."';";

echo "var previousvisited='".$aux->previous_visit."';";

echo "var currentvisit='".$aux->current_visit_started."';";

echo "var timesvisited='".$aux->times_visited."';";

echo "var pagesviewed=' ".$aux->pages_viewed."';";

?>

</script>

<script>

var _gaq = _gaq || [];

_gaq.push(['_setAccount', "gaid"]);

_gaq.push(['_trackPageview']);

_gaq.push(['_trackPageLoadTime']);

(function() {

var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;

ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google- analytics.com/ga.js';

var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

})();

`
`

sry didnt see theirs already a solution going on, i now see i totally got you wrong
Ibenor over 5 years ago
If i just use your first script, will I be able to use javascript to document.write "campaign_source" into a form field?
jlevey over 5 years ago
you can use document.my_form.form-txt_filed1.value = campaignsource; then
Ibenor over 5 years ago
where my_form and form-txt-filed1 are of course placeholders for the name of then form and the name of the field
Ibenor over 5 years ago
If I'm echoing variables above tag are they displaying? Essentially, how do I call a PHP variable from class.gaparse.php with Javascript?
jlevey over 5 years ago
sry for no beeing here i mean your joomla does run some php code somewhere. and this point you echo through php a javascript part the gets run by the browser and creates some global variables in javascript you could use verywhere else then.
Ibenor over 5 years ago
essentially you cant call php cariables form javascript, but you can output javascript code with php as well as html code. this javascript code can create javascript variables. i hope you undertsand what i mean
Ibenor over 5 years ago
the first half of my example above will essentially result in a blocksomehting like this. <scipt type="text/javascript">
var campaigncontext="blabla";
var campaginname="balbluupp";
... </script>
Ibenor over 5 years ago
maybe just add me on skype "ibenor23" and i explain it to you more detailed
Ibenor over 5 years ago
If I pull the variables from the php file, can I refer to a variable later in the form?
jlevey over 5 years ago
When I add the first part from your example yesterday, the page crashed. I tried above and then again above the GA tracking script. Each time, it did not work.
jlevey over 5 years ago
hmm i am not sure what you are doing wrong as i said maybe you can add me in skype and we can talk about it. should solve the problem quickly. otherweise i would have to install joomla so i can give you a more detailed instruction
Ibenor over 5 years ago
View Timeline