PHP or HTML to create forms????

Good morning all - I'm getting a little confused on using forms with PHP/HTML... I guess I'm just a little lost on the "proper" way to do things...
I can create a HTML forms page that submits values over to a PHP page to process the results and add them to a database - I have that working (Using PHP/mySQL) However lets say I wanted to created a select list on my HTML page that used rows out of my database to populate the options for the list instead of having to go to the HTML code to add another <option> - To be able to to that my first page (The HTML one) would have to be a PHP page... so .. Should I have a PHPpage with a lot of echo's submitting values over to another PHP page to process it?(is that even possible?)

Thanks - as I said, I'm just a bit confused about when to use php/Jscript/html.  I would think I would just use all .php pages so I can process the data as I'm getting it on the form - but I know I'm missing something because Most form pages I go to are HTML, not php, and I know some are using data driven select lists, etc.

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.

Aaron TomoskySD-WAN SimplifiedCommented:
Just use all .php. You can easily output plain old HTML on a php page. You can't do any scripting on an HTML page.

Olaf DoschkeSoftware DeveloperCommented:
To get the options from a mysql table you surely will need php, as javascript is only runnning client, the only thing javascript can do is request the result of a php skript and write the returned values out as options. Involving javascript in this would assemble what is referred to as ajax technique.

But as you query data from mysql with php while you're at it you could also let php output the options, yes. And as you aren't familiar with this, you would rather not involve javascript in the first place.

Get a grip on any book about php and you'll find a chapter about data driven html form creation for sure. There is quite nothing else you can do to have a form in a browser, alternatives are just plugins, but you won't want this to start with.

Just use all .php first, agreed with aarontomosky.

Bye, Olaf.
Hugh McCurdyCommented:
I agree that you need PHP.

Have PHP both generate the form and process/verify the form.  In a simple case, you can have a variable called $process.

In the form, use a hidden field, such as 'Process' and set the value to 1.

Before outputting anything, process the $_POST array.  You can set $process = $_POST [ 'Process' ];\
(Before you process $_POST, do this --   $process = 0 )

Then after that, you can use either if/then or switch for instance (psuedo code)

switch ( $process )
    case 0;
       Print the form using echo and/or printf

    case 1:
        Verify the form;

More complicated forms might have more values for process.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Hugh McCurdyCommented:
Following up.  This is simplified for learning purposes.  One thing we haven't done is sanitize your $_POST array.  

I'm going to give you an incorrect way to sanitize your input.  The purpose of doing so now is so you can test your program against the $clean array instead of the $_POST array.  When you have a working program, you'll want to learn about sanitizing inputs.  (Don't trust anything the program hasn't created itself during the current session.  Don't trust the users.  Don't trust the database.)

This gets into a bit much for someone who is learning so I'd try something like this

function sanitizePOST ( &$clean )
    $clean [ 'Process' ] = $_POST [ 'Process' ];

    // Do the same for other POST variables.

// in the main area
$clean = array();

Open in new window

Dave BaldwinFixer of ProblemsCommented:
This demo program combines PHP, HTML, a Form, and javascript all on one page.  Save as "Email.php", it posts the data to itself and change the email address to your own.   Something like this can be done to both get and submit data to your database.

# some settings of POST vars
if (!isset($_POST['send']))  $send = ''; else $send = $_POST['send'];
if (!isset($_POST['toText'])) $toText = ''; else $toText = $_POST['toText'];
if (!isset($_POST['ccText'])) $ccText = ''; else $ccText = $_POST['ccText'];
if (!isset($_POST['subjectText'])) $subjectText = ''; else $subjectText = $_POST['subjectText'];
if (!isset($_POST['msgText'])) $msgText = ''; else $msgText = $_POST['msgText'];
if (!isset($_POST['ccText'])) $ccTexth = ''; else $ccText = $_POST['ccText'];
if (!isset($_POST['bccText'])) $bccText = ''; else $bccText = $_POST['bccText'];
if (!isset($_POST['nameText'])) $nameText = ''; else $nameText = $_POST['nameText'];
if (!isset($_POST['fromText'])) $fromText = ''; else $fromText = $_POST['fromText'];

