runnerjp Posted January 28, 2009 Share Posted January 28, 2009 here is my form <?php foreach($_POST as $field => $value) { if (!$value) { $err .= "$field is a required field <br>"; $warnings[$field] ="required"; } } if ($_POST["firstname"] && !ereg("^A-Za-z", $_POST["firstname"])) $warnings["firstname"] = "Must be letters only"; if ($err){ echo $err; if (count($warnings) > 0) { ?> <form id="FormName" action='<?php "$_SERVER[php_SELF]" ?>' method="post" name="basic"> Name : <label>First: <input type="text" name="firstname" id="firstname" value="<?php echo $getuserprofile['first_name'] ?>" /> <?php echo $warnings["firstname"]; ?> </label> <label>Last: <input type="text" name="lastname" id="lastname" value="<?php echo $getuserprofile['last_name'] ?>" /> </label> <br /> Birthday : <?php // include the class require "datepicker/class.datepicker.php"; // instantiate the object $dp = new datepicker(); ?> (dd-mm-yyyy) <input type="text" id="date" value="<?php echo $getuserprofile['dob'] ?>"> <input type="button" value="..." onclick="<?= $dp->show("date") ?>"> <br /> Gender : <select id="gender" name="gender"> <option value="Male" >Male</option> <option value="Female" >Female</option> </select><br /> club :<?php $lines = file('runningclubs.txt'); echo '<select class="inputedit" id="club" name="club">'; foreach ($lines as $line) { if ($pclub == trim($line)) { echo '<option value="' . $line . '" selected="selected">' . $line . '</option>'; } else { echo '<option value="' . $line . '">' . $line . '</option>'; } } echo "</select>"; }} ?> <input type="hidden" name="id" value="<?php echo $id ?>"> <input type="submit" class="submit-btn" value=""> </form> basicly im trying to do to things 1) check fild is filled in! 2) check a field is filled in correctly! thing is the filled in works (can it be improved or is the simple method fine?). But it awayls displays $warnings["firstname"] = "Must be letters only"; even if letter are just entred ?? Quote Link to comment https://forums.phpfreaks.com/topic/142787-solved-form-validation/ Share on other sites More sharing options...
gevans Posted January 28, 2009 Share Posted January 28, 2009 try your ereg like this... <?php foreach($_POST as $field => $value) { if (!$value) { $err .= "$field is a required field <br>"; $warnings[$field] ="required"; } } if ($_POST["firstname"] && !ereg("/^[a-zA-Z]+$/", $_POST["firstname"])) $warnings["firstname"] = "Must be letters only"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/142787-solved-form-validation/#findComment-748402 Share on other sites More sharing options...
runnerjp Posted January 28, 2009 Author Share Posted January 28, 2009 humm saldy not...for some reaosn it just displays it as if it wherre normal text ( meaning all the time) Quote Link to comment https://forums.phpfreaks.com/topic/142787-solved-form-validation/#findComment-748410 Share on other sites More sharing options...
gevans Posted January 28, 2009 Share Posted January 28, 2009 I jsut realise you're looking for a FALSE return, you have the wrong boolean value, get rid of the exclamation mark. Also look into preg_match() as ereg() will be gone by php6 Quote Link to comment https://forums.phpfreaks.com/topic/142787-solved-form-validation/#findComment-748415 Share on other sites More sharing options...
runnerjp Posted January 28, 2009 Author Share Posted January 28, 2009 humm wired it still does not work even with <?php if ($_POST["firstname"] && preg_match("/^[a-zA-Z]+$/", $_POST["firstname"])) $warnings["firstname"] = "Must be letters only";?> Quote Link to comment https://forums.phpfreaks.com/topic/142787-solved-form-validation/#findComment-748420 Share on other sites More sharing options...
gevans Posted January 28, 2009 Share Posted January 28, 2009 if the following doesn't work then there's something wierd going on coz I just tested it!! <?php $fname = $_POST["firstname"]; if(preg_match("/^[A-Za-z]+$/", $fname)) echo 'good'; else echo 'bad'; Quote Link to comment https://forums.phpfreaks.com/topic/142787-solved-form-validation/#findComment-748425 Share on other sites More sharing options...
runnerjp Posted January 28, 2009 Author Share Posted January 28, 2009 humm so there seems tobe anouther error in it all :S could be a closing } wrong somewhere i suppose Quote Link to comment https://forums.phpfreaks.com/topic/142787-solved-form-validation/#findComment-748429 Share on other sites More sharing options...
gevans Posted January 28, 2009 Share Posted January 28, 2009 Post your whole code Quote Link to comment https://forums.phpfreaks.com/topic/142787-solved-form-validation/#findComment-748434 Share on other sites More sharing options...
runnerjp Posted January 28, 2009 Author Share Posted January 28, 2009 <link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/css/login.css"> <link rel="stylesheet" type="text/css" href="include/profiletab/ajaxtabs/ajaxtabs.css"> <script type="text/javascript" src="include/profiletab/ajaxtabs/ajaxtabs.js"></script> <?php require_once '../settings.php'; checkLogin('1 2'); include "../info.php"; // sets username/id ect include "../getuser.php"; // records user view on page $getuser = mysql_fetch_assoc(mysql_query("SELECT * from users where Username='$username'")); $getuserprofile = mysql_fetch_assoc(mysql_query("SELECT * from profile where ID='" . $getuser['ID'] . "'")); ?> <div align="center"><?php echo $getuserprofile['club'] ?>Please edit you prfile here admin</div> <table width="100%" cellspacing="0" cellpadding="0"> <tr> <td> </td> </tr> </table> <table width="100%" cellspacing="0" cellpadding="0"> <tr> <td width="10%"> </td> <td width="80%"><ul id="countrytabs" class="shadetabs"> <li><a href="#" rel="#default" class="selected">About me</a></li> <li><a href="include/profiletab/edit1.htm" rel="countrycontainer">Hobbies</a></li> <li><a href="include/profiletab/edit2.htm" rel="countrycontainer">Goals</a></li> <li><a href="include/profiletab/edit3.htm" rel="#iframe">Films and Books</a> </li> </ul> <div id="countrydivcontainer" style="border:1px solid gray; width:800px; margin-bottom: 1em; padding: 10px"> <p> <? foreach($_POST as $field => $value) { if (!$value) { $err .= "$field is a required field <br>"; $warnings[$field] ="required"; } } if ($_POST["firstname"] && preg_match("/^[a-zA-Z]+$/", $_POST["firstname"])) $warnings["firstname"] = "Must be letters only"; if ($err){ echo $err; if (count($warnings) > 0) { ?> <form id="FormName" action='<?php "$_SERVER[php_SELF]" ?>' method="post" name="basic"> Name : <label>First: <input type="text" name="firstname" id="firstname" value="" /> <?php echo $warnings["firstname"]; ?> </label> <label>Last: <input type="text" name="lastname" id="lastname" value="<?php echo $getuserprofile['last_name'] ?>" /> </label> <br /> Birthday : <?php // include the class require "datepicker/class.datepicker.php"; // instantiate the object $dp = new datepicker(); ?> (dd-mm-yyyy) <input type="text" id="date" value="<?php echo $getuserprofile['dob'] ?>"> <input type="button" value="..." onclick="<?= $dp->show("date") ?>"> <br /> Gender : <select id="gender" name="gender"> <option value="Male" >Male</option> <option value="Female" >Female</option> </select><br /> club :<?php $lines = file('runningclubs.txt'); echo '<select class="inputedit" id="club" name="club">'; foreach ($lines as $line) { if ($pclub == trim($line)) { echo '<option value="' . $line . '" selected="selected">' . $line . '</option>'; } else { echo '<option value="' . $line . '">' . $line . '</option>'; } } echo "</select>"; }} ?> <input type="hidden" name="id" value="<?php echo $id ?>"> <input type="submit" class="submit-btn" value=""> </form> </td> <td width="10%"> </td> </tr> </table> <script type="text/javascript"> var countries=new ddajaxtabs("countrytabs", "countrydivcontainer") countries.setpersist(true) countries.setselectedClassTarget("link") //"link" or "linkparent" countries.init() </script> Quote Link to comment https://forums.phpfreaks.com/topic/142787-solved-form-validation/#findComment-748442 Share on other sites More sharing options...
gevans Posted January 28, 2009 Share Posted January 28, 2009 here we go <link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/css/login.css"> <link rel="stylesheet" type="text/css" href="include/profiletab/ajaxtabs/ajaxtabs.css"> <script type="text/javascript" src="include/profiletab/ajaxtabs/ajaxtabs.js"></script> <?php require_once '../settings.php'; checkLogin('1 2'); include "../info.php"; // sets username/id ect include "../getuser.php"; // records user view on page $getuser = mysql_fetch_assoc(mysql_query("SELECT * from users where Username='$username'")); $getuserprofile = mysql_fetch_assoc(mysql_query("SELECT * from profile where ID='" . $getuser['ID'] . "'")); ?> <div align="center"><?php echo $getuserprofile['club'] ?>Please edit you prfile here admin</div> <table width="100%" cellspacing="0" cellpadding="0"> <tr> <td> </td> </tr> </table> <table width="100%" cellspacing="0" cellpadding="0"> <tr> <td width="10%"> </td> <td width="80%"><ul id="countrytabs" class="shadetabs"> <li><a href="#" rel="#default" class="selected">About me</a></li> <li><a href="include/profiletab/edit1.htm" rel="countrycontainer">Hobbies</a></li> <li><a href="include/profiletab/edit2.htm" rel="countrycontainer">Goals</a></li> <li><a href="include/profiletab/edit3.htm" rel="#iframe">Films and Books</a> </li> </ul> <div id="countrydivcontainer" style="border:1px solid gray; width:800px; margin-bottom: 1em; padding: 10px"> <p> <? foreach($_POST as $field => $value) { if (!$value) { $err .= "$field is a required field <br>"; $warnings[$field] ="required"; } } if (!$_POST["firstname"] || !preg_match("/^[a-zA-Z]+$/", $_POST["firstname"])) { $warnings["firstname"] = "Must be letters only"; } if ($err){ echo $err; if (count($warnings) > 0) { ?> <form id="FormName" action='<?php "$_SERVER[php_SELF]" ?>' method="post" name="basic"> Name : <label>First: <input type="text" name="firstname" id="firstname" value="" /> <?php echo $warnings["firstname"]; ?> </label> <label>Last: <input type="text" name="lastname" id="lastname" value="<?php echo $getuserprofile['last_name'] ?>" /> </label> <br /> Birthday : <?php // include the class require "datepicker/class.datepicker.php"; // instantiate the object $dp = new datepicker(); ?> (dd-mm-yyyy) <input type="text" id="date" value="<?php echo $getuserprofile['dob'] ?>"> <input type="button" value="..." onclick="<?= $dp->show("date") ?>"> <br /> Gender : <select id="gender" name="gender"> <option value="Male" >Male</option> <option value="Female" >Female</option> </select><br /> club :<?php $lines = file('runningclubs.txt'); echo '<select class="inputedit" id="club" name="club">'; foreach ($lines as $line) { if ($pclub == trim($line)) { echo '<option value="' . $line . '" selected="selected">' . $line . '</option>'; } else { echo '<option value="' . $line . '">' . $line . '</option>'; } } echo "</select>"; }} ?> <input type="hidden" name="id" value="<?php echo $id ?>"> <input type="submit" class="submit-btn" value=""> </form> </td> <td width="10%"> </td> </tr> </table> <script type="text/javascript"> var countries=new ddajaxtabs("countrytabs", "countrydivcontainer") countries.setpersist(true) countries.setselectedClassTarget("link") //"link" or "linkparent" countries.init() </script> Quote Link to comment https://forums.phpfreaks.com/topic/142787-solved-form-validation/#findComment-748445 Share on other sites More sharing options...
runnerjp Posted January 28, 2009 Author Share Posted January 28, 2009 thnaks can i ask what was wrong? Quote Link to comment https://forums.phpfreaks.com/topic/142787-solved-form-validation/#findComment-748450 Share on other sites More sharing options...
gevans Posted January 28, 2009 Share Posted January 28, 2009 I just realise you're looking for a FALSE return, you have the wrong boolean value, get rid of the exclamation mark. Basically instead of checking if it did have letters you were checking if it did. So.... Correct name -> return error Wrong name -> run script Quote Link to comment https://forums.phpfreaks.com/topic/142787-solved-form-validation/#findComment-748453 Share on other sites More sharing options...
runnerjp Posted January 28, 2009 Author Share Posted January 28, 2009 haha ooops .. thankyou for sloving this ! Quote Link to comment https://forums.phpfreaks.com/topic/142787-solved-form-validation/#findComment-748459 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.