Jump to content

[SOLVED] Problem with nested IF's


JoelRocks

Recommended Posts

Hello guys,

 

I am trying to create a basic registration script for my University project. Problem is nested if's are giving me a headache, i will post the code, the idea is you first enter your details your details and they get validated on register.php. If somone knows how to get register.php?step=1,2 then that would be a better idea.

 

At the moment nothing is being validated, i am still learning, sorry to be a pain,

 

Joel

 

<html>
<head>
	<title>Registration System V0.1</title>
</head>
<body>
<?php 
$first_name=$_POST["first_name"];
$surname=$_POST["surname"];
$username=$_POST["username"];
$email_address=$_POST["email_address"];
$confirm_email_address=$_POST["confirm_email_address"];
$terms_and_conditions=$_POST["terms_and_conditions"];

if ($first_name = "")
{
?>
    <p>
		Please enter your desired user details, if you have problems
		with anything, please contact the administrator.
	</p>
    <br />
    <form action="register.php" method="Post">
		First Name: 
			<input type="text" name="first_name" />
		<br />
		Surname: 
			<input type="text" name="surname" />
		<br />
		Username: 
			<input type="text" name="username" />
		<br />
		Email Address 
			<input type="text" name="email_address" />
		<br />
		Confirm Email Address 
			<input type="text" name="confirm_email_address" />
		<br />
		<br />
		In ticking this checkbox you are agreeing to abide by the
		following terms and conditions
		<input type="checkbox" name="terms_and_conditions" />
		<br />
		<br />
		<input type="submit" value="submit" />
	</form>
	<?
	}
	else
	{
		if ($first_name="")
		{
			$error = ("You must enter a firstname");
		}
		if ($surname="")
		{
			$error = ("You must enter a surname");
		}
		if ($email_address="")
		{
			$error = ("You must enter an email address"); 
		}
		if ($confirm_email_address="")
		{
			$error = ("You must confim your email address");
		}
		if ($email_address != $confirm_email_address)
		{
			$error = ("Sorry, the email addresses provided do not match");
		}
		if ($terms_and_conditions="")
		{
			$error = ("You must agree with our terms and conditions");
		}

	?>

		Please confirm the following details 

		<table> 
			<tr>
				<td>
					Firstname:
				</td>
				<td>
					<? echo ($first_name);?>
				</td>
			</tr>
			<tr>
				<td>
					Surname:
				</td>
				<td>
					<? echo ($surname);?>
				</td>
			</tr>
			<tr>
				<td>
					Username:
				</td>
				<td>
					<? echo ($username);?>
				</td>
			</tr>
			<tr>
				<td>
					Email Address:
				</td>
				<td>
					<? echo ($email_address);?>
				</td>
			</tr>
		</table>
		If you details are correct please click submit, if they are incorrect please go back.
		<form action="verify.php"> 
			<?
			$_POST["first_name"]=$first_name;
			$_POST["surname"]=$surname;
			$_POST["username"]=$username;
			$_POST["email_address"]=$email_address;
			$_POST["confirm_email_address"]=$confirm_email_address;
			$_POST["terms_and_conditions"]=$terms_and_conditions;
			?>
			<input type="submit" name="Submit">
		</form>
		<form action="register.php"> 
			<?
			$_POST["first_name"]=$first_name;
			$_POST["surname"]=$surname;
			$_POST["username"]=$username;
			$_POST["email_address"]=$email_address;
			$_POST["confirm_email_address"]=$confirm_email_address;
			$_POST["terms_and_conditions"]=$terms_and_conditions;
			?>
			<input type="submit" name="Back">
		</form>
	<?
	}
	?>
  </body>
  
</html>

Link to comment
https://forums.phpfreaks.com/topic/74216-solved-problem-with-nested-ifs/
Share on other sites

see comments,

i hope it makes sense

<html>
<head>
	<title>Registration System V0.1</title>
