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