lowe_22 Posted November 4, 2006 Share Posted November 4, 2006 Right, I've written script that is basically a simple error checking system for a webform that allows a user to enter a new member into a database.The form consits of three imptortant fields (and several others that are irrelevant to this problem):RankStar ColourInitials.The member may only have a star colour and initials if he is of a certain rank or higher. Therefore, when checking the form, if the selected rank is not high enough, and star colour or initials have been selected, an error message occurs. Similarly if the rank is high enough, but star colour and initials have not been selected, an error occurs.Ok?Right i think my problem comes when the user actually does things right! i.e.they slecet a high enough rank and a star colour, yet for some reason the star colour does not seem to be assigning to the variable I have set for it.I have checkd this by printing the variable assigned to each field. Here is the relevent bits of code:[code]<?php// Define Rank arrays.$highranks[] = "ES*";$highranks[] = "ES**";$highranks[] = "SM*";$highranks[] = "SM**";$highranks[] = "SL*";$highranks[] = "SL**";$highranks[] = "SS*";$highranks[] = "SS**";$highranks[] = "SC*";$highranks[] = "SC**";$highranks[] = "SCF";$highranks[] = "RCM";$highranks[] = "SOL";if ($_POST['btnAdd']) { // Make sure form has been posted before error checking! $nickname=$_POST['nickname']; $email=$_POST['email']; $realname=$_POST['realname']; $master=$_POST['master']; $rank=$_POST['rank']; $starcolor=$_POST['starcolor']; $initials=$_POST['initials'];$error = FALSE; // check up variable$err_nickname = FALSE;$err_rank = FALSE;$err_email = FALSE;$err_master = FALSE;$err_starcolor = FALSE;$err_initials = FALSE;// Check for correct input in star color and initials //starcolor if(!in_array($rank, $highranks) and ($starcolor!="-- No Color --")) { $error=true; $err_starcolor="The member is not of sufficient rank for a star color!"; } // Makes sure that user hasnt selected a star color when member isnt of high enough rank. if ((in_array($rank, $highranks)) and ($starcolor="-- No Color --")){ $error=true; $err_starcolor="Please select a star color!"; } //initials if(!in_array($rank, $highranks) and (!empty($initials))) { $error=true; $err_initials="The member is not of sufficient rank for initials!";}if ((in_array($rank, $highranks)) and (empty($initials))){ $error=true; $err_initials="<span class=error>Please enter some initials!</span>";}// the form... ?><form name="form2" action="<?=$_SERVER['PHP_SELF']?>" method="POST" class="register-form" id="form1"><div> <label for="rank"><strong>Rank*</strong></label> <select name="rank"> <?php if(isset($err_rank)){ echo "<option value=$rank selected=selected>$rank</option>"; }else{ echo "<option value=\"-- Select a Rank --\" selected=\"selected\">-- Select a Rank --</option>"; } ?> <?php foreach($rankslist as $val) { echo "<option value=" . $val . ">" . $val . "</option>"; } ?> <option value="-- Special Ranks --" disabled=disabled>-- Special Ranks --</option> <?php foreach($specialrankslist as $specialval) { echo "<option value=" . $specialval . ">" . $specialval . "</option>"; } ?> </select> <?php echo "<span class=error>".$err_rank."</span>"; ?></div> <div> <br /> <p>Please Note: The following feilds are only required if the members rank is ES* or above.</p> <br /> <div> <label for="starcolor"><strong>Star Color*</strong></label> <select name="starcolor"> <?php if(isset($err_rank)){ echo "<option value=$starcolor selected=selected>$starcolor</option> <option value=\"------\" disabled=disabled>-----</option> <option value=\"-- No Color --\">-- No Color --</option>"; }else{ echo "<option value=\"-- No Color --\" selected=\"selected\">-- No Color --</option>"; } ?> <? foreach($starlist as $val) { echo "<option value=\"" . $val . "\">" . $val . "</option>"; } ?> </select> <?php echo "<span class=error>".$err_starcolor."</span>"; ?> </div> <div> <label for="initials"><strong>Initials*</strong></label> <input type="text" name="initials" id="initials" class="text" value="<?=$initials?>" /> <?php echo "<span class=error>" . $err_initials . "</span>"; ?> </div> <input type="submit" name="btnAdd" class="btn" value="Add"> </div> </form>[/code]I am at a loss as to what the problem could be. As far as I'm aware, the error checking "if" statements are correct and are checking the right values... I just dont know.Any help? Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 4, 2006 Share Posted November 4, 2006 This problem was posted previously and I believe I gave you the same correction there as I am going to give you here. Please refrain from double posting.For the specific problem you are having the problem is most likely this line:[code]if ((in_array($rank, $highranks)) and ($starcolor="-- No Color --")){[/code]1) You should use '&&' not 'and'2) In the second conditional you have [b]$starcolor="-- No Color --"[/b]. That is NOT checking to see if $starcolor is equal to "--No Color--" it is assigning it that value! You need to use a double equal sign to test a value.So the line should be more like this:[code]if ((in_array($rank, $highranks)) && $starcolor=="-- No Color --") {[/code]As far as other problems:[code]echo "<option value=$starcolor selected=selected>$starcolor</option>[/code]1) "selected=selected" is not correct. It is just "selected". Same with disabled=disabled.2) When assigning the value to $starcolor you are not enclosing the value in quotes. If the value has a space it will not be correct. You should always enclose parameter values in quotes - especially when assigning them dynamically. Quote Link to comment Share on other sites More sharing options...
lowe_22 Posted November 4, 2006 Author Share Posted November 4, 2006 Ahhh... Cheers. The previous post I made regarding this script was with a different aspect, and so i felt I needed to post again as it was a separate problem.Many thanks for saving my ass twice! Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 5, 2006 Share Posted November 5, 2006 No problem. The main thing is that we get the issue solved. 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.