BeeR_KeG Posted April 16, 2006 Share Posted April 16, 2006 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]<?phperror_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. Quote Link to comment https://forums.phpfreaks.com/topic/7550-need-to-transfer-a-variable-from-one-file-to-another/ Share on other sites More sharing options...
Orio Posted April 16, 2006 Share Posted April 16, 2006 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]<?phpsession_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.phpOrio. Quote Link to comment https://forums.phpfreaks.com/topic/7550-need-to-transfer-a-variable-from-one-file-to-another/#findComment-27498 Share on other sites More sharing options...
wildteen88 Posted April 16, 2006 Share Posted April 16, 2006 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]<?phperror_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]<?phperror_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]<?phperror_reporting(E_ALL & ~E_NOTICE);echo "Welcome {$_POST["name"]} <br />";echo $_POST['minerals'];?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/7550-need-to-transfer-a-variable-from-one-file-to-another/#findComment-27499 Share on other sites More sharing options...
BeeR_KeG Posted April 16, 2006 Author Share Posted April 16, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/7550-need-to-transfer-a-variable-from-one-file-to-another/#findComment-27509 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.