|
-
March 27th, 2007, 08:14 AM
#1
validation using a function call in PHP
I am new to PHP.I am trying to create users using HTML controls and the validation is done in PHP as follows.I have used a template
my form is as follows......Createnewuser.php is the name of the file
<form method="post" action="Createnewuser.php" onSubmit="fun()">
<input type="text" name="txt_username"></td>
<input type="text" name="txt_userid">
<input type="password" name="txt_password" >
<input type="password" name="txt_cfrm_password">
<input type="text" name="txt_emailid">
<td><input type="submit" name="Submit" value="Create" >
<input type="submit" name="Submit" value="Cancel" onClick="Cancel_data()"></td>
</form>
*****positioning is not done properly******
once the html tag(</html>)is closed i have written the validation function fun() as follows:
function fun()
{
<?php
$flag=0;
include("Databaseconnect.php"); /*dataconnection file*/
$username=$_POST['txt_username'];
$password=$_POST['txt_password'];
$userid=$_POST['txt_userid'];
$mailid=$_POST['txt_emailid'];
$cfrm_password=$_POST['txt_cfrm_password'];
if( empty($username) || empty($userid) || empty($password) || empty($cfrm_password) || empty($mailid) )
{
$flag=1;
echo "value=$username";
echo "value=$userid"; /*trying to set values as entered*/
echo "value=$password";
echo "value=$mailid";
echo "<script language=javascript>alert('Please fill the blank fields.')</script>";
}
elseif(!(strlen($password)==strlen( $cfrm_password)))
{
$flag=1;
echo "<script language=javascript>alert('Passwords don't match')</script>";
}
elseif(!($password==$cfrm_password))
{
$flag=1;
echo "<script language=javascript>alert('password and confirm password don't match')</script>";
}
elseif(!ereg("@",$mailid))
{
$flag=1;
echo "<script language=javascript>alert('invalid emailaddress')</script>";
}
?>
}
These alert boxes are working.But when the page is loaded for the first time also i am getting the alertboxes with page on background.what is the solution?
-
March 27th, 2007, 08:36 AM
#2
Re: validation using a function call in PHP
First off, on form submission, it is best to put the PHP above the form.
Code:
<html>
<body>
<?php
...
?>
<form>
...
</form>
</body>
</html>
Second, you need to check if the information is posted first.
PHP Code:
<?php
if(isset($_POST['one of your variables'])){
// do your validation here
}
?>
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 28th, 2007, 05:08 AM
#3
Re: validation using a function call in PHP
To further clarify, do you need a method breaking up the page so that the first use just displays the form and then once submitted it checks the form until correct?
-
March 30th, 2007, 08:58 AM
#4
Re: validation using a function call in PHP
Hi CanUHelpMe, please use code tags when posting code. It makes things easier to read. You would use them in the following manner:
"["."code"."]".
"your code here".
"[/".code"."]"
Note: quit the quotes and doits.
Last edited by wilz04; March 30th, 2007 at 09:01 AM.
-
March 30th, 2007, 09:21 AM
#5
Re: validation using a function call in PHP
You mean like...
[code]
code goes here
[/code]
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|