Jump to content


Photo

Need to transfer a variable from one file to another


  • Please log in to reply
3 replies to this topic

#1 BeeR_KeG

BeeR_KeG
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 16 April 2006 - 04:53 PM

Alright, I need to transfer the value of a variable which is generated by mt_rand( ). I want that same value to be transfered to another file, but everytime I test it, I get the undefined variable error. I can't include the file with the piece of code that generates the integer, because it'll create a different number.
I've tried setting it as a global variable and trying to create a session, but I don't have much experience in there. I'd also like it if it didn't require the use of a MySQL, because I know nothing about that.

Here's the code:

data.php
<?php 
srand((double)microtime() * 1000000); $minerals = rand(0, 2500); 
global $minerals;
?>

index.php
<?php
error_reporting(E_ALL & ~E_NOTICE); 
session_start ( );
echo <<<HTML
<form action="form.php" method="POST">
Enter your username: <input type="text" name="name" /><br />
Enter the amount of minerals you wish to bet: <input type="text" name="minbet" /><br />
<input type="submit" />
</form>
HTML;
require ("data.php");
echo "You currently have {$minerals} minerals <br />";
?>

form.php
<?php 
error_reporting(E_ALL & ~E_NOTICE); 
echo "Welcome {$_POST["name"]} <br />";
echo $minerals;
?>

Alright, here's a brief summary of what is happening. I start up in index.php, which gives me an HTML form which asks for a name and amount of minerals to bet. The file will include $minerals, which is a random integer from 0 to 2500 which will be generated in data.php and required in index.php. When I submit the query form in index.php, it'll load form.php, but the $minerals doesn't transfer.
Thanks in advance.

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 16 April 2006 - 05:02 PM

I cant understand when the data.php is being visited or called...
If the user visits the page, you can do it with sessions. Do the following thing:
In data.php:
<?php 
srand((double)microtime() * 1000000); $minerals = rand(0, 2500); 
session_start();
$_SESSION['minerals'] = $minerals;
?>
In form.php:
<?php
session_start();
$minerals = $_SESSION['minerals'];
error_reporting(E_ALL & ~E_NOTICE); 
echo "Welcome {$_POST["name"]} <br />";
echo $minerals;
?>

If the user doesnt visit the data.php file, just inculde it in the form.php

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 16 April 2006 - 05:02 PM

Use sessions, so after this line in index.php:
require ("data.php");
Place:
$_SESSION['minerals'] = $minerals;
Then on form.php do this:
<?php
error_reporting(E_ALL & ~E_NOTICE);
$minerals = $_SESSION['minerals']

Or, this just come to me while I was doing this post. Is to have hidden form element form. So in index.php change it to this:
<?php
error_reporting(E_ALL & ~E_NOTICE);
require ("data.php");

echo <<<HTML
<form action="form.php" method="POST">
Enter your username: <input type="text" name="name" /><br />
Enter the amount of minerals you wish to bet: <input type="text" name="minbet" /><br />
<inout type="hidden" name="minerals" value="{$minerals}" />
<input type="submit" />
</form>
HTML;

echo "You currently have {$minerals} minerals <br />";
?>

Now in form.php chnage it to this:
<?php
error_reporting(E_ALL & ~E_NOTICE);
echo "Welcome {$_POST["name"]} <br />";
echo $_POST['minerals'];
?>


#4 BeeR_KeG

BeeR_KeG
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 16 April 2006 - 05:53 PM

Many thanks to the both of you.
I appreciate wildteen's inout script, but to put it simply, I'd rather do it with PHP because $minerals is not the only variable I'll be transfering, but a little knowledge doesn't hurt.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users