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?
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
}
?>
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?
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.
Re: validation using a function call in PHP
You mean like...
[code]
code goes here
[/code]