connect radiobuttons to script

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Fri Mar 15, 2013 8:12 am

Code: Select all

<form>
<input type="radio" name="sex" value="male">Male<br>
<input type="radio" name="sex" value="female">Female
</form> 
creates a simple form with 2 options. They share the same name, so only one can be selected at a time. When sent via form, $_POST['sex'] will contain the value of the selected option as specified by the value attribute. So your decision making in the form processing page would look at $_POST['yourRadioButtonName'] (assuming you're posting form instead of using GET variables.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Mon Mar 18, 2013 7:40 am

here is an example to play with , I'm sure you can get it.

Code: Select all

<?php

if (!$_POST) { //display the form since we're not responding to it yet
    echo '
<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">
<input type="text" name="cost" value="" /><br />    
<input type="radio" name="pay" value="25" />25<br />
<input type="radio" name="pay" value="50" />50<br />
<input type="radio" name="pay" value="75" />75<br />
<input type="radio" name="pay" value="100" />100<br />
<input type="submit" value="Multiply the text box by the radio button" />
</form>';
} else { //process form 
    //first for demonstration purposes; here's what we're working with:
    echo '<pre>';
    print_r($_POST);
    echo '</pre><br />';
    echo 'The answer is ' . $_POST['cost'] * $_POST['pay'];
}
 

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest