oddball25 Posted February 19, 2010 Share Posted February 19, 2010 Hey I have an simple form set up in my 'main.php' page with one field causing me problems which is below: <input id="exact" class="exact" name="exact" size="10"/><input type="submit" name="send" value="ApplyExact" /> So the once the user enters their input in the 'exact' field they submit the form. I have a 'require_once("validate.php"); set up that links a php file which has the following function to validate the input to check if it matches one of the exact terms in either of the 3 arrays: function validateExact($exact){ if(strlen($exact) < 10) return false; if(strlen($exact) >10) return false; $colour_one = array('red','orange','yellow''); $colour_two = array('pink','purple'); $colour_three = array('blue','grey','white'); if ( in_array($exact, $colour_one) ) return true; if ( in_array($exact, $colour_two) ) return true; if ( in_array($exact, $colour_three) ) return true; else return false; } Now back in my 'main.php' page once the submit button has been pressed the user gets taken to the 'ApplyExact' case which has the following code: $_SESSION['exact'] = $_POST['exact']; if( isset($_POST['exact']) && (!validateExact($_POST['exact']))) { echo ('<tr><td><td>The Colour is Invalid</td></tr><tr><tr><td>Colour<input id="exact" class="exact" name="exact" size="10"/><input type="submit" name="send" value="ApplyExact" /></td></tr>'); } else { echo ('<tr><td>Your Colour is '.$_SESSION['exact'].' and is valued at '.$type.'</td></tr>'); } Now this all works fine and i get the users colour choice echoed back if it is stored in one of the arrays but the problem i have is that i cannot get the variable of '$type' to be displayed. I need to set a variable of 'type' - which value depends on what colour array the user selected. So for example if they enter any of red, orange or yellow from the array '$colour_one' then $type = .30. If they enter pink or purple from the array '$colour_two' then $type = .50 and so on... I have tried numerous different attempts at getting this working including the below which i thought should work but isn't. Are the 2 examples below completely wrong and i need another way around this or am i missing something? ...if ( in_array($exact, $colour_one) ) return true; $type = .30; if ( in_array($exact, $colour_two) ) return true; $type = .50; if ( in_array($exact, $colour_three) ) return true; $type = .60; else return false; } or by: ...if ( in_array($exact, $colour_one) ) $type = .30; $true = true && $type; return $true; if ( in_array($exact, $colour_two) ) $type = .50; $true = true && $type; return $true; if ( in_array($exact, $colour_three) ) $type = .60; $true = true && $type; return $true; else return false; } If anyone can help with this? I have only posted the sections of the code that relate to my problem and question as the whole script for 'main.php' is about 1200 lines long with 'validate.php' 140 lines so tried to make it a bit clearer and easier. Many thanks Jon Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 19, 2010 Share Posted February 19, 2010 The specific problem is variable scope. A variable defined inside a function only has that value inside the function. There are ways around this with globals, but that is typically a bab solution. In this situation you are returning true or false. Just change that logic to return false or the $type value. Although I see problems with that function aside from that. At the beginning of the validation function you test to ensure the length is exactly 10 characters. I would first ask why you would use to tests to check for less than or greater than instead of just a single check to see if it is not 10, but there are two other problems with that logic. 1) The value needs to match one of the values in the arrays, but none of the values to match or 10 characters, so I would presume those tests will always fail. Plus, why do a length check at all since you are testing too see if the valuse match a defines list of values? Use this for the validation (notice change in function name) function getExactType($exact) { $colors = array( '.30' => array('red','orange','yellow'), '.50' => array('pink','purple'), '.60' => array('blue','grey','white') ); //Check against each array foreach ($colors as $type => $typeColors) { if (in_array($exact, $typeColors)) { //There was a match, return the type return $type; } } //There were no matches return false; } Also, I see a logic problem with this IF statement $_SESSION['exact'] = $_POST['exact']; if( isset($_POST['exact']) && (!validateExact($_POST['exact']))) { echo ('<tr><td><td>The Colour is Invalid</td></tr><tr><tr><td>Colour<input id="exact" class="exact" name="exact" size="10"/><input type="submit" name="send" value="ApplyExact" /></td></tr>'); } else { echo ('<tr><td>Your Colour is '.$_SESSION['exact'].' and is valued at '.$type.'</td></tr>'); } You first set a session value based on the POST value THEN you check if the POST value is set. Plus, you are assuming if the IF conditin fails that validation passed. But, if the POST value isn't set that IF statement fails, but the code assumes validation passed. I would correct that like this: if(isset($_POST['exact'])) { $color = $_POST['exact']; $_SESSION['exact'] = $color $type = getExactType($color); if($type===false) { echo ('<tr><td><td>The Colour is Invalid</td></tr><tr><tr><td>Colour<input id="exact" class="exact" name="exact" size="10"/><input type="submit" name="send" value="ApplyExact" /></td></tr>'); } else { echo "<tr><td>Your Colour is {$color} and is valued at {$type}</td></tr>\n"; } } 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.