1

How can i pass form value to 2 PHP functions. For example this is what am trying to do:

$delegate_name = trim($_POST['delegate_name']);

function certificate_custom_css(){  //function 1

  if(($_GET["page"] == "preview_certificate") && ("Clement" == $delegate_name))
{
wp_enqueue_style( 'certificates_stylesheet_base');
wp_enqueue_style( 'certificates_stylesheet_fancy'); 
wp_enqueue_style( 'certificates_stylesheet_tab3'); 

}

} 

function certificate($delegate_name){  //function 2

 return  $delegate_name;

}
Landry
  • 137
  • 11
  • 2
    What is the issue here? What happens? How are you using it? What is going wrong and is there any error? If there is an error, can you give us the error. – Martin Dimitrov Oct 02 '19 at 06:57
  • could you please elaborate your problem. – Badrinath Oct 02 '19 at 06:59
  • @MartinDimitrov is the code above correct? I don't get any error right now but the value does not seem to get passed – Landry Oct 02 '19 at 07:05
  • @Landry So your problem is, you cannot use `$delegate_name` in `function 1`? You have to tell us what you are trying to do and what is happening and what should happen, so we can give you a solution. – Martin Dimitrov Oct 02 '19 at 07:11
  • @MartinDimitrov, my problem is i am unable to use $delegate_name in function 1 and in function2. The two functions take tha variable to do something different with it. I know ideally i can pass the form value to both functions using the POST variable but i want to past the form value once from outside the function to the $delegate_name variable and use it in any function in the script. Thanks in advance for your help – Landry Oct 02 '19 at 07:15

2 Answers2

1

What you have to do in order this to work is, you have to make the function accept an argument like this:

function certificate_custom_css($delegate_name)

And later call it with

certificate_custom_css($delegate_name);

The other way to use it, is to use it as a global:

function certificate(){  //function 2
 global $delegate_name;

 return  $delegate_name;

}

I really really recommend using the first one, unless absolutely necessary to use global.

There are of course the closures, but I suggest to stick to the first option, if you do not completely understand them. But you can do it like this:

$certificate_custom_css = function() use ($delegate_name) { return $delegate_name;}

You can read some more here and here.

Martin Dimitrov
  • 1,304
  • 1
  • 11
  • 25
1

you can do it in following way

function certificate_custom_css($delegate_name){  //function 1

if(($_GET["page"] == "preview_certificate") && ("Clement" == $delegate_name))
{
    wp_enqueue_style( 'certificates_stylesheet_base');
    wp_enqueue_style( 'certificates_stylesheet_fancy'); 
    wp_enqueue_style( 'certificates_stylesheet_tab3'); 

}

} 

function certificate($delegate_name){  //function 2

return  $delegate_name;

}

and can call methods like

$delegate_name = trim($_POST['delegate_name']);

certificate_custom_css($delegate_name);
certificate($delegate_name);
Badrinath
  • 501
  • 5
  • 14