Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Dylan Hobbs
9,279 PointsBest way to create dynamic content on a page?
I don't know if there is a way to do this with javascript or if I have to use PHP, but I have a front-page with 3 select boxes. All 3 select boxes link to the same page however, I want to display different prices/text on this page depending which box was selected to arrive on the page.
Is using php includes the only or best way to achieve this?
3 Answers
Patrick Cooney
12,216 PointsDoes it have to go to the second page? With Javascript you can definitely inject new HTML into the current based on what the user clicks. Would that be an option?
Kevin Korte
28,149 PointsStore the value of the of the user selection in a cookie or local storage, and than read it on the next page load.
Christopher Rebacz
10,904 PointsI like the local storage answer better than the one I gave.
Patrick Cooney
12,216 PointsIf the user doesn't allow cookies I'm not sure if this would work. Dylan, is your worry about PHP that you want to keep this simple or is it that you don't know PHP and would have to learn? If the problem is you don't know PHP you could look into Node.js and Express. Here's a quick example of processing a form with Node.
Kevin Korte
28,149 PointsCorrect Patrick,
- If the user doesn't have javascript enabled, none of this works.
- If the user doesn't have cookies enabled, it won't work.
- If the user doesn't have a browser that supports local storage, it won't work.
You could use modernizr to detect for all 3 of the these issues, and than decide what to do in the event any of these tests fail. That would allow him to continue to use a client side javascript solution while having a graceful fallback if the client doesn't support one or more of these gottcha's. But now where getting into enough complication to start looking at what you recommended, Node.
It's a good suggestion looking at Node, if he's comfortable with JavaScript already. PHP also has a pretty low entry bar, although still has learning curve to it. It could be a good opportunity to learn it. I'd venture a guess, IMO that PHP has a lower entry bar than Node, but that's just me.
Dylan Hobbs
9,279 PointsAlright, alright, alright. I guess i'll do PHP, haha.
I'm not completely foreign to PHP, I know how to create submission forums and use phpmyadmin etc.. I just typically focus on front end languages.
In PHP, which function or docs should I read on the best way to achieve this?
Thanks again, guys.
Kevin Korte
28,149 PointsHaha sounds good! My first question is would you want this to go as a GET or POST request?
Dylan Hobbs
9,279 PointsHmm, I suppose it would be a GET request because it allows caching? So the users would be able to link the page afterwards, correct?
Basically the site I am working on has 3 tables/options on the home screen. Depending on the option they select, they are then brought to the second page which has pricing. The pricing values will change dependant on the option they selected in the first page.
If it helps clear it up, here is the staging link: http://kninetofive.likewater.ca
Patrick Cooney
12,216 PointsI think it depends. If he's new to Javascript and doesn't know it very well then you're absolutely right and PHP would be easier. However if he's fairly comfortable with Javascript I think it would be easier to learn a new framework than an entirely new language. The other place PHP has a major advantage is many web servers come with it set up which is unlikely with Node. You can pretty easily submit a form to itself and have the logic on that page change the contents.
Kevin Korte
28,149 PointsDylan, I think GET is the right choice. I think you should be able to just submit the choice via get to the second page, where you read the get value and show your prices. I might try to have those 3 boxes mechanically be radio inputs, since that most closely gives you the behavior you want. You can have an event listener to submit the form when one is clicked.
Than on the second page, read the get value, and inject your prices via PHP when the html is sent to the browser. Check out some of the PHP courses here, and you'll learn the basics.
Patrick, I agree there. Setting up a Node host might be the most difficult part. But with services like Heroku who offer Node, even that bar is low now, which is good for guys like me.
For both of you, looking at that link, I can't help but think this shouldn't be one page, not two. I don' know, thoughts? The form submit seems unnecessary to load another page to just show prices. You could even manipulate your urls if you wanted with the pushState() method, getting the same effect without a form submit and page reload.
Or just simply swapping text nodes with jQuery.
Patrick Cooney
12,216 PointsI misunderstood the initial post. I thought you meant they were check boxes. Looking at that I think you're going to have trouble even with PHP. The buttons are just CSS styling of an <a> element. I haven't worked with PHP in quite a while so Kevin may be able to jump in here and provide a correction, but I don't think you can get PHP POST or GET data from elements styled into buttons. I think those would need to be converted to submit buttons (which you can style in the exact same way with CSS).
Dylan Hobbs
9,279 Points"For both of you, looking at that link, I can't help but think this shouldn't be one page, not two. I don' know, thoughts? The form submit seems unnecessary to load another page to just show prices. You could even manipulate your urls if you wanted with the pushState() method, getting the same effect without a form submit and page reload."
Are you suggesting to not trigger any more reloads and possibly do this with AJAX/jQuery instead? Would that be a better option? After this I am going to be building a small calendar for the user to select the dates and click a 'book now' button.
I'm assuming the calendar would be on another page and require another load (might be too heavy to have it all on one page?) But I never thought of using ajax for that before for fear of the code/page being too bloated but now that I'm thinking of it more, ajax might prove a better experience for the user.
Patrick Cooney
12,216 PointsI tend to gravitate towards running my own server. Heroku isn't a bad option but a lot of the add-ons cost extra money (like Postgres) where if you have your own server you can just add stuff. Places like Digital Ocean have one click installs for various frameworks like Node but it involves knowing what you're doing once it's all set up.
I agree with your last statement. To me it doesn't make sense for this website to have a second page. Because the boxes stay in the same spot and change to pricing this would be an ideal use of AJAX to reload the boxes with the content that would have been displayed on the second page.
Christopher Rebacz
10,904 PointsIf you must use a second page, php (or any server side language) would be your best and simplest option.
However, you can use JavaScript if that is the only option you have available. You would use JavaScript on the second page to parse the url query parameters.
There is a great snippet on CSS Trickets called Get Url Variables which you can use to grab any data that is passed via query parameters. Don't have the url, but it is easy to find with a quick google search. You can then use that data to determine what price and text should be displayed on the page.
Dylan Hobbs
9,279 PointsAwesome! Thanks for the response, Chris.
Is this the article you are referring to? – https://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/
Would you say the JS way is less of a best practice or has any downsides compared to PHP? If I were to use PHP would it just be a PHP Include or which function would be best?
I am just trying to keep it light and simple.
Dylan Hobbs
9,279 PointsDylan Hobbs
9,279 PointsNo, unfortunately it would have to go to a new page since the design is someone different. Is PHP my only option? Thanks for the response