Jump to content

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??
      }
   }
?>

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.