</head>
<body>
<?php 
$first_name=$_POST["first_name"];
$surname=$_POST["surname"];
$username=$_POST["username"];
$email_address=$_POST["email_address"];
$confirm_email_address=$_POST["confirm_email_address"];
$terms_and_conditions=$_POST["terms_and_conditions"];

if ($first_name = "")
{
?>
    <p>
		Please enter your desired user details, if you have problems
		with anything, please contact the administrator.
	</p>
    <br />
    <form action="register.php" method="Post">
		First Name: 
			<input type="text" name="first_name" />
		<br />
		Surname: 
			<input type="text" name="surname" />
		<br />
		Username: 
			<input type="text" name="username" />
		<br />
		Email Address 
			<input type="text" name="email_address" />
		<br />
		Confirm Email Address 
			<input type="text" name="confirm_email_address" />
		<br />
		<br />
		In ticking this checkbox you are agreeing to abide by the
		following terms and conditions
		<input type="checkbox" name="terms_and_conditions" />
		<br />
		<br />
		<input type="submit" value="submit" />
	</form>
	<?php
	}
	else
	{
		if ($first_name="") //WILL NEVER HAPPEN
		{
			$error = ("You must enter a firstname");
		}
		if ($surname="")
		{
			$error = ("You must enter a surname");
		}
		if ($email_address="")
		{
			$error = ("You must enter an email address"); 
		}
		if ($confirm_email_address="")
		{
			$error = ("You must confim your email address");
		}
		if ($email_address != $confirm_email_address)
		{
			$error = ("Sorry, the email addresses provided do not match");
		}
		if ($terms_and_conditions="")
		{
			$error = ("You must agree with our terms and conditions");
		}

	?>

		Please confirm the following details 

		<table> 
			<tr>
				<td>
					Firstname:
				</td>
				<td>
					<? echo ($first_name);?>
				</td>
			</tr>
			<tr>
				<td>
					Surname:
				</td>
				<td>
					<? echo ($surname);?>
				</td>
			</tr>
			<tr>
				<td>
					Username:
				</td>
				<td>
					<? echo ($username);?>
				</td>
			</tr>
			<tr>
				<td>
					Email Address:
				</td>
				<td>
					<? echo ($email_address);?>
				</td>
			</tr>
		</table>
		If you details are correct please click submit, if they are incorrect please go back.
		<form action="verify.php"> 
			<?php //erm.. what? 
			//should be 
			?>
			<input type="text" name="first_name" value="<?php echo $first_name;?>"/>
			<?php // use the above as a template for the test
			$_POST["surname"]=$surname;
			$_POST["username"]=$username;
			$_POST["email_address"]=$email_address;
			$_POST["confirm_email_address"]=$confirm_email_address;
			$_POST["terms_and_conditions"]=$terms_and_conditions;
			?>
			<input type="submit" name="Submit">
		</form>
		<form action="register.php"> 
			<?php //erm.. what? (see above)
			$_POST["first_name"]=$first_name;
			$_POST["surname"]=$surname;
			$_POST["username"]=$username;
			$_POST["email_address"]=$email_address;
			$_POST["confirm_email_address"]=$confirm_email_address;
			$_POST["terms_and_conditions"]=$terms_and_conditions;
			?>
			<input type="submit" name="Back">
		</form>
	<?php
	}
	?>
  </body>
  
</html>

Thanks both, i have corrected the whole == thing,

 

techie where i am defining the post variables at the bottom of the page is simply to post them, so when i re-load register.php i can set the values for the inputs so they are basically what the user has entered... with me?

 

I have also added errors, but if error has more than one value... (see the code bellow) is there a way of echoing them all?

 

Thanks,

 

Joel

 

<html>
<head>
	<title>Registration System V0.1</title>
</head>
<body>
<?php 
$first_name=$_POST["first_name"];
$surname=$_POST["surname"];
$username=$_POST["username"];
$email_address=$_POST["email_address"];
$confirm_email_address=$_POST["confirm_email_address"];
$terms_and_conditions=$_POST["terms_and_conditions"];

