在php中传递函数和文件之间的变量

在php中传递函数和文件之间的变量

问题描述:

I have a file called admin.php in which I have a button with the name send. What I want to do is when I click it, to make visible a link on the user's page, user.php. How can I do this?

I have a file with all my functions called functions.php in which I have a function called onSubmit($var); I initialize the variable $var is admin.php with the value $_POST['send'] but when I call the function in the file user.php I have no way of telling him who the variable $var is so I get 'undefined index'.

Is there another way to do this?

EDIT Added code This is admin.php

<input type="button" name="send" value="Submit" /><br/>      

require 'functions.php';  
$posted = $_POST['send'];  
onSubmit($posted);  

This is user.php

require 'functions.php';  
onSubmit($var);  //here it says undefined index var because it doesn't know who the variable is
if($isSent == 1) { 

<a style="visibility:visible;" href="test3.html" id="test3">Test3</a> <br/>  

}  

And this is functions.php

global $isSent;  
function onSubmit($var) {  
if(isset($var)) {  
$isSent = 1;  
}  
}

我有一个名为admin.php的文件,其中有一个名为send的按钮。 我想要做的是当我点击它,在用户页面上看到一个链接user.php。 我该怎么做? p>

我有一个文件,其中包含所有函数,名为functions.php,其中有一个名为onSubmit($ var)的函数; 我使用值$ _POST ['send']初始化变量$ var是admin.php但是当我在文件user.php中调用该函数时,我无法告诉他变量$ var是谁所以我得到'undefined' 索引'。 p>

还有其他方法吗? p>

编辑添加代码 strong> 这是admin.php

 &lt; input type =“button”name =“send”value =“Submit”/&gt;&lt; br /&gt;  
 
需要'functions.php';  
 $ posted = $ _POST ['send'];  
onSubmit($发布);  
  code>  pre> 
 
 

这是user.php p>

  require'functions.php';  
onSubmit($ VAR);  //这里它表示未定义的索引var,因为它不知道变量是谁
if($ isSent == 1){
 
&lt; a style =“visibility:visible;”  href =“test3.html”id =“test3”&gt; Test3&lt; / a&gt;  &LT峰; br /&GT;  
 
} 
  code>  pre> 
 
 

这是functions.php p>

  global $ isSent;  
函数onSubmit($ var){
if(isset($ var)){
 $ isSent = 1;  
} 
} 
  code>  pre> 
  div>

Right I've done a bit of research on Caching and this is what I've come up with. It might not be 100% correct but it's a start as like I've said I've never tried it myself lol

In your admin.php I'd put this function in:

if(isset($_POST['send'])){
    if($enabled == true){
        $enabled == false;
    }
    else{
        $enabled == true;
    }
     apc_add('enabled',$enabled);
}

Now to 'get' our $enabled var:

$enabled = apc_fetch('enabled');

Then to check the the var within your client page:

if($enabled == true){
    echo ' button';
}

Now the only things I haven't fully looked at is the security of the apc_ function and the client usage. I believe it works for all clients of the server but I'm not 100% certain. Here the php manual to give better examples.

This is the method I was thinking of. But again I'm not sure on the security of it but I'm sure you can find something to keep it secure. The video is actually is tutorial for a Youtube API. But he does cover saving a variable to a cache text file which should be of use to you :)

If you have functions.php which defines functions, simply include it in admin.php file and then you can call the function from there and also pass value.

Basically you need to use sessions like below:

if(isset($_SESSION['makeVisible']) && $_SESSION['makeVisible'] == true){
    echo '<button>Button init</button>'; //you could also use html like the comment below.
}
/*
if(condition){?> <!-- this is now html --> <button>Button init</button><?}
*/

Then to set this variable on your admin page use:

if(isset($_POST['submitButton'])){
    $_SESSION['makeVisible'] == true;
}

You'll also need a form for this method to work but there are other methods but I prefer this one.

<form name="buttonMakerThing" method="POST">
    <input name="submitButton" value="Make button init Visible" type="submit"/>
</form>

Without an action the form defaults to 'POSTING' the form information to the current page. Making the condition if(isset($_POST)) return true.
You will need to add a $_SESSION declaration at the top of every php page you have on your site for this to work. It MUST go on the very first line of every page! for example:

01: | <?php session_start();
02: |//rest of script;

Please look more into $_SESSIONS for unnsetting/destroying your sessions and more uses for them :) http://php.net/manual/en/reserved.variables.session.php