0

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;  
}  
}
user1956185
  • 389
  • 4
  • 8
  • 16
  • i dont understand this "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?" could you make clear – NullPoiиteя Jan 12 '13 at 12:03
  • The error your having is that `$isSent` is only declared within the functions.php page and will stay there. You have two options. Use a session variable which will expire after a time limit. Or use a cache to save your variable data. So then you can effectively 'save' $isSent as `true` – bashleigh Jan 12 '13 at 14:32

3 Answers3

2

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

bashleigh
  • 8,813
  • 5
  • 29
  • 49
  • I already use sessions for my login code; I edited the question and posted the code for what I am trying to do; thank you for your suggestion; I will try with sessions and see if it works – user1956185 Jan 12 '13 at 13:36
  • @user1956190 Right I see! Is the selection supposed to be permanent? I'd recommend using a cache if it is. I'll post up another answer on caches if so. – bashleigh Jan 12 '13 at 14:27
  • Wait sessions would be useless as I've realised they're for admin and client. Use a cache to save the option and load the options. – bashleigh Jan 12 '13 at 14:33
  • isn't there an easier way for somehow sending the variable $var without cache? I haven't used cache before – user1956185 Jan 12 '13 at 14:42
  • @user1956190 Nor have I! lol looking it up now. It shouldn't be too hard. It will be similar to including a file to a page. No biggy :) – bashleigh Jan 12 '13 at 14:45
1

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.

CuriousMind
  • 3,143
  • 3
  • 29
  • 54
1

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

bashleigh
  • 8,813
  • 5
  • 29
  • 49
  • Thank you very much! I tried the code but it says Fatal error:Call to undefined function apc_fetch(); i guess that I have to install the apc extension, no? – user1956185 Jan 12 '13 at 15:44
  • @user1956190 Yea I think you do. But I'd still check out that youtube api video. That might help as he's saving a large variable to a 'cache' text file using `put_contents()` and `get_contents()` :) – bashleigh Jan 12 '13 at 15:49
  • It works! :) I stored the variable in the 'cache' file. I still have one more question though. In the code you wrote above, $enabled takes the value $_POST['send'] ? – user1956185 Jan 12 '13 at 16:01
  • @user1956190 No the value of `$_POST['send']` would be `'Submit'` as that's the value of your submit button. `$enabled = apc_fetch('enabled')` which is the Alternative PHP Cache named `'enabled'`. I'm guessing it works similar to an array key type declaration. So lets say the apc variable `'enabled'` equaled `'hello there I am a string'`. Then with declaring `$enabled = apc_fetch('enabled')`. `$enabled` would equal 'hello there I am a string'. Hope this helped :) – bashleigh Jan 12 '13 at 16:14