Jump to content

Recommended Posts

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
[code]<?php
srand((double)microtime() * 1000000); $minerals = rand(0, 2500);
global $minerals;
?>[/code]

index.php
[code]<?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 />";
?>[/code]

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

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.
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:
[code]
<?php
srand((double)microtime() * 1000000); $minerals = rand(0, 2500);
session_start();
$_SESSION['minerals'] = $minerals;
?>
[/code]
In form.php:
[code]
<?php
session_start();
$minerals = $_SESSION['minerals'];
error_reporting(E_ALL & ~E_NOTICE);
echo "Welcome {$_POST["name"]} <br />";
echo $minerals;
?>
[/code]

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

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

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:
[code]<?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 />";
?>[/code]

Now in form.php chnage it to this:
[code]<?php
error_reporting(E_ALL & ~E_NOTICE);
echo "Welcome {$_POST["name"]} <br />";
echo $_POST['minerals'];
?>[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.