runnerjp Posted January 29, 2009 Share Posted January 29, 2009 current i have my form <?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'] . "'")); ?> <table width="100%" cellspacing="0" cellpadding="0"> <tr> <td width="10%"> </td> <td width="42%"><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"> <? foreach ($_POST as $field => $value) { if (!$value) { $err .= "$field cannot be empty. <br>"; $warnings[$field] = "required"; } } if (!$_POST["firstname"] || !preg_match("/^[a-zA-Z]+$/", $_POST["firstname"])) { $warnings["firstname"] = " <label for=\"uname\" class=\"error\"><em>*</em>First name can only contain letters</label>"; } if (!$_POST["lastname"] || !preg_match("/^[a-zA-Z]+$/", $_POST["firstname"])) { $warnings["lastname"] = " <label for=\"uname\" class=\"error\"><em>*</em>Last name can only contain letters</label>"; } if ($err) { ?> <div class="errors"> <p align="center"><em>Oops... the following errors were encountered:</em></p> <div align="center"> <ul> <?php echo $err; ?> </ul> </div> <p align="center">Data has <strong>not</strong> been saved.</p> </div> <p> <? if (count($warnings) > 0) { ?> <fieldset> <legend>User Details</legend> <form id="FormName" action='<?php "$_SERVER[php_SELF]" ?>' method="post" name="basic"> <label> <table width="75%" align="left" cellpadding="0" cellspacing="0"> <tr> <td height="30"width="28%"><div align="right">FirstName:</div></td> <td width="2%"> </td> <td width="70%"><input name="firstname" id="firstname" <? if ($warnings['firstname']) echo "class=\"inputerror\""; ?> value="<?php echo $getuserprofile['first_name'] ?>" /> <?php echo $warnings["firstname"]; ?> </td> </tr> <tr> <td height="30"><div align="right">Last Name: </div></td> <td><label></label></td> <td><input type="text" name="lastname" id="lastname" <? if ($warnings['lastname']) echo "class=\"inputerror\""; ?> value="<?php echo $getuserprofile['last_name'] ?>" /> <?php echo $warnings["lastname"]; ?> </td> </tr> <tr> <td height="30"><div align="right">Birthday :</div></td> <td> </td> <td><?php // include the class require "datepicker/class.datepicker.php"; // instantiate the object $dp = new datepicker(); ?> (dd-mm-yyyy) <input id="date" value="<?php echo $getuserprofile['dob'] ?>" /> <input type="button" value="..." onclick="<?= $dp->show("date") ?>" /></td> </tr> <tr> <td height="30"><div align="right">Gender : </div></td> <td> </td> <td><select id="gender" name="gender"> <option value="Male" >Male</option> <option value="Female" >Female</option> </select></td> </tr> <tr> <td height="30"><div align="right">club :</div></td> <td> </td> <td><?php $lines = file('runningclubs.txt'); echo '<select 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 ?>" /></td> </tr> <tr> <td colspan="3"><div align="center"> <input type="submit" class="submit-btn" value="" /> </div></td> </tr> </table> </form> </fieldset> </div> </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> these have validation errors all ready written... the poblem is that the form on shows if there are any validation errors in it due to this <? if (count($warnings) > 0) { ?> how could i show the form and the warnings in i bit of script.... or would i also have to add an else statement and add the form without the error part?? im sure this could be done as just 1 form without the else statement but im unusre how.... Quote Link to comment Share on other sites More sharing options...
runnerjp Posted January 30, 2009 Author Share Posted January 30, 2009 bump Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 1, 2009 Author Share Posted February 1, 2009 can any 1 help today? Quote Link to comment Share on other sites More sharing options...
Snart Posted February 1, 2009 Share Posted February 1, 2009 Why not use the "if (count($warnings) > 0){" part only for those elements which need to be shown and leave the rest of the form outside the {} so it will always be shown? Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 1, 2009 Author Share Posted February 1, 2009 dam good idea lol... the thing is now i have to add data to db... so would i have to do an if statement liek if no errors do this... ?? Quote Link to comment Share on other sites More sharing options...
Snart Posted February 1, 2009 Share Posted February 1, 2009 If adding data depends on the amount of warnings, use something like if (count($warnings) == 0) { //Insert stuff into the db } Quote Link to comment Share on other sites More sharing options...
Snart Posted February 1, 2009 Share Posted February 1, 2009 And to make things even faster (given the assumption you will test the amount of warnings on multiple occasions), store the count only once in a variable and then use type comparison (use 3 equal signs): $count = count($warnings); if($count === 0) { } Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 1, 2009 Author Share Posted February 1, 2009 ok i have dine something like this <?php $count = count($warnings); if($count === 0) { if ($_POST['submit']{ $club = mysql_real_escape_string($_POST['club']); $first_name = mysql_real_escape_string($_POST['firstname']); $last_name = mysql_real_escape_string($_POST['lastname']); $gender = mysql_real_escape_string($_POST['gender']); $birthyear = mysql_real_escape_string($_POST['date']); $update = "UPDATE profile SET dob='$dob', club= '$club', first_name = '$first_name', gender = '$gender', last_name = '$last_name' WHERE id='$id' "; $result = mysql_query($update); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $er = 'Invalid query: ' . mysql_error() . "\n"; $er .= 'Whole query: ' . $query; die($er); }} echo ' <p class="error">' . $message . '</p>' . "\n"; } } } if ($err){?> i get synthax error here though if ($_POST['submit']{ $club = mysql_real_escape_string($_POST['club']); :S Quote Link to comment Share on other sites More sharing options...
Snart Posted February 1, 2009 Share Posted February 1, 2009 if ($_POST['submit'] should be closed with a ) if ($_POST['submit']){ I'd work with array_key_exists though: if(array_key_exists('submit', $_POST)) { } Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 1, 2009 Author Share Posted February 1, 2009 ok you mean liek this <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'] . "'")); ?> <table width="100%" cellspacing="0" cellpadding="0"> <tr> <td width="10%"> </td> <td width="42%"><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"> <? foreach($_POST as $field => $value) { if (!$value) { $err .= "$field cannot be empty. <br>"; $warnings[$field] ="required"; } } if (!$_POST["firstname"] || !preg_match("/^[a-zA-Z]+$/", $_POST["firstname"])) { $warnings["firstname"] = " <label for=\"uname\" class=\"error\"><em>*</em>First name can only contain letters</label>"; } if (!$_POST["lastname"] || !preg_match("/^[a-zA-Z]+$/", $_POST["firstname"])) { $warnings["lastname"] = " <label for=\"uname\" class=\"error\"><em>*</em>Last name can only contain letters</label>"; } $count = count($warnings); if($count === 0) { if(array_key_exists('submit', $_POST)) { $club = mysql_real_escape_string($_POST['club']); $first_name = mysql_real_escape_string($_POST['firstname']); $last_name = mysql_real_escape_string($_POST['lastname']); $gender = mysql_real_escape_string($_POST['gender']); $birthyear = mysql_real_escape_string($_POST['date']); $update = "UPDATE profile SET dob='$dob', club= '$club', first_name = '$first_name', gender = '$gender', last_name = '$last_name' WHERE ID='$id' "; $result = mysql_query($update); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $er = 'Invalid query: ' . mysql_error() . "\n"; $er .= 'Whole query: ' . $query; die($er); }} echo ' <p class="error">' . $message . '</p>' . "\n"; } if ($err){?> <div class="errors"> <p align="center"><em>Oops... the following errors were encountered:</em></p> <div align="center"> <ul> <?php echo $err; ?> </ul> </div> <p align="center">Data has <strong>not</strong> been saved.</p> </div> <p> <?php } ?> <fieldset> <legend>User Details</legend> <form id="submit" action='<?php "$_SERVER[php_SELF]" ?>' method="post" name="submit"> <label> <table width="75%" align="left" cellpadding="0" cellspacing="0"> <tr> <td height="30"width="28%"><div align="right">FirstName:</div></td> <td width="2%"> </td> <td width="70%"><input name="firstname" id="firstname" <? if (count($warnings) > 0){ if ($warnings['firstname']) echo "class=\"inputerror\""; }?> value="<?php echo $getuserprofile['first_name'] ?>" /> <?php if (count($warnings) > 0){ echo $warnings["firstname"];} ?> </td> </tr> <tr> <td height="30"><div align="right">Last Name: </div></td> <td><label></label></td> <td><input type="text" name="lastname" id="lastname" <? if (count($warnings) > 0){ if ($warnings['lastname']) echo "class=\"inputerror\"";} ?> value="<?php echo $getuserprofile['last_name'] ?>" /> <?php if (count($warnings) > 0){ echo $warnings["lastname"];} ?> </td> </tr> <tr> <td height="30"><div align="right">Birthday :</div></td> <td> </td> <td><?php // include the class require "datepicker/class.datepicker.php"; // instantiate the object $dp = new datepicker(); ?> (dd-mm-yyyy) <input id="date" value="<?php echo $getuserprofile['dob'] ?>" /> <input type="button" value="..." onclick="<?= $dp->show("date") ?>" /></td> </tr> <tr> <td height="30"><div align="right">Gender : </div></td> <td> </td> <td><select id="gender" name="gender"> <option value="Male" >Male</option> <option value="Female" >Female</option> </select></td> </tr> <tr> <td height="30"><div align="right">club :</div></td> <td> </td> <td><?php $lines = file('runningclubs.txt'); echo '<select 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 ?>" /></td> </tr> <tr> <td colspan="3"><div align="center"> <input type="submit" class="submit-btn" value="" /> </div></td> </tr> </table> </form> </fieldset> </div> </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> the think is its not inserting anything into the db :S and when u go on the page both firstname and last name are highlighted red even though its correct form the db :S Quote Link to comment Share on other sites More sharing options...
Snart Posted February 1, 2009 Share Posted February 1, 2009 Let's test first if all of the script is executed: Look for echo 'Test1<br />'; echo 'Test2<br />'; echo $update'; in the code below. if($count === 0) { echo 'Test1<br />'; if(array_key_exists('submit', $_POST)) { echo 'Test2<br />'; $club = mysql_real_escape_string($_POST['club']); $first_name = mysql_real_escape_string($_POST['firstname']); $last_name = mysql_real_escape_string($_POST['lastname']); $gender = mysql_real_escape_string($_POST['gender']); $birthyear = mysql_real_escape_string($_POST['date']); $update = "UPDATE profile SET dob='$dob', club= '$club', first_name = '$first_name', gender = '$gender', last_name = '$last_name' WHERE ID='$id' "; echo $update; $result = mysql_query($update); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $er = 'Invalid query: ' . mysql_error() . "\n"; $er .= 'Whole query: ' . $query; die($er); }} echo ' <p class="error">' . $message . '</p>' . "\n"; } Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 1, 2009 Author Share Posted February 1, 2009 intresting i got test 1... so the form is not been submitted ...humm can u see why cos i cant :S Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 1, 2009 Author Share Posted February 1, 2009 ahhh its because i didnt have a name attribute set to submit. now it wont submit as it says submit cannot be empty. :S i know its due to <?php foreach($_POST as $field => $value) { if (!$value) { $err .= "$field cannot be empty. <br>"; $warnings[$field] ="required"; } } ?> but why is it treating it as i field Quote Link to comment Share on other sites More sharing options...
Snart Posted February 1, 2009 Share Posted February 1, 2009 Submit buttons are special creatures. You can filter them out. I also added asnippet to check against values with only whitespace: <?php foreach($_POST as $field => $value) { if (($field != 'submit') && ((!$value) || (trim($value) == ''))) { $err .= "$field cannot be empty. <br>"; $warnings[$field] ="required"; } } ?> Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 1, 2009 Author Share Posted February 1, 2009 ah thanks yes could i ask a few more questions... 1) why is it wheni go onto edit profile it displays the error "first/last name can only contain letters".. even though the name taken from the db is letters? ... 2) how comes my code <?php $lines = file('runningclubs.txt'); echo '<select 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>"; ?> does not show the previous selected club from the db? and when a dob is selcted how comes it does not save to my db :S Quote Link to comment Share on other sites More sharing options...
Snart Posted February 1, 2009 Share Posted February 1, 2009 As for 1): This code apparently results to false: if (!$_POST["firstname"] || !preg_match("/^[a-zA-Z]+$/", $_POST["firstname"])) I wouldn't be surprised if it is the !$_POST["firstname"] part. To see if an array element exists, use array_key_exists() or at least isset() I'll take a look at 2 asap. Quote Link to comment Share on other sites More sharing options...
Snart Posted February 1, 2009 Share Posted February 1, 2009 As for 2): file() will return an array of lines within the file, but I'm pretty sure that the newline characters for each line are preserved. For example: Contents of runningclubs.txt: Club1 Club2 Club3 Then you use foreach ($lines as $line). The first iteration of $line will not be: Club1 but instead, $line will be: Club1\n or perhaps even: Club1\n\r If your clubnames do not contain \ then filter out the newline characters: if ($pclub == trim(str_replace(array('\n', '\r'), '', $line))) Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 1, 2009 Author Share Posted February 1, 2009 As for 1): This code apparently results to false: if (!$_POST["firstname"] || !preg_match("/^[a-zA-Z]+$/", $_POST["firstname"])) I wouldn't be surprised if it is the !$_POST["firstname"] part. To see if an array element exists, use array_key_exists() or at least isset() I'll take a look at 2 asap. ermm where would i stick these... could it not be because i will not have a $_POST array set until i submit the form. i also changed th club code to <?php $lines = file('runningclubs.txt'); echo '<select id="club" name="club">'; foreach ($lines as $line) { if ($pclub == trim(str_replace(array('\n', '\r'), '', $line))) { echo '<option value="' . $line . '" selected="selected">' . $line . '</option>'; } else { echo '<option value="' . $line . '">' . $line . '</option>'; } } echo "</select>"; ?> but with no result... in the text file the code is saved just as club club club(but obviusly the club name) Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 1, 2009 Author Share Posted February 1, 2009 ok the vclub issue is solved... i just noticed it saiod $pclub not $club!! its just the issue with is showin validation error on 1st page without fom submition Quote Link to comment Share on other sites More sharing options...
trq Posted February 1, 2009 Share Posted February 1, 2009 You need to wrap the entire process in a check for submition then. eg; if (isset($_POST['submit'])) { // process form } Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 1, 2009 Author Share Posted February 1, 2009 so ur saying i would have to have 1 form with the validation stuff and and else stament leading to the first form they come accross Quote Link to comment Share on other sites More sharing options...
the182guy Posted February 1, 2009 Share Posted February 1, 2009 You need to wrap the entire process in a check for submition then. eg; if (isset($_POST['submit'])) { // process form } This is not guaranteed to work all of the time. In some browsers, when the user submits the form by pressing the enter key with a field in focus then the submit button element of the $_POST array will not be set and it will skip the form processor code resulting in a confused user. The best way to do it is have a hidden field in the form, and check if that is set instead of checking if the submit button has been clicked. E.g. <input type="hidden" name="submitted" value="1" /> <?php if(isset($_POST['submitted'])) { //process form } ?> Quote Link to comment 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.