Page 1 of 1

form validation not working

Posted: Mon Jul 08, 2002 8:44 am
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;
        
        ?>

Posted: Mon Jul 08, 2002 8:59 am
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;

Posted: Mon Jul 08, 2002 9:22 am
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.

Posted: Mon Jul 08, 2002 9:24 am
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

Posted: Mon Jul 08, 2002 9:34 am
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

Posted: Mon Jul 08, 2002 9:58 am
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.

Posted: Mon Jul 08, 2002 10:13 am
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.

Posted: Mon Jul 08, 2002 11:21 am
by lloydie-t
everything is fine now thanks for all your help

Posted: Mon Jul 08, 2002 11:27 am
by twigletmac
llimllib wrote:where he has comments
where she has comments :)

Mac

Posted: Mon Jul 08, 2002 12:35 pm
by llimllib
oops 8O my bad mac, there's too many men in our field, good to see a female voice out there

Posted: Tue Jul 09, 2002 10:08 am
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:

Posted: Tue Jul 09, 2002 10:11 am
by twigletmac
:P