davefootball123 Posted March 14, 2013 Share Posted March 14, 2013 Hi all. I have an HTML Input array seen below. I am using a php script to get the checked values. I am using the foreach method to get the values of the input array. How would I compare the values of the input array from the html to an array in my php script? My current php script is shown below. As you can see the array has the values of the input array...as well as the corresponding text value that would be echoed by my script. How would I compare the input array that I used $_POST['calltotor'] to retrieve to the array seen in the php script to get the corresponding value. Any help would be great. Thanks, <div id="tornado" style="display:none; height:auto; text-align:left;"> <input type="checkbox" name="calltotor[]" value="defaulttor"> Default Tornado Warning<br> <input type="checkbox" name="calltotor[]" value="mobilehome"> Mobile Home<br> <input type="checkbox" name="calltotor[]" value="motorists"> Motorists<br> <input type="checkbox" name="calltotor[]" value="outside"> If Caught Outside<br> <input type="checkbox" name="calltotor[]" value="basement"> Get to Basement<br> <input type="checkbox" name="calltotor[]" value="lifethreatening"> Life Threatening Tornado<br> <input type="checkbox" name="calltotor[]" value="extremelydangerous"> Extremely Dangersous and Life Threatening<br> <input type="checkbox" name="calltotor[]" value="historyoftornado"> History of Producing A Tornado<br> <input type="checkbox" name="calltotor[]" value="historyoftornadoes"> History of Producing Tornadoes<br> <input type="checkbox" name="calltotor[]" value="historyofdamagingtornado"> History of Producing Damaging Tornado<br> <input type="checkbox" name="calltotor[]" value="tornadoatnight"> Tornado at Night<br> <input type="checkbox" name="calltotor[]" value="rainwrapped"> Rain Wrapped Tornado<br> <input type="checkbox" name="calltotor[]" value="rainwrappedtornadowind"> Rain Wrapped Tornado In Addition To Destructive Winds<br> <input type="checkbox" name="calltotor[]" value="supercell"> Supercell With All Severe Modes<br> </div> $callstoaction = array( 'defaulttor' => 'DEFAULT TORNADO WARNING', 'mobilehome' => 'IF IN A MOBILE HOME ABANDON IT IMMEDIATELY', 'motorists' => 'DO NOT SEEK SHELTER UNDER A HIGHWAY OVERPASS', 'outside' => 'IF CAUGHT OUTSIDE...', 'basement' => 'THE SAFEST PLACE TO BE DURING A TORNADO IS IN A BASEMENT.', 'lifethreatening' => 'THIS IS A LIFE THREATENING SITUATION...SEEK SHELTER NOW!', 'extremelydangerous' => 'THIS IS AN EXTREMELY DANGEROUS AND LIFE-THREATENING SITUATION', 'historyoftornado' => 'THIE STORM HAS THE HISTORY OF PRODUCING A TORNADO', 'historyoftornadoes' => 'THIS STORM HAS THE HISTORY OF PRODUCING TORNADOES', 'historyofdamagingtornado' => 'THIS STORM HAS THE HISTORY OF PRODUCING A DAMAGING TORNADO', 'tornadoatnight' => 'TORNADOES ARE DIFFICULT TO SEE AND HEAR AT NIGHT...', 'rainwrapped' => 'THIS TORNADO IS LIKELY WRAPPED IN RAIN...', 'rainwrappedtornadowind' => 'IN ADDITION TO BRIEF RAIN-WRAPPED TORNADOES...', 'supercell' => 'THIS IS A SUPERCELL THUNDERSTORM CAPABLE OF ALL MODES OF SEVERE WEATHER.'); if ($_POST['warntype'] == 'TOR') { foreach($_POST['calltotor'] as $toractions) { } } Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted March 14, 2013 Share Posted March 14, 2013 try foreach($_POST['calltotor'] as $toractions) { if(array_key_exists($toractions, $callstoaction)) echo $callstoaction[$toractions] . "<br>"; } Quote Link to comment Share on other sites More sharing options...
davidannis Posted March 14, 2013 Share Posted March 14, 2013 In your foreach if in_arrray ($toractions, $callstocation ) {do your stuff here;} Quote Link to comment Share on other sites More sharing options...
davidannis Posted March 14, 2013 Share Posted March 14, 2013 AyKay47 is right, you are POSTING keys not values. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 14, 2013 Share Posted March 14, 2013 (edited) I would have guessed that PHP would have a built in function to do an array_intersect using the values of one array against the indexes of another, but I don't see one. So, I see two options that would not require a foreach loop. 1. Use the POST array values to create an array using those as keys using array_fill_keys() and use that with array_intersect_key() to get the resulting array. $selectedValuesAry = array_intersect_key($callstoaction, array_fill_keys($_POST['calltotor'])); 2. To make things VERY simple you can just modify your checkboxes so you have ALL the information in the POST array. Use the keys from the $callstoaction array as the keys of the checkbox array and the values as the values: <input type="checkbox" name="calltotor[defaulttor]" value="Default Tornado Warning"> Default Tornado Warning<br> <input type="checkbox" name="calltotor[mobilehome]" value="Mobile Home"> Mobile Home<br> The post value $_POST['calltotor'] will now have all the selected options and ther keys and values will be the same as the associated keys and values from the master array. You SHOULD be creating those checkboxes dynamically from the master array anyway, so this is by far the easiest and smartest option, in my opinion. Edited March 14, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted March 14, 2013 Author Share Posted March 14, 2013 AyKay47, Thanks a lot man...worked awesome. Dave, 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.