Jump to content

foreach $_POST


timmah1

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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];
        }
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.