PHP Validation

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
Jay87
Forum Commoner
Posts: 61
Joined: Thu Jan 07, 2010 5:22 am

PHP Validation

Post by Jay87 »

Hi, i am new to PHP and could do with some help.....

Basically i need to update some script to add some validation....

The current code checks the 'desc' field and if it blanks it brings an error message, as you can see from this code:

Code: Select all

</td></tr><tr><td>&nbsp;</td></tr><tr><td align=center><button class=flat2 onclick="window.onbeforeunload=null;if(document.forms[0].desc.value==''){alert('Please enter a subject');return false;} else {document.forms[0].submit();return false;}">Enter Call</button><button class=flat2 onclick="window.onbeforeunload=null;document.location='log_be.php?cancel=1&id=<? echo $nt . "&pfx=" . $_SESSION['prefix'];?>'";>Cancel Call</button></td></tr>
What i want to do is add another validation field to the 'user' field (also can't be left blank), i tried adding the following code:

Code: Select all

or(document.forms[0].user.value==''){alert('Please enter a user');return false;} else {document.forms[0].submit();return false;}
this was added to the first bit of code, so it looked like:

Code: Select all

</td></tr><tr><td>&nbsp;</td></tr><tr><td align=center><button class=flat2 onclick="window.onbeforeunload=null;if(document.forms[0].desc.value==''){alert('Please enter a subject');return false;}or(document.forms[0].user.value==''){alert('Please enter a user');return false;} else {document.forms[0].submit();return false;} else {document.forms[0].submit();return false;}">Enter Call</button><button class=flat2 onclick="window.onbeforeunload=null;document.location='log_be.php?cancel=1&id=<? echo $nt . "&pfx=" . $_SESSION['prefix'];?>'";>Cancel Call</button></td></tr>
but this doesn't work...!

help please..!
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: PHP Validation

Post by aravona »

I dont think you can have two else after an if. Try making the middle else an elseif

Code: Select all

</td></tr><tr><td>&nbsp;</td></tr><tr><td align=center><button class=flat2 onclick="window.onbeforeunload=null;if(document.forms[0].desc.value==''){alert('Please enter a subject');return false;}or(document.forms[0].user.value==''){alert('Please enter a user');return false;} else {document.forms[0].submit();return false;} [b]elseif[/b] {document.forms[0].submit();return false;}">Enter Call</button><button class=flat2 onclick="window.onbeforeunload=null;document.location='log_be.php?cancel=1&id=<? echo $nt . "&pfx=" . $_SESSION['prefix'];?>'";>Cancel Call</button></td></tr>
Currently your trying to tell your code to do two final options.
Jay87
Forum Commoner
Posts: 61
Joined: Thu Jan 07, 2010 5:22 am

Re: PHP Validation

Post by Jay87 »

aravona wrote:I dont think you can have two else after an if. Try making the middle else an elseif

Code: Select all

</td></tr><tr><td>&nbsp;</td></tr><tr><td align=center><button class=flat2 onclick="window.onbeforeunload=null;if(document.forms[0].desc.value==''){alert('Please enter a subject');return false;}or(document.forms[0].user.value==''){alert('Please enter a user');return false;} else {document.forms[0].submit();return false;} [b]elseif[/b] {document.forms[0].submit();return false;}">Enter Call</button><button class=flat2 onclick="window.onbeforeunload=null;document.location='log_be.php?cancel=1&id=<? echo $nt . "&pfx=" . $_SESSION['prefix'];?>'";>Cancel Call</button></td></tr>
Currently your trying to tell your code to do two final options.

didn't work mate.

any other ideas?
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: PHP Validation

Post by aravona »

whoops! set the wrong one,

Code: Select all

</td></tr><tr><td>&nbsp;</td></tr><tr><td align=center><button class=flat2 onclick="window.onbeforeunload=null;if(document.forms[0].desc.value==''){alert('Please enter a subject');return false;}or(document.forms[0].user.value==''){alert('Please enter a user');return false;} [b]elseif[/b] {document.forms[0].submit();return false;} else {document.forms[0].submit();return false;}">Enter Call</button><button class=flat2 onclick="window.onbeforeunload=null;document.location='log_be.php?cancel=1&id=<? echo $nt . "&pfx=" . $_SESSION['prefix'];?>'";>Cancel Call</button></td></tr>
forgive me, going mad over data entry here. Try changing that one I accidently marked the last.
Jay87
Forum Commoner
Posts: 61
Joined: Thu Jan 07, 2010 5:22 am

Re: PHP Validation

Post by Jay87 »

aravona wrote:whoops! set the wrong one,

Code: Select all

</td></tr><tr><td>&nbsp;</td></tr><tr><td align=center><button class=flat2 onclick="window.onbeforeunload=null;if(document.forms[0].desc.value==''){alert('Please enter a subject');return false;}or(document.forms[0].user.value==''){alert('Please enter a user');return false;} elseif {document.forms[0].submit();return false;} else {document.forms[0].submit();return false;}">Enter Call</button><button class=flat2 onclick="window.onbeforeunload=null;document.location='log_be.php?cancel=1&id=<? echo $nt . "&pfx=" . $_SESSION['prefix'];?>'";>Cancel Call</button></td></tr>
forgive me, going mad over data entry here. Try changing that one I accidently marked the last.
didn't work, its not erroring when i run it just doesn't do anything when i hit enter call....
Jay87
Forum Commoner
Posts: 61
Joined: Thu Jan 07, 2010 5:22 am

Re: PHP Validation

Post by Jay87 »

changed the code to....

Code: Select all

</td></tr><tr><td>&nbsp;</td></tr><tr><td align=center><button class=flat2 onclick="window.onbeforeunload=null;if(document.forms[0].desc.value=='' || document.forms[0].enduser.value==''){alert('Please enter a subject');return false;} else {document.forms[0].submit();return false;}">Enter Call</button><button class=flat2 onclick="window.onbeforeunload=null;document.location='log_be.php?cancel=1&id=<? echo $nt . "&pfx=" . $_SESSION['prefix'];?>'";>Cancel Call</button></td></tr>
still not working though...
Jay87
Forum Commoner
Posts: 61
Joined: Thu Jan 07, 2010 5:22 am

Re: PHP Validation

Post by Jay87 »

Ok i got the script to work with the following code:

Code: Select all

</td></tr><tr><td>&nbsp;</td></tr><tr><td align=center><button class=flat2 onclick="window.onbeforeunload=null;if(document.forms[0].desc.value=='' || document.forms[0].user.value=='' || document.forms[0].details.value==''){alert('Missing valid fields');return false;} else {document.forms[0].submit();return false;}">Enter Call</button><button class=flat2 onclick="window.onbeforeunload=null;document.location='log_be.php?cancel=1&id=<? echo $nt . "&pfx=" . $_SESSION['prefix'];?>'";>Cancel Call</button></td></tr>
 
I then tried similar validation code on another form button, with this code:

Code: Select all

</td></tr><tr><td>&nbsp;</td></tr><tr><td align=center><button class=flat2 onclick="window.onbeforeunload=null;if(document.forms[0].address.value=='' || document.forms[0].desc.value=='' || document.forms[0].postcode.value=='' || document.forms[0].user.value=='' || document.forms[0].details.value=='' || document.forms[0].model.value=='' || document.forms[0].serial.value==''){alert('Missing valid fields');return false;} else {document.forms[0].submit();return false;}">Enter Call</button>
 
This code is identical to the 1st code i posted (just changed the fields i wanted to validate....)

But this code doesn't work, for some reason all the fields validate except the 'desc' field which when i enter data into it and press 'enter call' button nothing happens (no error, just stay the same- nothing loads or anything). When i leave the 'desc' field blank and test the other fields validation it brings the error message though....

So i then deleted the 'desc' from the code and then the 'postcode' field does the exact same thing and if i delete the postcode field the 'address' field did the same.......

Any reason why this is happening??

It's v frustrating...!

HELP!
Post Reply