JamesKoash Posted September 5, 2013 Share Posted September 5, 2013 Here is my code: $marketingvalueOutput = ""; foreach($marketingimportance2 as $marketingimportance){ $marketingvalueOutput .= "<input type=\"radio\" class=\"radio\" name=\"marketingimportance[]\" value=\"". $marketingimportance ."\">". $marketingimportance ." \r"; } $marketingvalueOutput .= ""; $custexpOutput = ""; foreach($custexp2 as $custexp){ $custexpOutput .= "<input type=\"radio\" class=\"radio\" name=\"custexp[]\" value=\"". $custexp ."\">". $custexp ." \r"; } $custexpOutput .= ""; $marfuOutput = ""; foreach($marfu2 as $marfu){ $marfuOutput .= "<input type=\"radio\" class=\"radio\" name=\"marfu[]\" value=\"". $marfu ."\">". $marfu ." \r"; } $marfuOutput .= "<br />"; $custexpoptionsOutput = ""; foreach($custexpoptions2 as $custexpoptions){ $custexpoptionsOutput .= "<input type=\"checkbox\" class=\"checkbox\" name=\"custexpoptions[]\" value=\"". $custexpoptions ."\">". $custexpoptions ." \r"; } $custexpoptionsOutput .= "<br />"; $outsourceoptionsOutput = ""; foreach($outsource2 as $outsource){ $outsourceoptionsOutput .= "<input type=\"checkbox\" class=\"checkbox\" name=\"outsource[]\" value=\"". $outsource ."\">". $outsource ." \r"; } $outsourceoptionsOutput .= "<br />"; Here are the two errors I'm getting: Warning: Invalid argument supplied for foreach() in C:\wamp\www\survey\page_3.php on line 135 Notice: Undefined variable: custexp2 in C:\wamp\www\survey\page_3.php on line 135 Line 135 is the second foreach loop which says foreach($custexp2 as $custexp). Thanks Quote Link to comment https://forums.phpfreaks.com/topic/281891-multiple-foreach-loops-causing-error/ Share on other sites More sharing options...
taquitosensei Posted September 5, 2013 Share Posted September 5, 2013 Usually that means it's not an array. Try print_r($custexp2); to make sure it contains what you expect it to. Here is my code: $marketingvalueOutput = ""; foreach($marketingimportance2 as $marketingimportance){ $marketingvalueOutput .= "<input type=\"radio\" class=\"radio\" name=\"marketingimportance[]\" value=\"". $marketingimportance ."\">". $marketingimportance ." \r"; } $marketingvalueOutput .= ""; $custexpOutput = ""; foreach($custexp2 as $custexp){ $custexpOutput .= "<input type=\"radio\" class=\"radio\" name=\"custexp[]\" value=\"". $custexp ."\">". $custexp ." \r"; } $custexpOutput .= ""; $marfuOutput = ""; foreach($marfu2 as $marfu){ $marfuOutput .= "<input type=\"radio\" class=\"radio\" name=\"marfu[]\" value=\"". $marfu ."\">". $marfu ." \r"; } $marfuOutput .= "<br />"; $custexpoptionsOutput = ""; foreach($custexpoptions2 as $custexpoptions){ $custexpoptionsOutput .= "<input type=\"checkbox\" class=\"checkbox\" name=\"custexpoptions[]\" value=\"". $custexpoptions ."\">". $custexpoptions ." \r"; } $custexpoptionsOutput .= "<br />"; $outsourceoptionsOutput = ""; foreach($outsource2 as $outsource){ $outsourceoptionsOutput .= "<input type=\"checkbox\" class=\"checkbox\" name=\"outsource[]\" value=\"". $outsource ."\">". $outsource ." \r"; } $outsourceoptionsOutput .= "<br />"; Here are the two errors I'm getting: Warning: Invalid argument supplied for foreach() in C:\wamp\www\survey\page_3.php on line 135 Notice: Undefined variable: custexp2 in C:\wamp\www\survey\page_3.php on line 135 Line 135 is the second foreach loop which says foreach($custexp2 as $custexp). Thanks Quote Link to comment https://forums.phpfreaks.com/topic/281891-multiple-foreach-loops-causing-error/#findComment-1448311 Share on other sites More sharing options...
JamesKoash Posted September 5, 2013 Author Share Posted September 5, 2013 Thank you -- just tried that and it seems that I made a minor error with variable names. Another quick question... I want to add checked="checked" to the radio buttons and checkboxes that have been selected. How is this possible? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/281891-multiple-foreach-loops-causing-error/#findComment-1448314 Share on other sites More sharing options...
JamesKoash Posted September 5, 2013 Author Share Posted September 5, 2013 Here is the rest of the code: $outsource2 = array("Marketing Agency","PR Agency","Design Studio","Marketing Consultant", "Media Agency", "Advertising Agency", "Service or Product Design Agency", "Customer Experience Agency"); if($_SERVER['REQUEST_METHOD'] == 'POST') { if(isset($_POST['outsource'])){ foreach($outsource2 as $outsource){ $value = (in_array($outsource,$_POST['outsource']) ? "True" : "False"); $_SESSION[$outsource] = $value; $result[$outsource] = $value; } } else { unset($_SESSION['outsource']); } } Like I said, I just need to know how to add checked="checked" for the options that have been selected. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/281891-multiple-foreach-loops-causing-error/#findComment-1448328 Share on other sites More sharing options...
Solution cyberRobot Posted September 6, 2013 Solution Share Posted September 6, 2013 First, radio buttons should have the same name. There's no need for the array format. Also, you wouldn't need to escape all those double quotes, if the input tags are enclosed in single quotes. Double quoted strings are only needed if the string contains things like variables or other special characters (\r). <?php //... foreach($marketingimportance2 as $marketingimportance){ $marketingvalueOutput .= '<input type="radio" class="radio" name="marketingimportance" value="' . $marketingimportance . ' \>' . $marketingimportance . " \r"; } //... ?> With the radio buttons having the same name, you can use a single POST variable to populate the radio buttons. Here's a quick example (note that you'll need to adapt it for your own code): <form method="post" action=""> <?php $marketingimportance2 = array('this', 'that'); foreach($marketingimportance2 as $marketingimportance){ print '<input type="radio" class="radio" name="marketingimportance" value="' . $marketingimportance . '"'; if(isset($_POST['marketingimportance']) && $_POST['marketingimportance']==$marketingimportance) { print ' checked="checked"'; } print ' />' . $marketingimportance . " \r"; } ?> <input type="submit" name="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/281891-multiple-foreach-loops-causing-error/#findComment-1448452 Share on other sites More sharing options...
JamesKoash Posted September 6, 2013 Author Share Posted September 6, 2013 Hi there cyberRobot Your code is great for radio buttons, but if you look at my original post you'll see two of the form fields use checkboxes, allowing the user to select multiple options. I've just tried your code for checkboxes and it pops up with error: "in_array() expects parameter 2 to be array, string given". My original code works much better for this as it saves the following to session data. I just haven't managed to work out how to add checked="checked" for the options that have been selected. [Marketing Agency] => False [PR Agency] => False [Design Studio] => False [Marketing Consultant] => True [Media Agency] => False [Advertising Agency] => False [Service or Product Design Agency] => False [Customer Experience Agency] => False Thanks Quote Link to comment https://forums.phpfreaks.com/topic/281891-multiple-foreach-loops-causing-error/#findComment-1448503 Share on other sites More sharing options...
JamesKoash Posted September 6, 2013 Author Share Posted September 6, 2013 Actually, don't worry... adding the [] back to the end of the name seems to have worked. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/281891-multiple-foreach-loops-causing-error/#findComment-1448505 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.