Pushing PHP variables to dataLayer using JS - javascript

I heard from a good friend that people in this community can be very helpful and good-hearted so I'd love some help from you peops!
I've been trying to push php variables to the datalayer using the .push() method in javascript (for google tag manager and google analytics). Below is the code snippet to better understand what's happening.
<?php . . . . . . ?>
<script language='javascript'>
$(window).on("load", function() {
var variable1 = <?php echo $variable1_php; ?>;
var variable2 = <?php echo $variable2_php; ?>;
var variable3 = <?php echo $variable3_php; ?>;
window.dataLayer.push({
'event' : 'eventName',
'variable1_for_gtm': $variable1,
'variable2_for_gtm': $variable2,
'variable3_for_gtm': $variable3,
});
});
</script>;
<?php . . . . . .
What am I missing exactly? Thanks a lot!!
P.S. Other events are correctly being tracked by GTM and recorded in GA.

Instead of using a $ sign just use variable names without it. $ sign is used for PHP variable names but not javascript.
var variable1 = <?php echo $variable1_php; ?>;
var variable2 = <?php echo $variable2_php; ?>;
var variable3 = <?php echo $variable3_php; ?>;
window.dataLayer.push({
'event' : 'eventName',
'variable1_for_gtm': variable1,
'variable2_for_gtm': variable2,
'variable3_for_gtm': variable3,
});

Related

how to pass php variable to javascript?

I am using following code to pass PHP variables to javascript. but it is not working.
function gto(str) {
document.getElementById('goto').action = str;
document.getElementById('ID').value = <?php echo "$userid" ?>;
document.getElementById('name').value = <?php echo "$user_name" ?>;
document.getElementById('gname').value = <?php echo "$usergname" ?>;
document.getElementById('fmname').value = <?php echo "$userfname" ?>;
document.getElementById('img').value = <?php echo "$userimg" ?>;
document.getElementById('email').value = <?php echo "$useremail" ?>;
document.getElementById('goto').submit();
}
Following is the PHP code
<?php
if($_POST["name"] == null)
{
$user_name = 'Annomyous';
}
else{
$user_name = $_POST["name"];
$userid= $_POST["id"];
$usergname= $_POST["gname"];
$userfname= $_POST["fname"];
$userimg= $_POST["img"];
$useremail= $_POST["email"];
}
echo "<p style='color : white'>$user_name";
echo "$userid" ;
echo "$gname";
echo "$fname";
echo "$img";
echo "$email";
echo "$user_name";
echo "$user_name</p>";
$user_name =htmlspecialchars($user_name);
$user_name =str_replace("<script>","", $user_name);
?>
the output is a follows:
ReAlItY TuTs104598758504708047866ReAlItY TuTsReAlItY TuTs//this is php echo output.
JAVASCRIPT OUTPUT:-
function gto(str) {
document .getElementById('goto').action = str;
document.getElementById('ID').value = ;
document.getElementById('name').value = Annomyous;
document.getElementById('gname').value = ;
document.getElementById('fmname').value = ;
document.getElementById('img').value = ;
document.getElementById('email').value = ;
document.getElementById('goto').submit();
}
Function gto is called here:
<button class="w3-btn header-btn" onclick="gto('Contact.php');">Contact Us</button>
I can see in PHP output I am getting all variable output.
but nin juavascript im getting only Annonymous why????
I need to pass post variables to contact us so i am using the form tag and javascript but this is not working Please help me!
Thanks in Advance
values from php to js can be passed in many ways, here's one of them
try to pass the php values when calling the js function, like this:-
<button onclick="gto('Contact.php','<?php echo $userid;?>','<?php echo $username;?>');">Contact Us</button>
and then get values on js function like this:-
function gto(str,userid,username) {
document .getElementById('goto').action = str;
document.getElementById('ID').value = userid;
document.getElementById('name').value =username;
}
that's it, now use the values in js as your wish
Uh, found another issue. The js is in smart quotes, which won't work... "`" is invalid. use "'".
Also, w3schools can't handle php in their editor, so it's no use.
Example for POST requests: https://www.w3schools.com/code/tryit.asp?filename=FQSA8MJYGJ47
Hope this helps.

Get javascript variable value in php

I need to get javascript variable value in php file.
html example:
UPDATE:
$html = '
<script>
window.runParams.adminSeq="3423423423423";
window.runParams.companyId="2349093284234";
</script>';
Shout I use regex ? regex is very complex to me... any help ?
<?php
$html = '<script>
window.runParams.adminSeq="3423423423423";
window.runParams.companyId="2349093284234";
</script>';
$variables = ["adminSeq", "companyId"];
$counter = 0;
foreach($variables as $variable) {
preg_match_all('/"(.*?)"/', $html, $matches);
${"$variable"} = ($matches[1])[$counter];
$counter++;
}
echo $adminSeq; // Prints out: 3423423423423
echo $companyId; // Prints out: 2349093284234
?>
You can also use GET requests to do this. The link would look like http://localhost/?adminSeq=3423423423423&companyId=2349093284234 then get out these values in PHP with:
<?php
$adminSeq = $_GET["adminSeq"];
$companyId = $_GET["companyId"];
?>

Failing to embed php in javascript

Im trying to give a javascript variable a value from php, this way:
<script type="text/javascript">
var id = <?php echo json_encode( $_GET['id']); ?>;
window.alert('Javascript still works');
The probem is once include the line: var id = <?php echo json_encode( $_GET['id']); ?>;
My javascript breaks any ideas on the way around this?

Referencing a defined html variable in JS

My index.php page includes a config.php file, which returns an array that I have defined some variables in by using "define('var1' , 10)".
I am trying to validate my forms input, but I can't figure out how I can reference var1 from within the JS function. What is the easiest way to do this?
Just echo it to a javascript variable:
<script type="text/javascript">
var var1JS = "<?php echo $var1; ?>";
</script>
Not quite sure I am full understanding without seeing the code but, you could echo the variable from the PHP array in the JS function (as above answer).
Or Echo the entire JS query:
$y = count($PHPdata_array);
echo "function exampleFunction() {";
echo "var ArrayName = [";
for ($i = 1; $i <= $y; $i+=2) {
echo "{" . $PHPdata_array[$i] . "," . $PHPdata_array[$i-1] . "},";
}
echo "];";

Setting JS variable to PHP variable error

I want to use a php login name in my javascript game...
I have this at the top of my php page that runs the javascript:
<?php
$username = (isset($_GET['username']) ? ($_GET['username']) : "dude");
?>
In my javascript I have this... which doesn't alert the name at all and throws a Uncaught ReferenceError: daniel is not defined error:
var name = <?php echo $username; ?>;
alert(name);
And then I have this... which displays the name correctly at the top of the HTML:
<?php
echo "Welcome back " . $username . "...";
?>
If the name is being displayed correctly server side when the page has loaded, why can't it be alerted out from the Javascript?
Thanks
You aren't delimiting the value as a JavaScript string. It may be a string on the server, but not on the client. Do the following:
Change this:
var name = <?php echo $username; ?>;
To this:
var name = "<?php echo $username; ?>";
Enclose your php code within quotes.Try like this :
<script type="text/javascript">
var name = '<?php echo $username; ?>';
alert(name);
</script>

Categories

Resources