Jump to content


Photo

How to access a PHP variable in both CSS and HTML?

variable variables php variable php variables css

  • Please log in to reply
1 reply to this topic

#1 MythicalTomatoSoup

MythicalTomatoSoup
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 15 October 2016 - 05:18 PM

Hey!
I want to create a website that can test your counting pace. I’ll make colored, square div that change color after 10 to 30 seconds when you hover over it, and then you’ll type down how long you think it took in a form to check if you’re right.
My solution to do this is to create the PHP variable $time = rand(10,30), and in CSS get the div:hover transition-delay to be $time. Then, after the form, I need the same variable for if($_POST[timeguess] = $time) {echo “Correct!”}.
I have no idea how to access the same variable in both the CSS and HTML, so I hope some of you can help me out!

Thanks :)



#2 NotionCommotion

NotionCommotion
  • Members
  • PipPipPip
  • Advanced Member
  • 1,601 posts

Posted 16 October 2016 - 11:36 AM

Hello Mythical,

 

The part you need to understand is that PHP lives on the server, and HTML/CSS/JS lives on the client browser.  The only way to can use them on both is if you send the variables from one to another.

 

One option is for PHP to create a web page and for PHP to echo your random time both within your CSS and within a hidden input.  By echoing it, you are writing it or sending it from the server to the client.  Then when the user fills out the form, both the hidden input and the timeguess input are posted or sent to the server.

 

Some recommend not using PHP to dynamically generate CSS (or JS for that mater), and another alternative would be to echo an attribute such as data-time in you HTML, and use JS of the color changing.

 

Good luck!


NotionCommotion




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users