Web Design Forum: Else statment viewed before anything else... - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Else statment viewed before anything else... Rate Topic: -----

#1 User is offline   Adam 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 76
  • Joined: 05-July 09
  • Reputation: 0

Posted 10 March 2010 - 11:39 AM

Now, firstly I appologise as Im not at my laptop where I have the script in question but heres the problem, when
ever I run a script that does the following...

I have a form on a page which has a form with a text field which when submitted would echo something like
'Thanks for your deposit'

the else conditional would say 'The amount you entered was invalid'


Now when I run this script, before any amount has been submitted id get the 'else' conditional echoing on the page, above the form.

Without seeing my exact script chan this be rectified by knowing the above?
0

#2 User is offline   TopShopper 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 878
  • Joined: 21-July 09
  • Reputation: 14
  • Gender:Male
  • Location:Cardiff, UK
  • Experience:Intermediate
  • Area of Expertise:Coder

Posted 10 March 2010 - 11:43 AM

Can't really say without seeing script, but I would say that there is a syntax error in your PHP or ASP - perhaps a misplaced quotes " for instance.
0

#3 User is offline   Jay Gilford 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,104
  • Joined: 11-October 09
  • Reputation: 185
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 10 March 2010 - 11:45 AM

Sounds to me like you've not checked to see if the page has been submitted or not. If you put
if(isset($_POST)) {

// .. Your php code here ..

}


