Jump to content

Help - Variable Keeps Resetting - Dynamic Text


oliverj777

Recommended Posts

Hello,

 

I'm working on a dynamic text form (I guess thats what its called). Basically, I have a html page that echos in some PHP code. What the PHP code does is show different strings of text depending on a variable:

 

<PHP?
$brief1_info = "brief info goes here";
$brief1 = 0;

if (isset($_POST['brief1Go'])) {
$brief1 = $brief1 + 1;
}
else if (isset($_POST['brief1Back'])) {
$brief1 = $brief1 - 1;
}


$breif1Controller = "
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">
<input type=\"submit\" name=\"brief1Back\" id=\"brief1Back\" value=\"BACK\" />
</form>

<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">
<input type=\"submit\" name=\"brief1Go\" id=\"brief1Go\" value=\"CONTINUE\" />
</form>";



if($brief1 == 0){
$brief1_info = "<b>Welcome Commander,</b> you are currently viewing the Mission Control Brief page, here you can view all your missions that you need to complete in order to advance through your prestiges. You will need to follow your briefs carefully in order to succeed. <br /><br />

";
}
else if($brief1 == 1){
$brief1_info = "Okay, let me show you around the place ...";
}
else if($brief1 == 2){
$brief1_info = "brief is on 2";
}


?>

 

The problem is, the BACK and CONTINUE buttons don't work. If $brief1 equals 0, and the user presses continue, the is should go to 1, and the brief1_info string is changed (so some different text shows in my html). It actually works when brief1 is on '0', but when its on 1 and the user pressed continue, the brief1 is changed to 1, but because the submit button refreshes the page, the variable is re-read again, and thus is reset back to 0.

 

Is there a way around this? Or is there a better method than what I'm doing?

 

Thanks

 

If you need a variable like that then you could place into a session variable, that way from your description the variable would not be reset when the script is reloaded.

 

Please elaborate, I'm not that advanced at PHP. I sure would appreciate it.

Give this a test

EDIT:  Updated

<?PHP 
session_start();
if(isset($_SESSION['brief'])){
$brief1=$_SESSION['brief'];
}
else{
$brief1 = 0;
}
//$brief1_info = "brief info goes here"; Not used/remove


if (isset($_POST['brief1Go'])) {
$brief1 = $brief1 + 1;
$_SESSION['brief']=$brief1;
}
else if (isset($_POST['brief1Back']) && $_SESSION['brief']>=1) {
$brief1 = $brief1 - 1;
$_SESSION['brief']=$brief1;
}
?>
<html>
<head>
  <title></title>
</head>
<body>
<?PHP
$breif1Controller = "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">";
//Added conditions for showing BACK button/
if(isset($_SESSION['brief']) && $_SESSION['brief']>=1){
$breif1Controller .= "<input type=\"submit\" name=\"brief1Back\" id=\"brief1Back\" value=\"BACK\" />";
}
//Here you can set the max number of briefings you've made//
$maxbrief=2;
if(!isset($_SESSION['brief']) || isset($_SESSION['brief']) && $_SESSION['brief']<$maxbrief){
$breif1Controller .= "<input type=\"submit\" name=\"brief1Go\" id=\"brief1Go\" value=\"CONTINUE\" />";
}
$breif1Controller .= "</form>";


if($brief1 == 0){
$brief1_info = "<b>Welcome Commander,</b> you are currently viewing the Mission Control Brief page, here you can view all your missions that you need to complete in order to advance through your prestiges. You will need to follow your briefs carefully in order to succeed.";
}
else if($brief1 == 1){
$brief1_info = "Okay, let me show you around the place ...";
}
else if($brief1 == 2){
$brief1_info = "brief is on 2";
}

//Added so I could test
echo "$breif1Controller";
if (isset($brief1_info)){
echo "$brief1_info";
}	
?>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.