Web forms and PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tvollmer
Forum Newbie
Posts: 1
Joined: Wed Jan 15, 2003 12:11 pm

Web forms and PHP

Post by tvollmer »

I have crated a web form that sends it's data to a PHP script for handling.

I would like to know how I can refresh the form, KEEPING the good values and deleting the bad values (ones that did not pass validation).

Right now, if someone submits the form, and there are errors in it, the form will reload, but ALL of the form fields will be empty.

I would like to be able to return to the form, display an error message, and have all of the data the person entered to still be in the form (except for the bad fields).

Any help would be appreciated.

Todd
User avatar
skehoe
Forum Commoner
Posts: 59
Joined: Sun Dec 22, 2002 5:57 am
Location: Denver

Post by skehoe »

You could do something like this...

Code: Select all

<?
function check_form($form_data) &#123;
    $errors = NULL;
    $required = array('fromName','email');
    foreach ($required as $key => $val) &#123;
        $form_data&#1111;$val] = trim($form_data&#1111;$val]);
        if ( ($form_data&#1111;$val] == NULL) || ($form_data&#1111;$val] == '') ) &#123;
            $errors .= "$val~";
        &#125;
    &#125;
    return $errors;
&#125;

function check_email($email) &#123;
    if(!ereg("^&#1111;_\.''0-9a-z-]+@(&#1111;0-9a-z]&#1111;0-9a-z-]+\.)+&#1111;a-z]&#123;2,3&#125;$",$email)) &#123;
        $error = "bad email";
        return $error;
    &#125; else &#123;     
        return NULL;
    &#125;
&#125;

display_form($form_data, $form_errors) &#123;
    echo '<form name="form" action="$PHP_SELF" method="post">'
    if ($mail_error != '') &#123;
        echo "<font color="red"><center><h3>Email Error</font></h3><br>";
        echo $mail_error."<br>";
    &#125;
    if ($form_errors != '') &#123;
        echo "<font color="red"><center><h3>Please complete the required fields.</font></h3><br>";
    &#125;       
    if (ereg('fromName', $form_errors)) &#123; echo '<font color="red">'; &#125;
?>   
        <b><font color="#0033FF">*</font>Name:</b></td>
        <td colspan="1" align="left">    
        <input name="form_data&#1111;fromName]" type="text" size="50"
<?
    if (isset($form_data&#1111;'fromName'])) &#123;
        echo "value="".$form_data&#1111;'fromName']."">";
    &#125; else &#123;
        echo ">";
    &#125;
        if (ereg('email', $form_errors)) &#123; echo '<font color="red">'; &#125;
?>
        <b><font color="#0033FF">*</font>Email:</b></td>
        <td colspan="1" align="left">
        <input name="form_data&#1111;email]" type="text" size="50"
<?
    if (isset($form_data&#1111;'email'])) &#123;
        echo "value="".$form_data&#1111;'email']."">";
    &#125; else &#123;
        echo ">";
    &#125;
    echo "</form>";
&#125;

// Main

if (isset($_POST&#1111;'form_data']&#1111;'submit'])) &#123;
    $form_data = $_POST&#1111;'form_data'];
    $form_errors  = check_form($form_data);
    $form_errors .= check_email($form_data&#1111;'email']);
    if ($form_errors != '') &#123;
        display_form($form_data, $form_errors, NULL);
    &#125; else &#123;
        // do something...
    &#125;
&#125; else &#123;
    display_form($form_data, NULL, NULL);
&#125;
?>
Post Reply