Page 1 of 1

Pass value through if statements

Posted: Sat Aug 12, 2006 9:24 pm
by waradmin
Im still working on a form to do account creation for a server im starting, and it uses a variety of if statements to pass form data, or atleast thats the idea.

For example:

Code: Select all

if($_GET['action'] == 'registered')
					{
						include('config.php');
						$conn=@mysql_connect("$DBHOST", "$DBUSER", "$DBPASS")
		 					or die("Err:Conn");
						#select the specified database
						$rs = @mysql_select_db("$DBNAME", $conn) 
							or die("Err:Db");
						$result = mysql_query("SELECT * FROM verify
						WHERE ip='{$_SERVER['REMOTE_ADDR']}'") or die(mysql_error());
						$row = mysql_fetch_array( $result );
						$ark = $row['ark'];
						echo "<b>Your Account Creation Key (ARK) has been filled into the form, please continue registration.</b>";
						echo "<b>Once you have recieved your ARK key in your email address, please fill out the rest of the form:</b><br>";
						echo "<form method=\"post\" action=\"index.php?action=arkcheck\">ARK: <input type=\"text\" name=\"ark\" value=\"$ark\">
						<br>Username: <input type=\"text\" name=\"username\">
						<br>Password: <input type=\"text\" name=\"password\">
						<br><input type=\"submit\" value=\"Finish\"></form>";
						mysql_close();
					}
Takes the username and password and passes it to:

Code: Select all

if($_GET['action'] == 'arkcheck')
					{
						$ip = $_SERVER['REMOTE_ADDR'];
						include('config.php');
						$conn=@mysql_connect("$DBHOST", "$DBUSER", "$DBPASS")
		 					or die("Err:Conn");
						#select the specified database
						$rs = @mysql_select_db("$DBNAME", $conn) 
							or die("Err:Db");
						$result = mysql_query("SELECT * FROM verify
						WHERE ip='{$_SERVER['REMOTE_ADDR']}'") or die(mysql_error());
						$row = mysql_fetch_array( $result );
						$ark = $_POST['ark'];
						if($ark != $row['ark'])
						{
							echo "ARK does not match up with the database. Please click <a href=\"javascript: history.go(-1)\">here</a> to go back.";
							exit();
						}
						else
						{
							echo "<SCRIPT LANGUAGE=\"JavaScript\">
							window.location=\"index.php?action=continue\";
							</script>";
						}
					}
And on a successful verification of the ARK key (which is what the arkcheck if statement does, there is no user input in that statement), it automaticly redirects to the action continue which is supposed to put the information from the intiial form into a database. However it seems as if the data from the first form is lost during the movement through all of the if statements because the database never gets populated with the username, password, and email address (which is requested in the initial form that i didnt show you).

My question, how do you pass form information through multiple if statements so they are avalible 3-4 if statements later.

Thanks in advance

-Steve

Posted: Sun Aug 13, 2006 12:34 am
by RobertGonzalez
Look closely at your logic. To answer your question, vars are available in their current state until a) the script ends or b) you change the var programmatically. Here is an example...

Code: Select all

<?php

$var = false;

if ($var)
{
    // This will not execute because $var is false
}

if (!$var)
{
    // This will execute because $var is false
    // lets change it
    $var = true;
}

if ($var)
{
    // This will now execute because we changed $var
}
?>
As you can see, you can use the same var as a conditional throughout your script no matter how many ifs you go.

Posted: Mon Aug 14, 2006 12:16 am
by waradmin
I did look close at my logic, and it didnt make much sense but it was the conclusion I reached. Why? Because in say if ($_GET['action'] == 'form') has a form in it that is a POST form, where when submit is pressed it takes you to http://whatever.com/index.php?action=continue, I was able to assign the email entry in the form to $email (by doing $email = $_POST['email'];) HOWEVER when moving from action=continue to say action=finished I was no longer able to echo $email and recieve a value. ONLY when i made a form with hidden fields and populated the form fields (value=) with $email and have a submit button to go to the next if statement was I able to pass the values, if I just had it automaticly redirect to the next if statement, the $email showed up as nothing.

-Steve

Posted: Mon Aug 14, 2006 12:20 am
by RobertGonzalez
So are you developing a stepped application? Something along the lines of 'Submit step 1 to Step 2, then submit Step 2 to Step 3' etc? There are ways to do that programmatically without having to force your action value in your form.

Posted: Mon Aug 14, 2006 1:06 am
by waradmin
Yeah it goes the initial form where email address is filled in, then the second part where your username and password are filled in, then the 4th if statement, or 5th, i dont remember is where the info is put into the DB, however for some reason the values for $username, $password, and $email (all defined by $____ = $_POST['___']; in the if statement the form goes to) are always set to 0 after they reach the second if. On the FIRST if they remain holding their values, on the second if, they lose their values.

The form can be found at http://swgcluster1.no-ip.org/register

-Steve

Posted: Mon Aug 14, 2006 1:17 am
by RobertGonzalez
Why not use the posted information in hidden fields in the form. And have a hidden field called step that can track where you are?

Code: Select all

<?php
$step = ( isset($_POST['step']) ) ? $_POST['step'] : 0;

if ($step == 5)
{
    // We just posted the last step, do something different
}
else
{
    echo '<form id="myform" action="' . basename(__FILE__) . '">';
    switch ($step) 
    {
        case 0:
            // Fresh page load, handle the first form display
            break;

        case 1:
            // Handle the first step processing and 
            // set hidden fields for next step
            break;

        case 2:
            // Handle the second step processing and 
            // set hidden fields for next step
            break;

        case 3:
            // Handle the third step processing and 
            // set hidden fields for next step
            break;

        case 4:
            // Handle the fourth step processing and 
            // set hidden fields for next step
            break;
    }
    echo '</form>';
}
?>