I ran into an interesting wall today working on a simple form with PHP and HTML and can't think of a quick fix.
I'm using the PHP script I've written when the user presses submit on my HTML page. In order to re-display the page after running the script I simply write
include "html_forms.html"
at the bottom of the PHP script. Firstly, this seems like a bad design for presenting the page as it needs to be constantly fully re-displayed but I couldn't figure out how to circumvent this problem.
The problem I'm more concerned with here though is that I'm trying to keep the form data from before the user first pressed submit by using PHP. To do this I modify the value of each input like this (within the HTML):
value = "<?php echo $website; ?>
However, when the webpage is first loaded from the HTML (I'm guessing it can't find the PHP to reference since the Submit button hasn't been clicked yet so) in my entry box on the page the first time it's loaded it prints <?php echo $website; ?>
. If I load the PHP first it doesn't have that problem but this schism makes me question the overall design that I'm going for and although it might fix the problem in this case it seems like it only works because the simplicity of the webpage.
My main question is how to fix this? I've thought of looking into just emptying the html entries on the first go and other techniques but I'm not sure if this would be the best approach since making special cases especially for the first time the page is clicked seems like bad form.
Thanks in advance for the help guys and this is the first question I've asked on stack-overflow so let me know if there's anyway I could improve my question-asking and don't be too harsh on me!
Basically the issue is that your web server does not parse .html files by PHP interpreter. You have two options from here:
If you use Apache you may configure .htaccess to have following line:
AddHandler php5-script .html
You may rename your .html file into .php file
However both options are bad design: you should allow user to interact with your PHP script only and use underlying html as template if needed.
I'm guessing the problem is that you load the page html_forms.html
, then submit the form to a page like process_form.php
. Within process_form.php
you are calling include 'html_forms.php';
which allows for value = '<?php echo $website; ?>'
because it's inside a PHP page.
However, when you reload html_forms.html
, it's back to being just an HTML doc, and so you lose your PHP-ability, and thus, value = '<?php echo $website; ?>'
is translated to value = ''
- HTML will completely ignore your PHP.
Short answer: use PHP extensions. Rename html_forms.html
to html_forms.php
and you can use all the PHP magic you need.