last day (15 days later) » 

8:28 PM
1
A: Global variables use

DennisWhat you are looking for is: <?php function add() { $GLOBALS['pubblica'] = "insert name"; } function esplore() { global $pubblica; echo "Contents of variables is $pubblica"; } add(); esplore(); ?> If you don't use global $pubblica; the esplore() function doesn't know that $pubbli...

 
How to pass the global variable as a parameter?
 
@Heisenberg The same way as every other variable, but if you have a global variable you don't have to pass it since IT IS GLOBAL and can be used from everywhere (unless it gets overwritten in the local scope).
 
Well, this solution in my code doesn't working.. the contents of $pubblica is blank
 
@Heisenberg What? The code I posted definately works (ideone.com/ViUzbU). Of course you have to call add() first as I did.
 
I updated the question, I think I've successfully implemented its working code. The problem is that the variable is always empty.
 
8:28 PM
@Heisenberg Of course it is always empty if you never assign any value to your global variable. You have to call add() first and then esplore(). Otherwise your global variable is empty/not set. You have to actually look at my code and not just assume it's the same as your code.
 
I obtain this message: Notice: Undefined index: pubblica in esplore() function
 
hey
 
I do not know if you noticed, but I wrote that the value is assigned when you clisk a button. In this way:
if(isset($_POST['button']))
{
elabora();
}
 
how does elabora(); look like?
 
elabora corresponds to the add function
 
8:32 PM
k
can you upload the whole script?
 
just use pastebin.com or something similar
 
Ok just a moment please
The code is large, the line 312 is concerned, and the function at line 330
 
got it
 
Ok look good news
 
8:38 PM
are you sure you actually assign anything to $GLOBALS? I mean do you even go to the if condition where you set $GLOBALS?
 
I checked the values that check for $ GLOBALS and have fields that I want, but for some strange reason the variable does not assume
 
So if you write
echo 'yep working';
at line 313 you can see this text?
 
yes
The code runs correctly
 
what do you get if you write var_dump($GLOBALS['pubblica']); at line 313?
 
now I feel
I obtain the contents that i've passed so:
string(32) "consulta_provinciale;VB;marck;;;"
 
8:42 PM
goot
good
 
So there the variable takes the content properly.
But it seems to disappear in elabora
 
now please go to line line 331 (inside elabora()) and write var_dump($GLOBALS['pubblica']);
so if you write the same at the beginning of elabora it is empty?
 
I obtain: NULL
Notice: Undefined index: pubblica in C:\xampp\htdocs\Registrazione\elaborazione.php on line 334
NULL
334 is: var_dump($GLOBALS['pubblica']);
 
mhhhh
 
Is so strage
strange*
 
8:48 PM
var_dump($GLOBALS['pubblica']); at line 326 right after the if(...) {
in front of elabora();
 
Notice: Undefined index: pubblica in C:\xampp\htdocs\Registrazione\elaborazione.php on line 328
NULL
 
ok, well we are getting closer
 
The value is lost somewhere?
 
wait a sec
 
no problems :)
 
8:51 PM
same code in front of fclose($handle); ..... so } var_dump(...); fclose(..);
schould be line 321/322 (at least in the file i have right now)
should
 
}
var_dump($GLOBALS['pubblica']);
fclose($handle);
}
right?
 
yeah
 
string(39) "consulta_provinciale;VB;dfdfd;io;marck;"
 
and after fclose()?
 
the same
 
8:54 PM
oO
ok last var_dump
line 324 before if(isset($_POST['pubblica']))
 
Ok
Notice: Undefined index: pubblica in C:\xampp\htdocs\Registrazione\elaborazione.php on line 325
NULL
if you exit of isset the value is lost
 
Guys the problem is hes probably calling another script.
 
uhm
 
what code you have in the button ?
 
I've created the button in this way: echo "<form action='#' method='post'>";
echo "<input type='submit' name='pubblica' value='Pubblica Risultati'>";
echo "</form>";
 
9:03 PM
yes so you "RECALL" the current script again
and so all values are lost, including globals
 
recall?
 
use $_POST['pubblica'] to get your value
 
how do I not lose it?
 
youre not losing your form data, which can be retrived in this case with $_POST
 
it be because I'm using the same name for the button?
What interests me is the variable name.
 
9:06 PM
echo "<input type='submit' name='pubblica' value='Pubblica Risultati'>";
here the name='pubblica' is passed to the target script as $_POST['publicca']
because
<form action='#' method='post'>" here you use POST mthod of passing data from the form
 
I changed in echo "<input type='submit' name='go_publish' value='Pubblica Risultati'>";
but the variables is same blank
 
and action="#" is the target script - in this case # is substtution for you current one
I will write you an answer in your question - an example
 
Uhm ok now i feel
 
Ok now function
 
9:13 PM
ah now I got it John... so GLOBALS is set in one call but not in the next call. I was assuming this script is running one time only
but yeah makes sense now
 
I was careless in not realizing it.
Thanks anyway for the great help guys.
I offer you my greetings and out, thanks again.
 
after the button is pressed, the script runs completelly new. So the variable is not kept in the script, but passed in the POST (or GET) data, I wrote you an exmple script in the answer
 

last day (15 days later) »