Results 1 to 3 of 3

Thread: Form Submission with Validation

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Form Submission with Validation

    Hi,

    I have a problem with form submission.

    Situation
    I have 3 .php files.
    one.php -> Main file to run. If textbox filled with data then it is redirected to three.php.
    two.php -> This file is to refer a friend. If email textbox is filled with data then it is redirected to three.php. It is included in the one.php.
    three.php -> Result page. Either Submission from one.php or two.php redirect to this page on successful submission.

    Problem
    one.php -> On clicking the submit button without properly checking the php and javascript validation the page is redirected to three.php.
    two.php -> On clicking the submit button without checking the php validation the page is redirected to three.php.

    Code: one.php
    PHP Code:
    <?php

    if(isset($_POST["t"]))
    {
        
    header("Location: three.php");
    }

    ?>

    <html>
    <head>
    <title>Test Script</title>
    <script language="javascript">
    function validate(thisform)
    {
          if(document.f.t.value=="")
          {
                alert("Please enter name");
                document.f.t.focus();
                return false;
          }
    }
    </script>
    </head>
    <body>
    <form name="f" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>" onsubmit="return validate(this)">
    Name<input type="text" name="t" size="20"><br>
    <input type="submit" name="b" value="Submit">
    </form>
    </body>
    </html>

    <?php

    include("two.php");

    ?>
    Code: two.php
    PHP Code:
    <?php

    if(isset($_POST["email"]))
    {
        
    $email $_POST["email"];
        
    mail($email"Test""Test Mail""From: sender <[email protected]>");
        
    header("Location: three.php");
    }

    ?>

    <form name="f" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
    <table border="0">
        <tr>
            <td>
                Write Email ID Here <input type="text" name="email" size="20">
            </td>
            <td>
                <input type="submit" name="b" value="Submit">
            </td>
        </tr>
    </table>
    </form>
    Code: three.php
    PHP Code:
    <?php

    echo "Mail Sent Successfully";

    ?>
    I need your assistance to understand the problem and help to solve the situation. Thank you so much.

  2. #2
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: Form Submission with Validation

    You should take a look at this function to determine if they typed in something:
    http://www.php.net/manual/en/function.empty.php
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Form Submission with Validation

    You are including two.php inside one.php. So you have two forms and the same page; both are which are going to be sent to one.php.

    After inclusion your entire page will look like this:
    PHP Code:
    <?php

    if(isset($_POST["t"]))
    {
        
    header("Location: three.php");
    }

    ?>

    <html>
    <head>
    <title>Test Script</title>
    <script language="javascript">
    function validate(thisform)
    {
          if(document.f.t.value=="")
          {
                alert("Please enter name");
                document.f.t.focus();
                return false;
          }
    }
    </script>
    </head>
    <body>
    <form name="f" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>" onsubmit="return validate(this)">
    Name<input type="text" name="t" size="20"><br>
    <input type="submit" name="b" value="Submit">
    </form>
    </body>
    </html>
    <?php


    if(isset($_POST["email"]))
    {
        
    $email $_POST["email"];
        
    mail($email"Test""Test Mail""From: sender <[email protected]>");
        
    header("Location: three.php");
    }

    ?>

    <form name="f" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
    <table border="0">
        <tr>
            <td>
                Write Email ID Here <input type="text" name="email" size="20">
            </td>
            <td>
                <input type="submit" name="b" value="Submit">
            </td>
        </tr>
    </table>
    </form>
    So to start with your code is producing invalid HTML markup. Second, you are outputing two Location heades, the second of which is after you have output HTML; so form two will never actually redirect to three.php. Second; you cannot simply use isset() to check your variables and rely upon Javascript to validate them you need to check for the name and check that it has data in in the PHP code too. You also need to validate the email address.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

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