Generate a PHP page based on HTML input info

Hello Experts,
     I have another question to ask...  I would like to create a web page which based on a user's input.  I create a form and once a user input some info and click a submit button, a php page will be generated, reflecting user's input.

     But I am not sure how to apply some css info into PHP and at this point, I am not generating a php page.

     I am sure that some of the PHP codes are totally wrong or not so well-written, if someone say nicely, because I'm still at a beginner's level.
     But I would like to know how to solve my problem and how to write PHP properly to perform what I would like to do in this small project.

createAWebPage.phpcreateAWebPage.html

Thank you for your help in advance.
sabregirlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
Hi, sabregirl.
First, let me say you can put display errors instructions ony once:

<?php	
    ini_set( 'display_errors', 1 );
    error_reporting(E_ALL & ~E_NOTICE);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 areixr//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3/org/1999/xhtml">

    <head>
	    <meta charset="UTF-8">
	    <title>Create A Web Page</title>
		<?php	
			$txtBkgColor=filter_input(INPUT_POST, "txtBkgcolor");
			$txtTextColor=filter_input(INPUT_POST, "txtTextColor");
		?>
    </head>
    <body bgcolor="<?php print $txtBkgColor; ?>" text="<?php print $txtTextColor; ?>">
		<?php
			$txtCaption=filter_input(INPUT_POST, "txtCaption");
			$txtContent=filter_input(INPUT_POST, "txtContent");
            
			print "<h1>$txtCaption</h1>";
			print "<p>"$txtContent"</p>";

        ?>
	</body>
</html>

Open in new window


In addition you put too double quotes printing the content. The correcct way is

                  print "<p>$txtContent</p>";

Let me play a  little for further suggestions.

Cheers
0
Marco GasiFreelancerCommented:
Secondly, you must pay attention to letter case: Php is case-sensitive so if you write: txtBkgcolor is totally different than txtBkgColor:

                  $txtBkgColor=filter_input(INPUT_POST, "txtBkgColor");
works fine.

So working code is:

html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 areixr//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3/org/1999/xhtml">

    <head>
	    <meta charset="UTF-8">
	    <title>Create a Web Page</title>
    </head>
    <body>
	  <h1 align="center">Create a Web Page</h1>
      <br>
        <form name="form1" method="post" action="createAWebPage.php">
<table width="600" height="400" border="1" align="center">
   <tr>
      <td width="200"><div align="right"><strong>Caption:</strong></div></td>
      <td width="400"><input name="txtCaption" type="text" size="100" /></td>
   </tr>
   <tr>
      <td><div align="right"><strong>Background color:</strong></div></td>
      <td><input type="text" name="txtBkgColor" />
      HTML color or #rrggbb </td>
   </tr>
   <tr>
      <td><div align="right"><strong>Text color:</strong></div></td>
      <td><input type="text" name="txtTextColor" />
      HTML color or #rrggbb </td>
   </tr>
   <tr>
      <td>
         <div align="right"><strong>Content:</strong></div></td>
      <td><textarea cols="75" name="txtContent" rows="8" /></textarea></td>
   </tr>
   <tr>
      <td colspan="2">
         <div align="center">
            <input type="submit" name="submit" value="Create A Page" />
            </div></td>
   </tr>
</table>
</form>
	</body>
</html>

Open in new window


php
		<?php	
		    ini_set( 'display_errors', 1 );
		    error_reporting(E_ALL & ~E_NOTICE);
			?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 areixr//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3/org/1999/xhtml">

    <head>
	    <meta charset="UTF-8">
	    <title>Create A Web Page</title>
		<?php	
			$txtBkgColor=filter_input(INPUT_POST, "txtBkgColor");
			$txtTextColor=filter_input(INPUT_POST, "txtTextColor");
		?>
    </head>
    <body bgcolor="<?php print $txtBkgColor; ?>" text="<?php print $txtTextColor; ?>">
		<?php
			$txtCaption=filter_input(INPUT_POST, "txtCaption");
			$txtContent=filter_input(INPUT_POST, "txtContent");
            
			print "<h1>$txtCaption</h1>";
			print "<p>$txtContent</p>";

        ?>
	</body>
</html>

Open in new window


