Page 1 of 1
Passing variables from one php file to another
Posted: Mon Jun 04, 2007 9:27 pm
by zarkolazarov
I'm a little new to php programming so I need a little help.
I'm stuck with the passing of a variable from one php in another. Namely I get the variable in the first php using the rand function:
$rn1=rand(1,6);
and than I need to use this variable in the other php in this portion of the code:
function showdie($nr)
{
echo "<img src='die";
echo $nr;
echo ".jpg'>";
}
showdie($rn1);
On the first php I have a form that with submit calls the second php in which I use some of the variables in the first one. The problem is, they don't make it to the second one. I would have tried $rn1=$HTTP_GET_VARS['rn1']; only this variable is not in a form.
I do have register_globals set to "on" in the php.ini file.
Can anyone please help me solve this problem.
Thanks infront
Posted: Mon Jun 04, 2007 9:31 pm
by feyd
PHP doesn't automagically transfer variables created in one script over to another. You have to ask it to. Either through forms, links, sessions or variants thereof.
Posted: Mon Jun 04, 2007 9:54 pm
by zarkolazarov
I know that PHP does not transfer variables automatically, I was thinking of transferring them through a form, but I get the variable through a function that is not in any form. What would the code look like if it was in a form? Doesnt the form need to have input types? And what would I input if the variable is generated automatically through the rand function.
To be more clear, here's a bigger portion of the code:
the first file: king.php
Code: Select all
<?php
function showdie($nr)
{
echo "<img src='die";
echo $nr;
echo ".jpg'>";
}
function check_king($nr)
{
switch($nr)
{
case 1:
return 1;
break;
case 2:
return 0;
break;
case 3:
return 1;
break;
case 4:
return 0;
break;
case 5:
return 1;
break;
case 6:
return 0;
break;
}
}
function checksoldier($nr)
{
switch($nr)
{
case 1:
return 0;
break;
case 2:
return 2;
break;
case 3:
return 2;
break;
case 4:
return 4;
break;
case 5:
return 4;
break;
case 6:
return 6;
break;
}
}
$rn1=rand(1,6);
$rn2=rand(1,6);
$rn3=rand(1,6);
$rn4=rand(1,6);
echo "<h1 align='center'>Kings and Soldiers</h1>";
//show the sides of the die which are generated randomly (we use the generated number and
//that number send it to the function showdie() to display the image based on the generated numnber)
echo "<table border='0' align='center'>";
echo "<tr><td>";
showdie($rn1);
echo "</td><td>";
showdie($rn2);
echo "</td><td>";
showdie($rn3);
echo "</td><td>";
showdie($rn4);
echo "</td></tr></table>";
//calculates the total number of kings generated randomly
$totalk=check_king($rn1)+check_king($rn2)+check_king($rn3)+check_king($rn4);
print <<<HERE
<br>
<form method="get" name="input" action="presmetka.php">
<center>
Number of Kings<input type="text" name="NrKings" />
Number of Soldiers<input type="text" name="NrSoldiers" /><br /><br />
<input type="submit" />
</center>
</form>
HERE;
//display the result
echo "<h1>In total there are :$totalk kings</h1>";
//calculate the numner of soldiers generated randomly
$totals=checksoldier($rn1)+checksoldier($rn2)+checksoldier($rn3)+checksoldier($rn4);
//display the result
echo "<h1>In total there are :$totals soldiers</h1>";
?>
and here is a portion of the second file: presmetka.php
Code: Select all
<?php
function showdie($nr)
{
echo "<img src='die";
echo $nr;
echo ".jpg'>";
}
showdie($rn1);
?>
How do I pass the value from king.php to presmetka.php?
Posted: Mon Jun 04, 2007 10:20 pm
by feyd
feyd wrote:Either through forms, links, sessions or variants thereof.
Posted: Tue Jun 05, 2007 7:01 am
by AvalonMel
zarkolazarov wrote:I know that PHP does not transfer variables automatically, I was thinking of transferring them through a form, but I get the variable through a function that is not in any form. What would the code look like if it was in a form? Doesnt the form need to have input types? And what would I input if the variable is generated automatically through the rand function.
What about something like this? You then just need to trigger the
Code: Select all
<?php
$rn1=rand(1,6);
?>
<form id="form1" name="form1" method="post" action="secondphppage.php">
<input type="hidden" name="rn1" value="<?php echo $rn1; ?>" />
</form>
Then in your second php page just include the line:
Posted: Tue Jun 05, 2007 7:09 am
by infolock
Code: Select all
$var = serialize(rand(1,6));
header("Location: bob.php?my_var=$var");
bob.php
Code: Select all
echo unserialize($_GET['my_var']);