timmah1 Posted November 19, 2010 Share Posted November 19, 2010 I have a form with radio buttons, depending on if a person selects yes, they will be given an answer after they hit submit. I don't want to do a bunch if un-necessary coding, so I'm trying something else, but it's not working. My form is this <table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr> <td align="center" valign="top">Yes</td> <td align="center" valign="top"><input type="radio" name="phone1" id="phone1" value="1" /></td> <td align="center" valign="top">No</td> <td align="center" valign="top"><input type="radio" name="phone1" id="phone1" value="2" /></td> </tr> </table> My code is this <?php if(isset($_POST['submit'])) { $key; $key['phone1'] = "testing phone 1"; $key['phone2'] = "testing phone 2."; foreach( $_POST as $key => $value) { if($value == "1"){ echo $key."<br />"; } } } ?> Now, it does not show the wording associated with $key if the value is 1. Can anybody help me out here? This form is going to have over 100 questions. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 19, 2010 Share Posted November 19, 2010 I would do something like: <td align="center" valign="top">Yes</td> <td align="center" valign="top"><input type="radio" name="phone[1]" id="phone1-1" value="1" /></td> <td align="center" valign="top">No</td> <td align="center" valign="top"><input type="radio" name="phone[1]" id="phone1-0" value="0" /></td> <td align="center" valign="top">Yes</td> <td align="center" valign="top"><input type="radio" name="phone[2]" id="phone2-1" value="1" /></td> <td align="center" valign="top">No</td> <td align="center" valign="top"><input type="radio" name="phone[2]" id="phone2-0" value="0" /></td> if(isset($_POST['submit'])) { $message[1] = "testing phone 1."; $message[2] = "testing phone 2."; foreach($_POST['phone'] as $key => $value) { if($value == 1) { echo $message[$key]; } } } Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 19, 2010 Share Posted November 19, 2010 But in this simple example where the only difference is numbers, this is simpler: if(isset($_POST['submit'])) { foreach($_POST['phone'] as $key => $value) { if($value == 1) { echo "Testing phone $key"; } } } Quote Link to comment Share on other sites More sharing options...
timmah1 Posted November 19, 2010 Author Share Posted November 19, 2010 Thank you so much AbraCadaver. I knew it was something simple, I just couldn't put my finger on it 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.