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?
Related
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,
});
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"];
?>
I'm having a problem trying to use a PHP variable within JavaScript. I keep getting the following message.
"Invalid or unexpected token. Message: Undefined variable: example."
I'm unsure why example is being undefined, as it is defined within the php code. Here is my code:
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = "<?php echo json_encode($example); ?>";
</script>
Does anyone have any suggestions? I have also tried the following javascript that results in the same problem:
<script type="text/javascript">
var php_var = "<?php echo $example; ?>";
</script>
Firstly, your original code has a syntax error: $example = '2' needs a semicolon.
Secondly, the next piece of code is just assigning the string <?php echo $example; ?> to the JavaScript variable php_var where the $example PHP variable is first substituted. The $example variable should be initiated properly first, however, for this to work.
As a separate note: JS cannot execute PHP directly -- only a PHP server can do so. What you're most likely trying to do is this:
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = '<?php echo $example ;?>';
</script>
This should work, use single quotes
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = '<?php echo $example; ?>';
</script>
Tried and working both variant:
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = <?php echo json_encode($example); ?>;
console.log(php_var);
</script>
<script type="text/javascript">
var php_va = "<?php echo $example; ?>";
console.log(php_va);
</script>
How I can set the JavaScript variable strUser from PHP?
I am using the following code:
<script>
function val()
{
var e = document.getElementById("ali");
var strUser = e.options[e.selectedIndex].text;
}
</script>
brand<select id="ali" onChange="val()">
<?php
$brand=modsearchkhodroHelper::retrieve();
foreach($brand as $item)
{
?>
<option value="<?php echo $item['brand']?>" selected="<?php $id=$item['brand']?>">
<?php echo $item['brand']?>
</option>
<?php
}
echo "</select>";
?>
If you want to set the variable when the page loads, you could use something like this in the PHP code:
<script type="text/javascript">var strUser = <?php echo json_encode($someVariable); ?>;</script>
Just make sure to remove the later variable declaration from the JavaScript.
If you want to set the variable after the page loads, you'll have to use an AJAX call to ge the value from the server.
Use Cookie
in your javascript
<script type="text/javascript">
document.cookie = "cookieName=cookieValue";
</script>
in your php
<?php
$phpVar = $_COOKIE['cookieName'];
echo $phpVar;
?>
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>