form validation not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

form validation not working

Post by lloydie-t »

I am trying to introduce some form validation into a page. The validation I have attempted to create does not work, in that the forn is still posted.

I have gone though the posts for forms and cannot find any hints to help me. Any ideas?

The form checks to see if it has already been posted using


Code: Select all

if(!$submit) {



and it post to itself using action

Code: Select all

<?php echo $php_self; ?>



Please find the following code

Code: Select all

<?php &#125;
        else
        &#123;
include('pageconnect.php');
$connection = @mysql_connect($host, $user, $pass) or die ("Unable to connect to database");
mysql_select_db($db) or die ("Unable to select database: $db ");        

If (($cust_name == "") && ($cust_address == "") && ($postcode == "") && ($dforename == "") && ($dsurname == "") 
&& ($dusername == "")  && ($dpasword == "") && ($upload == "") && ($cust_email == "") )
&#123;
echo "You have not completed all required fields";
&#125;
else
&#123;
$sql="INSERT INTO customer (cont_type_id, cust_name, cust_address, postcode, cust_phone,
cust_fax, cust_mobile, cust_email, phone_in, router_in, pcaw, cont_expiry, prospect, prosp_follow, upload)
VALUES ('$cont_type_id', '$cust_name', '$cust_address', '$postcode', '$cust_phone',
'$cust_fax', '$cust_mobile', '$cust_email', '$phone_in', '$router_in', '$pcaw', '$cont_expiry', '$prospect', '$prosp_follow', '$upload')
";
mysql_query($sql) or die('An error occured executing sql statement.  SQL='.$sql.'<br>'.mysql_error());

$cust_id = mysql_insert_id();
$sql="INSERT INTO dept (cust_id, name, location)
VALUES ('$cust_id', '$dept_name', '$location')
";
mysql_query($sql) or die('An error occured executing sql statement.  SQL='.$sql.'<br>'.mysql_error());

$dept_id = mysql_insert_id();

$sql="INSERT INTO users (dept_id, username, password, defaultuser, forename, surname, job_title, user_address, 
user_postcode, user_phone, user_fax, user_mobile, user_email)
VALUES ('$dept_id', '$dusername', '$dpassword', 'Yes', '$dforename', '$dsurname', '$job_title', '$cust_address', '$postcode', 
'$cust_phone', '$cust_fax', '$cust_mobile', '$cust_email')
";
mysql_query($sql) or die('An error occured executing sql statement.  SQL='.$sql.'<br>'.mysql_error());
?>
<script type="text/javascript">
            location.href = "custconfirm.php";
        </script>
        
        <?php &#125;
        mysql_close($connection);
        &#125;
        
        ?>
User avatar
martin
Forum Commoner
Posts: 33
Joined: Fri Jun 28, 2002 12:59 pm
Location: Cambridgeshire

Post by martin »

If you mean it's not validating that would be your logic here

Code: Select all

If (($cust_name == "") && ($cust_address == "") && ($postcode == "") && ($dforename == "") && ($dsurname == "") 
&& ($dusername == "")  && ($dpasword == "") && ($upload == "") && ($cust_email == "") ) 
&#123; 
echo "You have not completed all required fields"; 
&#125;
You are saying IF this variable is empty AND this variable is empty etc.
Code should be OR(||)

Code: Select all

If (($cust_name == "") || ($cust_address == "") || ($postcode == "") || ($dforename == "") || ($dsurname == "") 
|| ($dusername == "")  || ($dpasword == "") || ($upload == "") ||($cust_email == "") ) 
&#123; 
echo "You have not completed all required fields"; 
&#125;
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Also, if you are using a newer version of PHP, then you need to read this article:

http://www.devnetwork.net/forums/viewtopic.php?t=511

It explains issues with register_globals which you may find quite useful since you are doing form validation.
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

Thanks Martin. Is working in a fashion. I now need to work out how to display the rest of the form when the validation finds fields not entered
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Further to what Martin said you could also rearrange your logic in the following way:

Code: Select all

<?php
if (!empty($cust_name) && !empty($cust_address) && !empty($postcode) && !empty($dforename) && !empty($dsurname) && !empty($dusername)  && !empty($dpasword) && !empty($upload) && !empty($cust_email)) &#123;
	// Everything has been added, process form data.
&#125; else &#123;
	// Something hasn't been filled in, deal with it here.
&#125;
?>
If you keep your form HTML in a separate file you can then just include that file when you've determined that something hasn't been filled in. The same effect could be achieved by putting the generation of the form's HTML into a function which you could call to recreate the form.

Mac
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

Thanks twigletmac. I kind of understand what you are saying, but I do not clearly understand functions yet. Can you point me in the right direction.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

if you don't want to read the manual about functions, you can still do what twinglet said without functions - just put your code where he has comments.
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

everything is fine now thanks for all your help
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

llimllib wrote:where he has comments
where she has comments :)

Mac
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

oops 8O my bad mac, there's too many men in our field, good to see a female voice out there
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

twigletmac wrote:
llimllib wrote:where he has comments
where she has comments :)

Mac
8O Never would have guessed 8O

Mind you the picture is a bit girly :lol:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

:P
Post Reply