Jump to content

Not identifying a positive value in the array from a form....


TeroYukio

Recommended Posts

Okay so here is my problem, I have an error to be set if the file type isn't specified.

if ($type == 0 || !in_array($type,array('sounds','textures','ui'))) {
            $errors[] = "Please select a valid type!";
        }

 

Seems right...right?  Well it gets $type from my form:

    echo "<tr><td>Type</td><td><select name=\"type\">
					<option value=\"0\">--Select One--</option>
					<option value=\"sounds\">Sound Mod</option>
					<option value=\"textures\">Texture Mod</option>
					<option value=\"ui\">User Interface Mod</option>
				   </select></td></tr>\n";

 

The problem is I choose Sound Mod from the options and the error pops up after I click submit saying I didn't specify or choose a type of file.  Anyone know why?

$type = $_POST['type'];

 

 

The problem is because you are comparing $type with a numeric value using a loose comparison (==.) Php converts the value in $type to a number in this case, which results in a zero being used for the comparison.

 

You actually only need to use if(!in_array($type,array('sounds','textures','ui')) because the zero value is not in that array as well.

Archived

This topic is now archived and is closed to further replies.

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