aeafisme23 Posted October 31, 2006 Share Posted October 31, 2006 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 More sharing options...
alpine Posted October 31, 2006 Share Posted October 31, 2006 [code]<?phpif(empty($_POST['username'])){echo "username is empty";}if(!empty($_POST['username'])) // note the !{echo "username is not empty";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/25719-server-side-form-validation/#findComment-117397 Share on other sites More sharing options...
aeafisme23 Posted October 31, 2006 Author Share Posted October 31, 2006 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] Link to comment https://forums.phpfreaks.com/topic/25719-server-side-form-validation/#findComment-117402 Share on other sites More sharing options...
alpine Posted October 31, 2006 Share Posted October 31, 2006 Example:[code]<?phpif(isset($_POST['submit'])){if(!empty($_POST['username'])){$username = htmlspecialchars($_POST['username']);// username is not emptyif(!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] Link to comment https://forums.phpfreaks.com/topic/25719-server-side-form-validation/#findComment-117410 Share on other sites More sharing options...
aeafisme23 Posted October 31, 2006 Author Share Posted October 31, 2006 THANK YOU :) Link to comment https://forums.phpfreaks.com/topic/25719-server-side-form-validation/#findComment-117414 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.