Jump to content

Error with form checking


lowe_22

Recommended Posts

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):
Rank
Star Colour
Initials.

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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.