Page 1 of 1
PHP Validation
Posted: Thu Jan 07, 2010 5:32 am
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> </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> </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..!
Re: PHP Validation
Posted: Thu Jan 07, 2010 5:37 am
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> </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.
Re: PHP Validation
Posted: Thu Jan 07, 2010 5:55 am
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> </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?
Re: PHP Validation
Posted: Thu Jan 07, 2010 5:58 am
by aravona
whoops! set the wrong one,
Code: Select all
</td></tr><tr><td> </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.
Re: PHP Validation
Posted: Thu Jan 07, 2010 6:23 am
by Jay87
aravona wrote:whoops! set the wrong one,
Code: Select all
</td></tr><tr><td> </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....
Re: PHP Validation
Posted: Thu Jan 07, 2010 7:19 am
by Jay87
changed the code to....
Code: Select all
</td></tr><tr><td> </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...
Re: PHP Validation
Posted: Fri Jan 08, 2010 3:50 am
by Jay87
Ok i got the script to work with the following code:
Code: Select all
</td></tr><tr><td> </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> </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!