Jump to content

Server Side Form Validation


aeafisme23

Recommended Posts

Everything is working 100% full functional. I have not seen many PHP scripts like this because for some reason everyone keeps using stupid Client Side JS and you have the annoying pop ups. So I have ripped some code and manipulated this a little, but for some reason I cant figure out how to do the same to the USERNAME field to validate that its not null, can anyone help. Heres the code below:
[code]<?PHP
    $message    = "";
    $csserrorfont = "basictext";
    $username  = "";

    if ($_POST['process'] == 1) {

        $pattern = '/.*@.*\..*/';
        $email  = $_POST['email'];
        $urlname = urlencode($$_POST['username']);

        if (preg_match($pattern, $_POST['email']) > 0) {
            // Here's where you would store
            // the data in a database...
            header(
              "location: thankyouemail.php?&username=$urlname");
        }
        $message    = "Please enter a valid email address.";
        $username  = $_POST['name'];
        $csserrorfont = "errortext";
    }
?>

<html>
<style>
    .basictext {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 14px; color:#000066;
    }
    .errortext {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 14px; color:#000066; font-weight: bold;
    }
</style>
<body>

<form action="emailtest.php" method="post">
<table width="520" cellpadding="2" cellspacing="0" border="1" bordercolor="#000000">
<tr>
<td width="100" valign="top"><p><span class="<?php print $csserrorfont; ?>">Email:</span></p></td>
<td width="200" valign="top"><input name="email" type="text" maxlength="50" size="25" class="<? print $csserrorfont; ?>"></td>
<td width="220" valign="top"><?php if ($message != "") { print '<span class=\"errortext\">'.$message."</span>";} ?>
<span class="<?php print $csserrorfont; ?>"> </td>
</tr>
<tr>
<td width="100" valign="top"><p><span class="basictext">First Name:</span></p></td>
    <td width="200" valign="top"><input name="username" type="text" class="basictext" value="<?php print $username; ?>"></td>
<td width="220">validation</td>
    </tr>
<tr>
<td colspan="3" valign="top">
<input type="hidden" name="process" value="1">
    <input type="submit" name="Button1" value="submit"></td>
</tr>
</table>
</form>

</body></html>[/code]
Link to comment
https://forums.phpfreaks.com/topic/25719-server-side-form-validation/
Share on other sites

Thank You for the first step. That works correctly but offers no "validation", do i need to make another hidden input box and make a 2nd process, or can i validate with in the email since its saying if valid redirect to thankyou.php.

code up to now with above implementation:

[code]<?PHP
    $message    = "";
    $csserrorfont = "basictext";
    $username  = "";

    if ($_POST['process'] == 1) {

        $pattern = '/.*@.*\..*/';
        $email  = $_POST['email'];
        $urlname = urlencode($$_POST['username']);

        if (preg_match($pattern, $_POST['email']) > 0) {
            // Here's where you would store
            // the data in a database...
            header(
              "location: thankyouemail.php?&username=$urlname");
        }
        $message    = "Please enter a valid email address.";
        $username  = $_POST['name'];
        $csserrorfont = "errortext";
    }
?>


<html>
<style>
    .basictext {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 14px; color:#000066;
    }
    .errortext {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 14px; color:#000066; font-weight: bold;
    }
</style>
<body>

<form action="emailtest.php" method="post">
<table width="520" cellpadding="2" cellspacing="0" border="1" bordercolor="#000000">
<tr>
<td width="100" valign="top"><p><span class="<?php print $csserrorfont; ?>">Email:</span></p></td>
<td width="200" valign="top"><input name="email" type="text" maxlength="50" size="25" class="<? print $csserrorfont; ?>"></td>
<td width="220" valign="top"><?php if ($message != "") { print '<span class=\"errortext\">'.$message."</span>";} ?>
<span class="<?php print $csserrorfont; ?>"> </td>
</tr>
<tr>
<td width="100" valign="top"><p><span class="basictext">First Name:</span></p></td>
    <td width="200" valign="top"><input name="username" type="text" class="basictext" value="<?php print $username; ?>"></td>
<td width="220">
<?php
if(empty($_POST['username']))
{
echo "error";
}


if(!empty($_POST['username'])) // note the !
{
echo "";
}

?></td>
    </tr>
<tr>
<td colspan="3" valign="top">
<input type="hidden" name="process" value="1">
    <input type="submit" name="Button1" value="submit"></td>
</tr>
</table>
</form>

</body></html>[/code]
Example:
[code]

<?php

if(isset($_POST['submit']))
{
if(!empty($_POST['username']))
{
$username = htmlspecialchars($_POST['username']);
// username is not empty

if(!empty($_POST['email']))
{
$email = htmlspecialchars($_POST['email']);
// email is not empty

// OK, at this point both email and username exists - proceed
}
else
{
// empty email, print out error or define error message
}
}
else
{
// empty username, print out error or define error message
}
}

echo <<<_HTML

<form method="post" action="this.php">
<input type="text" name="username" value="$username" />
<input type="text" name="email" value="$email" />
<input type="submit" name="submit" value="Send It" />
</form>

_HTML;

?>

[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.