But if you want to embed some css, you have two ways: or you follow the same method, printing inline css code in your php-geneated html page; or you can use form data to create a css file, save it on your server and then put a linkk to it in your html file. Give it a try and ask if you have problems ;)
Cheers
0
sabregirlAuthor Commented:
Hello again  marqusG,
     Thank you again for your help.  I definitely need to be more careful what I type :(
As I'm getting sick (physically, a.k.a. cold), I will fix some mistakes you pointed out for me and will work tomorrow...

     So for css, how can I generate a user-input-info css file...?

Thank you for your help!
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Julian HansenCommented:
If I understand your question you want to be able to output the styling for the elements you are creating?

Firstly, the use of the bgcolor and text in the body tag is deprecated - you should use style sheets for that.

Also I would recommend use the HTML 5 doctype

I am assuming that filter_input is a function defined somewhere in your code for retrieving values from the POST array.

To style your code add as style section and put the posted values in there.

<?php	
ini_set( 'display_errors', 1 );
error_reporting(E_ALL & ~E_NOTICE);
?>
<!DOCTYPE html>
<html lang="EN" dir="ltr" xmlns="http://www.w3/org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Create A Web Page</title>
    <style type="text/css">
        body {
            background: <?php echo filter_input(INPUT_POST, "txtBkgColor");?>;
            color: <?php echo filter_input(INPUT_POST, "txtTextColor")?>;
        }
    </style>
</head>
<body>
		<?php
			$txtCaption=filter_input(INPUT_POST, "txtCaption");
			$txtContent=filter_input(INPUT_POST, "txtContent");
            
			print "<h1>$txtCaption</h1>";
			print "<p>$txtContent</p>";

        ?>
	</body>
</html>

Open in new window

0
Marco GasiFreelancerCommented:
@julianH: filter_input is a php function documented here

@sabregirl, to create a css file you can do something like this:

<?php	
    ini_set( 'display_errors', 1 );
    error_reporting(E_ALL & ~E_NOTICE);
	
	$txtBkgColor=filter_input(INPUT_POST, "txtBkgColor");
	$txtTextColor=filter_input(INPUT_POST, "txtTextColor");
$css =<<<CSS
body {background-color: $txtBkgColor; color: $txtTextColor}
CSS;
file_put_contents('mystyle.css', $css);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 areixr//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3/org/1999/xhtml">

    <head>
	    <meta charset="UTF-8">
	    <title>Create A Web Page</title>
        <link rel="stylesheet" href="mystyle.css" type="text/css"/>
    </head>
    <body>
		<?php
			$txtCaption=filter_input(INPUT_POST, "txtCaption");
			$txtContent=filter_input(INPUT_POST, "txtContent");
            
			print "<h1>$txtCaption</h1>";
			print "<p>$txtContent</p>";

        ?>
	</body>
</html>

Open in new window


As you can see, it's your script with just a little of extra-work: simply use this and you'll see it is working fine. You can produce a very complex dynamic css with this tecnique :)

Cheers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
@marqusG - thanks for the link - never had cause to use it as I do my own filtering but always good to learn something new.
0
Marco GasiFreelancerCommented:
Me too: I knew it today when I check what it could be 'filter_input'! ;) I thought it were something old and on the contrary it has been introduced in Php 5: I admit I don't care enough to stay up-to-date :( cheers
0
Ray PaseurCommented:
I'm guessing you're new to PHP.  This article contains some learning resources that can help you get started.  And perhaps more importantly it can help you keep away from the less dependable or outright dangerous examples that seem to litter the internet.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

This article contains some things you don't want to do.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12293-AntiPHPatterns-and-AntiPHPractices.html

Fortunately for all of us, the PHP.net web site has documentation on every PHP function.  There are something like 1,500 functions so you can't memorize them all, but you can look them up if you're not sure.  Just point your browser to http://php.net/ and type the name of the function at the end of the URL.  Example: http://php.net/filter_input

I want to take just a moment to hate on this line of code (but in a nice way):

error_reporting(E_ALL & ~E_NOTICE);

What you're telling PHP with that statement is "Don't tell me if I have a typographical error in my script.  Make me guess about it."  That's not the real intent of suppressing Notices, but it's the practical effect.  The intent was to make PHP "easy" so that novice programmers could fumble along and use undefined variables as if they were empty strings, or zero or boolean FALSE.  The unwanted consequence was that many PHP programmers came to think that it was acceptable practice to rely on undefined variables and constants.  Then one day when in haste they misspelled a variable name, they found themselves confounded by the script failure (a run-time failure!) and spent hours looking at the same piece of code, over and over, never seeing that the script relied on an undefined variable.  If you want to give yourself a HUGE advantage over other programmers start your script with this:

error_reporting(E_ALL);

If you get Notice messages, correct the code!  But don't suppress the messages; they are your connection to what PHP is doing with your script.  You wouldn't try to drive with a blindfold on; don't program that way, either.

You can set and reset the error_reporting() levels any number of times in a script.
0
sabregirlAuthor Commented:
Hello JulianH,
Thank you for your suggestion, but as I'm still learning HTML5, it's less stress for me to use HTML4, as I don't need to check what is deprecated or not.

Also, this is a part of challenge for me what I can do with PHP.  When I work on a web site, of course, I will create an external css file or two :)
0
sabregirlAuthor Commented:
Hello Ray_Paseur,
Thank you for your comment.  Yes, I'm a very beginner of PHP and my current goal is to be a web designer who has some "basic" knowledge of JavaScript and PHP!

Anyway, thank you for the links you posted.  
It's always nice to know some extra knowledge other than from a book.

From now on, I will change my error reporting code to what you suggested me to use.
0
sabregirlAuthor Commented:
Hello marqusG,
Thank you for letting me to show the sample.
I really appreciate it :)
0
sabregirlAuthor Commented:
Thank you for all the experts to answer such a beginner's question!
I was sick for the last two days and I apologize that I didn't check the site often enough.

To be honest with you, I posted a different question to a different site, which is written in my mother language (English is not my first language, as you can guess!), but some of the comments are not-so-friendly, like, "Oh, you don't understand such a basic PHP?  I can't believe that you are posting this type of question..."

All the experts here are very kind, generous and patient to explain me a lot of things which I can't find in my book and I really appreciate it.

I'm very sure that I will post some other questions in the near future, but I hope that I can find a solution in here if I stuck.
Thank you again for all of your help!
0
Ray PaseurCommented:
Thanks for the points, and thanks for using EE, ~Ray
0
Marco GasiFreelancerCommented:
Thanks for the points, sabregirl and good luck with your learning. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.