if ($send == "") {
    $title="Test Email Page";
else {
	if($fromText === "") die("No name!");
	$title="Test Email Page";
  $announce="Your Message has been Sent!";
	$header = "From: ".$fromText."\r\n";
//	$header .= "Cc: ".$ccText."\n";
	$header .= "Reply-To : ".$fromText."\r\n";
	$header .= "Return-Path : ".$fromText."\r\n";
	$header .= "X-Mailer: PHP\r\n";
	$header .= "MIME-Version: 1.0\r\n";
	$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
//	ini_set(sendmail_from,$fromText);  
	mail($toText, $subjectText, $msgText, $header, '-f'.$fromText);
//	ini_restore(sendmail_from);

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title><?php echo($title)?></title>
<style type="text/css">
A:link { color: #999999; }
A:visited { color: #999999; }
A:hover {color: #0099ff;}
<script type="text/javascript">
function check()
var at=document.getElementById("fromText").value.indexOf("@");
var eml=document.getElementById("fromText").value;
var nam=document.getElementById("nameText").value;
var alerttxt="";
var submitOK="true";

if (eml.length < 5 || at == -1)
    alerttxt=alerttxt+"Please enter a valid e-mail address!\r\n";
    //return false;
if (nam.length < 3)
    alerttxt=alerttxt+"Please enter your name.\r\n";
    //return false;
if (submitOK=="false")
    return false;


// -->

<body bgcolor="#ddeedd">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" summary="" width="580">
<tr><td align="center">

if ($send != "") {
   	echo ("To: ".$toText."<br>\r\nSubject: ".$subjectText."<br>\r\n".$msgText."<br>\r\n".$header);

<p><b><font color="#000000" size="5">Test Email</font></b></p>
<font size="4" color="#000000">

  <form method="POST" action="Email.php" onsubmit="return check();">
    <p><font size="3"><b>Name: <input type="text" name="nameText" id="nameText" size="46"></b></font></p>
    <p><font size="3"><b>Email: <input type="text" name="fromText" id="fromText" size="46"></b></font></p>
    <input type="hidden" name="subjectText" value="Web Mail">
    <p><font face="Arial" size="3"><b>Message Text:</b></font></p>
    <p><font face="Arial" size="3"><b><textarea rows="6" name="msgText" cols="60"></textarea></b></font></p>
    <p><font size="3"><b><input type="submit" value="Send" name="send" style="font-family: Arial; font-size: 12pt; font-weight: bold"></b></font></p>
    <input type="hidden" name="state" value="1">
  <b><font face="Arial" size="4" color="#e00000"><?php echo($announce)?></font></b><br><br>



Open in new window

hej613Author Commented:
Thanks for the great responses!  if I could, just want to add a followup here... All of my current books tell me to make the .html page that POSTS over to a PHP page - is there any issues I should be aware of that using a PHP page (even if I use a php page to post over to another php page if I need to have the form go to a different page?) instead of the HTML?

Thanks again!
Aaron TomoskySD-WAN SimplifiedCommented:
You dot need a single .HTML page on your entire site.
Hugh McCurdyCommented:
I'm not aware of any issues.  I remember reading (perhaps w3 schools, not sure) one advantage of PHP is that it can both generate the form and also process the form.  It appeared to be the recommendation (the same PHP document both creates the form and processes it).

One major advantage of having them in the same form is handing entry errors.  When telling a user that phone # is required, it is bad form (IMHO) to lose any of the valid information the user entered.  When it is all in one document it is fairly easy to generate the form again but this time with fields populated with the previous entries.

I usually use a variable called $process.  I set it to 0 before processing the $_POST variables.  If one of the $_POST variables is "process" then I set $process to that (as long as the value in the $_POST variable makes sense.)  Then if $process is 1 (or 2 or 3, etc for more complex forms) I know to verify.  The trick is that I put the form generation after the processing code.  So, if the verification fails, I just let the program fall through to form generation.

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
hej613Author Commented:
I tried to give everyone points, Thanks everyone again for all the great information!
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

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.