_post inside a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

_post inside a function

Post by Da_Elf »

i know with functions i need to pass my variables into the function but how about this situation. it doesnt seem to work
ive got a few forms and i want to use 1 function to deal with them (each having a different button so i tried

Code: Select all

function try($button){
  if(isset($_POST['$button'])){
  $field1=$_POST['field1'];
  echo $field1." was inserted when ".$button." was pressed";
  }
}
try(help);
id like it to echo the info from the text box called field1 as well as work with different buttons from different forms
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: _post inside a function

Post by cpetercarter »

Code: Select all

try("help");
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: _post inside a function

Post by Da_Elf »

doesnt work. i figure i had to change it to this

Code: Select all

function try($button, $field1){
  if(isset($button)){
  echo $field1." was inserted when ".$button." was pressed";
  }
}
try($_POST['help'], $_POST['field1']);
this seems to work now
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: _post inside a function

Post by requinix »

Code: Select all

if(isset($_POST['$button'])){
Variables don't work when you put them in single-quoted strings.

Code: Select all

if(isset($_POST[$button])){
Post Reply