PHP or HTML to create forms????

Posted on 2011-10-07
Last Modified: 2013-12-12
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.

Question by:hej613
    LVL 38

    Assisted Solution

    by:Aaron Tomosky
    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.

    LVL 29

    Assisted Solution

    by:Olaf Doschke
    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.
    LVL 13

    Expert Comment

    by:Hugh McCurdy
    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.
    LVL 13

    Expert Comment

    by:Hugh McCurdy
    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

    LVL 82

    Assisted Solution

    by:Dave Baldwin
    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

    LVL 1

    Author Comment

    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!
    LVL 38

    Expert Comment

    by:Aaron Tomosky
    You dot need a single .HTML page on your entire site.
    LVL 13

    Accepted Solution

    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.
    LVL 1

    Author Closing Comment

    I tried to give everyone points, Thanks everyone again for all the great information!

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
    HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now