if ($first_name == "")
{
?>
    <p>
		Please enter your desired user details, if you have problems
		with anything, please contact the administrator.
	</p>
    <br />
    <form action="register.php" method="Post">
		First Name: 
			<input type="text" name="first_name" />
		<br />
		Surname: 
			<input type="text" name="surname" />
		<br />
		Username: 
			<input type="text" name="username" />
		<br />
		Email Address 
			<input type="text" name="email_address" />
		<br />
		Confirm Email Address 
			<input type="text" name="confirm_email_address" />
		<br />
		<br />
		In ticking this checkbox you are agreeing to abide by the
		following terms and conditions
		<input type="checkbox" name="terms_and_conditions" />
		<br />
		<br />
		<input type="submit" value="submit" />
	</form>
	<?
	}
	else
	{
		if ($first_name == "")
		{
			$error = ("You must enter a firstname");
		}
		if ($surname == "")
		{
			$error = ("You must enter a surname");
		}
		if ($email_address == "")
		{
			$error = ("You must enter an email address"); 
		}
		if ($confirm_email_address == "")
		{
			$error = ("You must confim your email address");
		}
		if ($email_address != $confirm_email_address)
		{
			$error = ("Sorry, the email addresses provided do not match");
		}
		if ($terms_and_conditions == "")
		{
			$error = ("You must agree with our terms and conditions");
		}

	?>

		You have the following errors: 
		<br />
		<?
			echo ($error);
		?>
		<br />
		<br />
		Please confirm the following details 

		<table> 
			<tr>
				<td>
					Firstname:
				</td>
				<td>
					<? echo ($first_name);?>
				</td>
			</tr>
			<tr>
				<td>
					Surname:
				</td>
				<td>
					<? echo ($surname);?>
				</td>
			</tr>
			<tr>
				<td>
					Username:
				</td>
				<td>
					<? echo ($username);?>
				</td>
			</tr>
			<tr>
				<td>
					Email Address:
				</td>
				<td>
					<? echo ($email_address);?>
				</td>
			</tr>
		</table>
		If you details are correct please click submit, if they are incorrect please go back.
		<form action="verify.php"> 
			<?
			$_POST["first_name"]=$first_name;
			$_POST["surname"]=$surname;
			$_POST["username"]=$username;
			$_POST["email_address"]=$email_address;
			$_POST["confirm_email_address"]=$confirm_email_address;
			$_POST["terms_and_conditions"]=$terms_and_conditions;
			?>
			<input type="submit" name="Submit">
		</form>
		<form action="register.php"> 
			<?
			$_POST["first_name"]=$first_name;
			$_POST["surname"]=$surname;
			$_POST["username"]=$username;
			$_POST["email_address"]=$email_address;
			$_POST["confirm_email_address"]=$confirm_email_address;
			$_POST["terms_and_conditions"]=$terms_and_conditions;
			?>
			<input type="submit" name="Back">
		</form>
	<?
	}
	?>
  </body>
  
</html>

techie where i am defining the post variables at the bottom of the page is simply to post them, so when i re-load register.php i can set the values for the inputs so they are basically what the user has entered... with me?

i know what your trying to do.. but you have add them into a form.. with a submit button.. that submit button will have no effect..

$_POST["username"]=$username;

will work with an include but NOT on a post (that the submit button will perform)..

 

 

I have also added errors, but if error has more than one value... (see the code bellow) is there a way of echoing them all?

 

Ok see example

	<?php
	}
	else
	{
		$error = array(); //ErrorArray
		if ($first_name == "")
		{
			$error[] = "You must enter a firstname";
		}
		if ($surname == "")
		{
			$error[] = "You must enter a surname";
		}
		if ($email_address == "")
		{
			$error[] = "You must enter an email address"; 
		}
//SNIPP....

	?>

		You have the following errors: 
		<br />
		<?php
			//Display errors
			foreach($error as $e)
			{
				echo "$e<br>";
			}
		?>

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.