Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a form using php and mysql. The main goal is to make the form (1) detect missing field. (2) update user input after successful submit and (3) most importantly to avoid re-submission on reload/refresh. I am able to manage the first and the third one but doesn't have any idea on the second one. Here's my code (able to achieve first and third)

   <!DOCTYPE html>
<html>
    <head></head>
    <body>
        <?php
        $name = "";
        $class = "";
        $school = "";

        if(isset($_POST["submit"])){
            $name = $_POST["name"];
            $class = $_POST["class"];
            $school = $_POST["school"];
            $output = false;

            if(empty($_POST["name"]) || empty($_POST["class"]) || empty($_POST["school"])){
                    echo 'field cannot be empty';
                    $output_form = true;
            }
            if(!empty($_POST["name"]) && !empty($_POST["class"]) && !empty($_POST["school"])){
                    $hostname = "localhost";
                    $admin = "root";
                    $password = "";
                    $database = "testdatabase";

                    $dbc = mysqli_connect($hostname, $admin, $password, $database) or die("database connection error");

                    $insert_query = "INSERT INTO `sorty` (`name`, `class`, `school`) VALUES ('$name', '$class', '$school')";
                    $insert_result = mysqli_query($dbc, $insert_query) or die("error");
                    if($insert_result == 1)
                        echo "data inserted";
                    else
                        echo "insert query failed";

                    mysqli_close($dbc);
                    header('Location: form2.php');
            }
        }
        else
            $output = true;

        if($output){
        ?>
            <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
                Name: <input type="text" name="name" value="<?php echo $name?>"/><br/>
                Class: <input type="text" name="class" value="<?php echo $class?>"/><br/>
                School: <input type="text" name="school" value="<?php echo $school?>"/><br/>
                <input type="submit" value="submit" name="submit"/>
            </form>
        <?php
        }
        ?>
    </body>
</html>


My second file form2.php(succesful page after form submission)

<body>
Name: /*user input here*/<br/>
Class: /*user input here*/<br/>
School: /*user input here*/<br/>


As I can't access the variable $name, $class, $school of form.php I am having problem updating the user input data. So is there anyway to access the variable across file or is it not possible to do in this way.

What I have tried:

I have try updating without using header and it works fine. But I need to do in this way.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900