Jump to content


Mr-Chidi

Member Since 20 May 2008
Offline Last Active Aug 16 2015 05:32 AM

Posts I've Made

In Topic: random value change on next page

24 July 2015 - 08:53 AM

Thanks all.

 

Finally i got it to work. I noticed that the point where the regenerating starts is not the next page but on the first page (reason i don't understand though)

I had to create a hidden field and assign the random number to it and then i did an insert with the post value and after that reassigned the value to a session.

Viola! it did the magic!

 

PS: The code shown is not the complete code but the modified code/logic that was giving me trouble and that worked the trouble.

 

Here's my code

 

 
$number = mt_rand(1000, 9999);

$name = "John Doe";

echo $name;
echo "<br>";
echo "Your tally nuber is: ". $number ;

if(isset($_POST['submit'])){

//database insert stuff
$_SESSION['numb'] = $_POST['numb'];
header("location: nextpage.php");

}else{

echo "could not complete action";

}
//html form
<form method="post" action="">
<input name="numb" type="hidden" value="<?php echo $number; ?>">
<input name="submit" type="submit" value="NEXT" >
</form>


//page two

session_start();
 
$number = $_SESSION['numb'];

echo $number;
 

In Topic: random value change on next page

23 July 2015 - 08:50 PM

Where are you generating the number and setting it the session and where on the next page are getting the number?

 

i'm generating the number on the first page, see new code


In Topic: random value change on next page

23 July 2015 - 08:49 PM

The code you posted, is that copy and pasted right from your work or did you modify it for us? It's broken by the looks of it.

 

Maybe it's throwing an error but redirecting to the other page before you see the message. Comment out the header and see what happens.

 

i doubt there's any error


In Topic: random value change on next page

23 July 2015 - 08:49 PM

There seems to be a little problem with my first post but the problem still lingers.

I generate the code on the first page so that it can pass on the value generated to the second page.

here's a modification of my code

//page one
session_start();

//generate the number
// i even tried $_SESSION['number'] =  mt_rand(1000, 9999); but to no avail
$number = mt_rand(1000, 9999);

$name = "John Doe";

echo $name;
echo "<br>";
echo "Your tally nuber is: ". $number ;

if(isset($_POST['submit'])){

//database insert stuff
$_SESSION['number'] = $number;
header("location: nextpage.php");

}else{

echo "could not complete action";

}
//html form cos
<form method="post" action="">
<input name="submit" type="submit" value="NEXT" >
</form>


//page two

session_start();

echo $_SESSION['number']; //but it regenerates another number
 

In Topic: random value change on next page

23 July 2015 - 12:49 PM

Have you made sure you are calling session_start() before the use $_SESSION's

 

yes. i have other session value and all is working well except this one. funny enough there's no code generating function on the next page. but why does it regenerate?