Jump to content

radio button function call


weeder

Recommended Posts

I am new to php and getting the hang of the basic stuff but this i have failed

 

Have 3 radio buttons and looking to call a function

 

print "<h3>Banks</h3><br />

<form action='bank.php' method='post'>

<input type='radio' name='money' id='1' value='1' /> Money<br />

<input type='radio' name='crystal' id='2' /> Crystal<br />

<input type='radio' name='ruby' id='3' value='3' /> Ruby<br />

<input type='submit' value='bank' /></form>";

 

Need to call

button 1

function money()

button2

function crystal()

button3

function ruby()

 

All the functions are in the same file and all work ok

 

I just can't get the radio buttons to call functions any help will be greatly apreceated

Link to comment
https://forums.phpfreaks.com/topic/119332-radio-button-function-call/
Share on other sites

you'd be better off naming them the same, and giving them the respective value they represent.  this will allow you to use the radio as it was meant to (restricting the selection to one option):

 

<?php
if (isset($_POST['submit'])
{
  {$_POST['fn']}();
}

print "<h3>Banks</h3>

<form action='bank.php' method='post'>
<input type='radio' name='fn' id='fn' value='money' /> Money

<input type='radio' name='fn' id='fn' value='crystal' /> Crystal

<input type='radio' name='fn' id='fn' value='ruby' /> Ruby

<input type='submit' value='bank' /></form>";
?>

 

please use code tags in the future.

First, give your submit input a name, so you can reference it directly within your PHP code.  Then, in bank.php, do something along the lines of:

<?php
   if(isset($_POST['submit'])) //this assumes you named your submit input 'submit'
   {
      if(isset($_POST['money']))
      {
         //execute money code
      }
      else if(isset($_POST['crystal']))
      {
         //execute crystal code
      }
      else if(isset($_POST['ruby']))
      {
         //execute ruby code
      }
      else
      {
         //any error reporting you need to do??
      }
   }
?>

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.