khalek Posted August 16, 2008 Share Posted August 16, 2008 Hello all! I am working on a php code that will accept user input and then search an array for that number. If its there, i want it to display yes, and if not, no. this is the code i have but it will only come up with yes for any number....PLEASE HELP! CODE FOR FORM ON PAGE 1 <form action="Ex4-04-test.php" method="get"> Pick a number between 0 - 50:<br /> <input name="num_wanted" type="text" id="num_wanted" /> <input name="go" type="submit" value="Search for my Number!" /> </form> CODE FOR SEARCHING ARRAY <?php $numWanted = $_POST['num_wanted']; // create an array of numbers called $numArray $numArray = array(12, 0, 89, 64, 23, 50); ?> <?php // use the array_search function to check if the number entered is in the array if(in_array($numWanted, $numArray) ) { echo "YES"; } else { echo "NO"; } ?> if someone could please help me that would be really great Link to comment https://forums.phpfreaks.com/topic/119944-searching-an-array/ Share on other sites More sharing options...
papaface Posted August 16, 2008 Share Posted August 16, 2008 Try <?php $numWanted = trim($_POST['num_wanted']); // create an array of numbers called $numArray $numArray = array(12, 0, 89, 64, 23, 50); ?> <?php // use the array_search function to check if the number entered is in the array if(in_array($numWanted, $numArray) ) { echo "YES"; } else { echo "NO"; } ?> Link to comment https://forums.phpfreaks.com/topic/119944-searching-an-array/#findComment-617902 Share on other sites More sharing options...
khalek Posted August 16, 2008 Author Share Posted August 16, 2008 hmm, didnt work, getting yes for all numbers still Link to comment https://forums.phpfreaks.com/topic/119944-searching-an-array/#findComment-617921 Share on other sites More sharing options...
papaface Posted August 16, 2008 Share Posted August 16, 2008 Your not posting the form data. <form action="Ex4-04-test.php" method="get"> should be <form action="Ex4-04-test.php" method="POST"> Link to comment https://forums.phpfreaks.com/topic/119944-searching-an-array/#findComment-617924 Share on other sites More sharing options...
khalek Posted August 16, 2008 Author Share Posted August 16, 2008 IT WORKS! omg, thankyou soooo much! I have been trying to get this code working all day...its lame it was as simple as that, hehe. Thankyou again! You rock! \m/ Link to comment https://forums.phpfreaks.com/topic/119944-searching-an-array/#findComment-617958 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.