That should solve it (change $_POST to $_GET if you're using the GET method with your form)
0

#4 User is offline   Adam 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 76
  • Joined: 05-July 09
  • Reputation: 0

Posted 10 March 2010 - 11:52 AM

Hmm Im pretty sure that I was checking that it had been submitted first


but, to be sure I best get off my lazy arse and grab the laptop out the car...its further than it sounds.
0

#5 User is offline   Jay Gilford 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,104
  • Joined: 11-October 09
  • Reputation: 185
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 10 March 2010 - 11:53 AM

hehe run! :D
If you post the code it will help that's for sure. It was only a guess
0

#6 User is offline   Adam 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 76
  • Joined: 05-July 09
  • Reputation: 0

Posted 10 March 2010 - 12:24 PM

Ok check this badboy out...

heres the problems Ive got

1. When you dont fill out a textfield. ie leave blank no errors are displayed, UNLESS the password fields dont match.

2. I get the error message

Warning: Invalid arguement supplied Foreach() in ...Line 111

3. As I originally posted I get the Error message of 'Error, the following errors occured' before anything is submitted. And ironically it doesnt even list the errors array..

These are the three main ones


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" media="screen" type="text/css" />

<title>Untitled Document</title>
</head>
<body>
<?php 

$page_title ='register.php';


//Initialize an errors array...
$errors = array ();

//Check First name
if (empty($_POST['first_name'])) 
{
	$errors [] = 'You forgot to enter a First name';
}

else
{
	$fn = trim($_POST['first_name']);
}



//Check Last Name

if (empty($_POST['last_name'])) 
{
	$errors [] = 'You forgot to enter a Last name';
}

else
{
	$ln = trim($_POST['last_name']);
}

// Check Email address

if (empty($_POST['email'])) 
{
	$errors [] = 'You forgot to enter an Email Address';
}

else
{
	$e = trim($_POST['email']);
	
}

// Check Password

if (!empty($_POST['pass1'])) 
{
	if ($_POST['pass1'] != $_POST['pass2']) 
	{
		$errors [] = 'Your passwords didnt match';
		
	}

	else
	{ 
	$p = trim ($_POST['pass1']);
	}
}
	else
	{
		$errors ="You forgot to enter a password";
	}

if (empty ($errors)) 
{
	
//Register the user in the database

require_once 
('mysqli_connect.php');

//make the query

$q = "INSERT INTO users (first_name, last_name, email, pass) VALUES ('$first_name', '$last_name', '$email', SHA1('$pass') )";

$r = mysqli_query ($dbc, $q) ;

//The above code runs the query

if ($r) {
	
	echo " Thanks for registering, ";
}
else 
{
	echo '<p class="errors"> you could not be registered due to a system error</p>';

}

mysqli_close($dbc);
exit();
}

else {
	echo '<h1>Error</h1>
	<p class="errors"> The following errors occured:<br/>';
	
	foreach 
	($errors as $msg) 
	{
		echo  "-$msg <br/>\n";
	}
	
}

//Below is the fields for the form

?>


<h1>Register</h1>
<form action="register.php" method="post" class="myForm">
First Name:<input type="text" name="first_name" size="15" maxlength="20" value=" 

<?php

if (isset($_POST['first_name']))
echo $_POST['first_name'];
?> " /><br />

Last Name:<input type="text" name="last_name" size="15" maxlength="40" value=" 

<?php

if (isset($_POST['last_name']))
echo $_POST['last_name'];
?> " /><br />


Email:<input type="text" name="email" size="20" maxlength="80" value=" 

<?php

if (isset($_POST['email']))
echo $_POST['email'];
?> " />
<br>
Password: <input type="password" name="pass1" size="10" maxlength="20" />
<br />
Confirm Password <input type="password" name="pass2" size="10" maxlength="20" /><br />

<input type="submit" name="submit" value="register" /><br />

<input type="hidden" name="submitted" value="TRUE" /><br />
</form>

 
</body>
</html>



Now I want you to know Ive not come running to you guys, ive spent ages changing the code trying to work this out, but I have conceeded, I need some guidance...

After all I think you all owe me, before I came to this site I had a passing interesst in webdesign, then someone suggested I buy a book and have subsequently spent the last month pulling my hair out.
0

#7 User is offline   Jay Gilford 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,104
  • Joined: 11-October 09
  • Reputation: 185
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 10 March 2010 - 12:33 PM

OK, well I would personally rewrite the code. It's very "scatty" tbh. You should just do one check on whether the form has been submitted in one if test. Check out here http://www.jaygilfor...using-one-page/ for how to do it
0

#8 User is offline   Adam 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 76
  • Joined: 05-July 09
  • Reputation: 0

Posted 10 March 2010 - 01:26 PM

Really? Scatty?

Thats good to know as I thought it looked a little rubbish, Im following a book. Which although may not be the best practice, it has taught me a lot about PHP.

Still some confusing elements though

eg. make sence of this part of the code for me

foreach         ($errors as $msg)         {           
     echo  "-$msg <br/>\n";        }


especialy the logic behind "-$msg"


Cheers
0

#9 User is online   rallport 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 3,814
  • Joined: 03-January 10
  • Reputation: 266
  • Gender:Male
  • Location:England, UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 10 March 2010 - 01:35 PM

View PostAdam, on 10 March 2010 - 01:26 PM, said:

Really? Scatty?

Thats good to know as I thought it looked a little rubbish, Im following a book. Which although may not be the best practice, it has taught me a lot about PHP.

Still some confusing elements though

eg. make sence of this part of the code for me

foreach ($errors as $msg) { 
 echo "-$msg <br/>\n"; }


especialy the logic behind "-$msg"


Cheers



You're just outputting each element of an array. When you're checking your posted data if it meets a certain condition you add a element to the $errors array. the "-$msg" is the array element and simply produces a list of errors with a dash before each line. E.g.

if (empty($_POST['last_name'])) 
{
 //add a element to your errors array
 $errors [] = 'You forgot to enter a Last name';
}


To see this more easily, add print_r($errors) at the end of your script. Enter some invalid data to see the content of the $errors array.
2

#10 User is offline   Jay Gilford 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,104
  • Joined: 11-October 09
  • Reputation: 185
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 10 March 2010 - 01:38 PM

Yeah it's just echoing out - followed by the message

Out of interest, what book are you following?
0

#11 User is offline   Adam 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 76
  • Joined: 05-July 09
  • Reputation: 0

Posted 10 March 2010 - 01:41 PM

Oh ok, I was curious about the dash more than anything.

Thanks for the reply.

After reading the above guys comments about rewritting the script, I want some further advice before I go back to the drawing board.

Why exactly am I going wrong in this script? Whats making it look ugly and not perform correctly?

I understand your not here to write it for me but if I start again I dont want to make the same mistakes.

Thanks
Adam
0

#12 User is offline   Adam 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 76
  • Joined: 05-July 09
  • Reputation: 0

Posted 10 March 2010 - 01:43 PM

View PostJay Gilford, on 10 March 2010 - 01:38 PM, said:

Yeah it's just echoing out - followed by the message

Out of interest, what book are you following?



PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (Visual QuickPro Guides)

Advised by one of the other guys on here.

A lot of it makes sence, but does not always work the way he intend...more my fault than his I imagine.
0

#13 User is offline   Jay Gilford 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,104
  • Joined: 11-October 09
  • Reputation: 185
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 10 March 2010 - 02:04 PM

Well this is just an example, but something like this is better IMO

$errors = array();

//Check that form has been submitted
if(isset($_POST)) {

	// Loop through all posted form variables
	foreach($_POST as $k => $v) {
		
		// Trim the variable
		$_POST[$k] = trim($_POST[$k]);
		
		// If it's empty add error "You forgot to enter field_name_here"
		if(empty($_POST[$k])) $errors[] = 'You forgot to enter '.$k;
	}
	
	//Any additional error checking such as password check
	if($_POST['pass1'] != $_POST['pass2']) {
		$errors[] = 'Passwords do not match';
	}

	// Check for error count here and insert into database if none
	if(empty($errors)) {
		// Put mysql insert code here
	}
} 


Also just so you know, the isnert query you have is wrong and won't actually insert anything since you have $fn when defining the first name and $firstname in the query. Its the same for last name too

One final note. When creating queries it's much better to use something like sprintf to build your query with your variables rather than just sticking them into the string directly. If you use the mressf function I created it will also run each through the myqsl_real_escape_string function too to prevent mysql injection attacks
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users