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;

}
5
  • 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. Commented Oct 2, 2019 at 6:57
  • could you please elaborate your problem. Commented Oct 2, 2019 at 6:59
  • @MartinDimitrov is the code above correct? I don't get any error right now but the value does not seem to get passed Commented Oct 2, 2019 at 7: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. Commented Oct 2, 2019 at 7: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 Commented Oct 2, 2019 at 7:15

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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);

3 Comments

Both answers are similar and correct. Thanks very much guys
You are most welcome and can ask any question directly by searching my id user:5461560
How do you mean?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.