2

I'm trying to find a generic way to call out which radio button has selected. Problem is, while using $_GET in my foreach loop, also submit button is echo in the loop.

Can anyone tell me how to avoid this, so that I just show up the radio buttons? I do not know a way, because foreach just accepts arrays as far as I know

here is my code

<form action="" method="get">
<input type="radio" name="one" value="One1" />One1<br/>
<input type="radio" name="one" value="One2" />One2 <br/>
<input type="radio" name="one" value="One3" />One3<br/>
<input type="submit" name="submit"/> <br/>
</form>



if(isset($_GET['submit'])){
    foreach( $_GET as $key=>$val){
        echo "$val <br/>";
    }
}
4
  • $_GET["one"] should contain the value of which was selected Commented Jul 19, 2013 at 5:13
  • @Orangepill: can't use this in foreach because it just accepts an array Commented Jul 19, 2013 at 5:13
  • You don't need to use foreach in the first place. Commented Jul 19, 2013 at 5:14
  • 1
    just echo $_GET["one"]; ... no foreach ... just that Commented Jul 19, 2013 at 5:15

2 Answers 2

6

just try to add if condition in your loop will not show submit button in your form.

if(isset($_GET['submit'])){

    foreach( $_GET as $key=>$val){
      if($key != 'submit')
        echo "$val <br/>";
    }
}

hope it will help you

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need a loop just do this.

<form action="" method="get">
<input type="radio" name="one" value="One1" />One1<br/>
<input type="radio" name="one" value="One2" />One2 <br/>
<input type="radio" name="one" value="One3" />One3<br/>
<input type="submit" name="submit"/> <br/>
</form>


<?php
if(isset($_GET['one'])){
     echo "You submitted ".$_GET["one"];
}
?>

When you are trying to do something with a known key in an array there is not reason not to access it directly like this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.