soma56 Posted December 29, 2010 Share Posted December 29, 2010 I know this works and that's why I'm puzzled. I'm bringing in data from a textarea and trying to compare it. index.php <form name"fruit-farm" action="fruit-check.php" method="POST"> <textarea name="fruit-list"> Apples Bananas Oranges Pickles Hamburgers Grapes </textarea> <input type"submit" name="submit" value="Check The Fruit"> </form> fruit-check.php if (isset($_POST['submit'])) { //Bring in the data $fruit-list = explode("\n", $_POST['fruit-list']); //Search for Hamburgers $hamburgers = "Hamburgers"; if (in_array($hamburgers,$fruit-list)){ echo "That is not a fruit"; } } What's puzzling me is that it's not working. It must be something blantently obvious. It has something to do with the '$fruit-list' array and how it is being brought into the form. It posts fine. The reason I think this is the case is because if I simply create an array that is identical to the form coming in it works: if (isset($_POST['Submit'])) { //Bring in the data $fruit-list = explode("\n", $_POST['fruit-list']); //Search for Hamburgers $hamburders = "Hamburgers"; $fruit-list = array('Apples', 'Bananas', 'Oranges', 'Pickles', 'Hamburgers', 'Grapes); if (in_array($hamburders,$fruit-list)){ echo "That is not a fruit"; } } When I 'print_r' both arrays they both look identical. What is wrong and/or different with the way I'm bringing in the textarea that's causing this not to work? Quote Link to comment https://forums.phpfreaks.com/topic/222873-in_array-is-puzzling-me/ Share on other sites More sharing options...
Zurev Posted December 29, 2010 Share Posted December 29, 2010 Might be a long shot, but try renaming the variable without a hyphen. Also may want to try trimming, since I suppose the \n could be farther along. Quote Link to comment https://forums.phpfreaks.com/topic/222873-in_array-is-puzzling-me/#findComment-1152435 Share on other sites More sharing options...
trq Posted December 29, 2010 Share Posted December 29, 2010 It's not a long shot. Variables cannot have hyphens in there names. You should have error reporting set to E_ALL and display errors on while developing. This code would generate an error. Quote Link to comment https://forums.phpfreaks.com/topic/222873-in_array-is-puzzling-me/#findComment-1152437 Share on other sites More sharing options...
soma56 Posted December 29, 2010 Author Share Posted December 29, 2010 Thanks guys. Ironically there is not hyphens - I just renamed everything to present a simply and logical example for everyone to see. In any event you got me thinking about trimming the whitespace which led to the discovery of a solution. Instead of exploding the array with '\n' I tried '\r' which magically solved the problem Quote Link to comment https://forums.phpfreaks.com/topic/222873-in_array-is-puzzling-me/#findComment-1152439 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.