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
https://forums.phpfreaks.com/topic/219224-foreach-_post/
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
https://forums.phpfreaks.com/topic/219224-foreach-_post/#findComment-1136837
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/219224-foreach-_post/#findComment-1136839
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.