Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: Best way to turn a simple static website interactive with PHP?
17 points by bkfh on March 25, 2019 | hide | past | favorite | 27 comments
I'm able to easily design a static website with Bootstrap, but struggle as a non-dev with connecting simple things like forms, although I have some basic PHP knowledge.

A friend helped me with setting up Netlify and React, but I find it to be over engineered for my simple requirements and always need to ping him when I have to make some small changes. What I'm looking for is support for templates to minimize recurring code and submitting forms.

What can you recommend and is easy to set up?




If you don’t want to install any frameworks, just use PHP in the way it was originally meant to be used.

When someone POST a form, you can use the $_POST superglobal to extract all of that information from the submitted form.

Then, you can store that information in the $_SESSION superglobal.

Lastly, when the user visits a page with the same or similar form entries, you can actually “echo” their own answers from a previous page into the form.

This is baby’s first PHP here, not really production grade, but still quite useful.


I'd 2nd this. Just go get some cheap PHP hosting with Hostgator or something similar.

At this point, you don't need a best practice recommendation, you need the learning experience and working with PHP by itself will give you that without a lot of headache.


<?php print_r($_POST); ?>

go from there


<pre><?php print_r($_POST); ?></pre>


<?php header('Content-type: text/plain'); var_dump($_POST);


Seems like you should be using a content management system (CMS) instead of coding a website. I'd recommend a hosted service[0] so you don't have to manage updates and security yourself.

[0] Examples: https://wordpress.com/pricing https://webflow.com/hosting


exactly what I will suggest. a simple CMS like wordpress would work wonders for what OP needs.


You can do a surprising amount of handy things by just using <?php include "stuff.php"; ?> blocks to grab chunks of other files, without making something a "PHP site" in a traditional sense.

You just have to be really careful particularly with any data input fields you have when you roll your own code, as that's pretty much the primary assault vector for exploiting PHP websites.


If this was me that should should begin the journey as a frontend developer moving towards the backend. You know, when you have your backend up and running in php, you are going to request JSON from it. So why now start by adding a folder to your web app and in that folder place some static json files with the content you one day wants to retrive from the backend. When you have your frontend working with these static json files, then you can look into how to create a WebApi in php that generates the same json. This way you can do it step by step.


If you're already using Netlify, why not use their forms feature? It seems very easy to use: https://www.netlify.com/docs/form-handling/

(I'm assuming you just want to collect submissions in a table, or send them somewhere via Zapier, and don't need to do anything interactive with the info you collect.)


I also recommend netlify.

You could use google forms for free and push the results to a google sheet.


I still build HTML/CSS/JS websites (the old fashioned way) and occasionally need a basic form handler. I've used FormtoEmail Pro for years, it has both a one-file PHP script, and hosted option.

https://formtoemail.com *No affiliation


You might want to look into Laravel (https://laravel.com/). It has a lot of stuff you won't need, but it's also has a very intuitive API, is well documented and will lead you down a route of best practices for this kind of website. I've found that server-rendered websites using Laravel are pleasingly simple.

See https://laravel.com/docs/5.8/blade for specific documentation on templates and forms, although I would recommend a more comprehensive look through the documentation, or working through a tutorial.


Or for a more simple, easy to set up and lightweight alternative OP could use the micro framework made by Laravel: Lumen. This might be more suitable for this case. https://lumen.laravel.com/


Try Lumen which is a lightweight version of Laravel, for form processing this is all you need.

https://lumen.laravel.com/docs/5.4/


OP here. Thanks for all your responses, they helped me assessing on how I can get max output with least amount of work to quickly get online.

I will go ahead and create a static website, add some simple PHP to include header and footer on each page (either w/out any framework or Lumen) and apply some Zapier (or Netlify) magic to make forms work.

Once the MVP is validated, we might as well switch to some other framework or stack but for now, this is totally doing the trick.


Learn PHP if you want to learn PHP. Using Frameworks as a shortcut will undoubtedly create bad habits in the future. Unless you wanna learn the framework itself that is


You seem to gloss over what your forms are doing. Do you _need_ a server side language like PHP? Or can you get away with jQuery?


It seems like Firebase could be useful.


Bridging this gap is the holy grail of web hosting. One can do a lot from static sites nowadays. There are lots of third party services that provide generic form backends, they’ll be posting here in no time.

My question is; if you can build it, why not take the and get a shared hosting account and write the php yourself? Is OP able to list the obstacles?


Wordpress is your default option, although it may be overkill if you’re just doing some simple forms. It’s hard to say beyond that since you don’t provide enough information about your actual needs. I suggest sitting down with a local expert to better clarify your needs and a proper solution.


No need to use PHP. Example of forms with a static website or static site generator : https://forestry.io/blog/5-ways-to-handle-forms-on-your-stat... Example of comments for static sites or SSG : https://darekkay.com/blog/static-site-comments/ ( I recommend personally Isso )


Depends on what you want to achieve. For example you can only forward forms‘ input to a mail address and go from there. Or if you use a CMS for example, there are lots of plugins which might help you, without getting to deep. Or you can use some "external" services for newsletters and stuff. They offer their own code, JS, PHP and others, you can implement.


I think you probably want to keep the Netlify but lose the React.


This is what I was thinking. Netlify is a static webhost, so if you're already comfortable hand-coding a static, bootstrap-based website you should be good to go.


Ditch React. Try JQuery. You can then stay on static hosting from Netlify. Going to PHP is no less complicated IMO


jquery and turbolinks. If you want to go further than that I'd look at VueJS, which doesn't require your site be an SPA but gives you DOM bindings. Sound like you're just looking for the former though.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: