CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2006
    Posts
    11

    Question 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?

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    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.

  3. #3
    Join Date
    Dec 2004
    Posts
    438

    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?

  4. #4
    Join Date
    Mar 2007
    Posts
    13

    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.

  5. #5
    Join Date
    May 2002
    Posts
    10,943

    Re: validation using a function call in PHP

    You mean like...

    &#91;code&#93;
    code goes here
    &#91;/code&#93;
    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
  •  





Click Here to Expand Forum to Full Width

Featured