I want to add my javascript popup code which is in an html file to my php function. It's a visitor counter in php. I want when visitors visit the page 4 times my popup html js is displayed
Asked
Active
Viewed 107 times
-1
-
> I want when visitors visit the page 4 times my popup html js is displayed Why don't you do this in javascript instead of PHP? Would seem easier no? – JGreatorex May 16 '22 at 11:53
-
it's an api that counts visitors. Maybe, Do you have any other options for linking the two codes? Maybe echo with html? – Myllouw May 16 '22 at 11:54
-
Don't think about HTML/JS in your PHP logic, do it in your Twig templates instead. You are probably looking for `app.session` (read the [docs](https://symfony.com/doc/current/templates.html#the-app-global-variable)). – AymDev May 16 '22 at 11:57
-
1Have you tried to use flash messages? [doc](https://symfony.com/doc/current/controller.html#flash-messages) – Charlie Lucas May 16 '22 at 13:08
1 Answers
0
Do you just mean like this?
if ($increment == true) {
$compteur_actuel = $session->get('compteurClicsFreemium', 0);
$session->set('compteurClicsFreemium', $compteur_actuel+1);
if ($compteur_actuel > 4) {
?>
<div>
Your HTML code here
</div>
<?php
}
}

BogusFeet
- 31
- 1
- 8
-
OP did not tell where the PHP code was located, this could get displayed anywhere. He should never do this in a Symfony application anyways. – AymDev May 16 '22 at 11:59
-
If he doesn't want the div to appear where the code is executed then just store a boolean with the result and check the boolean where you want the div to appear... – BogusFeet May 16 '22 at 12:01
-
Will be something in your style or javascript if it's disappearing since the php code will only run once – BogusFeet May 16 '22 at 12:05
-
Well I can't see all your code but it could be something as simple as an animation in one of your css classes, just double check. – BogusFeet May 16 '22 at 12:20
-
It's hard to help much without seeing the full project. Are the 2 code snippets located in different files? Is the PHP file separate to the full HTML page? – BogusFeet May 16 '22 at 12:35
-
You'll need to have your php if statement as part of the main html page, so you will need to change your main page to a php file and embed the php code where you want the popup to appear within your html. Hope that helps. – BogusFeet May 16 '22 at 12:52