-2

This is my index.php code

<html>
    <head>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
        <script>
            if ( window.history.replaceState ) {
                window.history.replaceState( null, null, window.location.href );
            }
        </script>
    </head>
    <body>
        <?php session_start(); ?>
        <?php if(isset($_SESSION['success']) && $_SESSION['success'] == 1) { ?>
            <?php unset($_SESSION['success']); ?>
            <div class="jumbotron text-center">
                <h1>SUCCESS</h1>
                <p>CONTACT CSV UPLOADED SUCCESSFULLY</p> 
                <p><a onclick="resetFormDetails();" href="">Go Back to Form</a></p>
            </div>
        <?php } else { ?>
            <div class="jumbotron text-center">
                <h1>CONTACT CSV UPLOAD</h1>
                <p>On Submit it may take some time to update. Be Patience!</p> 
            </div>

            <div class="container">
                <div class="row">
                    <div class="col-12">
                        <form id="contact-form" method="post" action="<?php echo 'upload.php'; ?>" enctype="multipart/form-data">
                            <div class="form-group">
                                <label for="contact-file">Upload Contact CSV</label>
                                <input name="contact_file" type="file" class="form-control-file" id="contact-file">
                            </div>
                            <button type="submit" class="btn btn-default">Submit</button>
                        </form>
                    </div>
                </div>
            </div>
        <?php } ?>
    </body>
    <script>
        function resetFormDetails() {
            document.getElementById("contact-form").reset();
            location.reload();
        }
    </script>
</html>

and here is my upload.php code

<?php

// SERVER SIDE
if(isset($_FILES["contact_file"]) && !empty($_FILES["contact_file"])) {

    // UPLOAD FILE
    $storagename = "contacts.csv";

    if(!is_dir('uploads')) {
        @mkdir('uploads');
    } else {
        if(file_exists("uploads/" . $storagename)) {
            @unlink("uploads/" . $storagename);
        }
    }

    @move_uploaded_file($_FILES["contact_file"]["tmp_name"], "uploads/" . $storagename);
    unset($_FILES["contact_file"]["tmp_name"]); // UNSET TEMP
    unset($_FILES["contact_file"]); // UNSET UPLOADED FILE VARIABLE

    $file = fopen("contacts.csv", "r");
    $csv_data = array();
    while(!feof($file))
    {
        $csv_data[] = fgetcsv($file);
    }

    fclose($file);

    if(file_exists("uploads/" . $storagename)) {
        @unlink("uploads/" . $storagename);
    }

    $first_row = $csv_data[0];
    $contacts = array();

    if(trim($first_row[0]) == 'email' && trim($first_row[1]) == 'link') {
        array_shift($csv_data);

        foreach ($csv_data as $row) {
            if(!empty($row)) {
                $contacts[] = array(
                    'email' => $row[0],
                    'link' => $row[1]
                );
            }
        }
    } else {
        echo "Fatal Error: Format Issue";
        exit;


      }

//hiding for the purpose 
        $servername = "X.X.X.X";
    $username = "XYZ";
    $password = "XYZ";
    $dbname = "XYZ"; 

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    foreach ($contacts as $contact) {
        $sql = "INSERT INTO ch_contact_tbl(`contact_email`, `contact_url`) VALUES ('".$contact['email']."', '".$contact['link']."')";

        if($conn->query($sql) === TRUE) {
            continue;
        } else {
            echo "Error Occured";
            break;
        }
    }

    session_start();
    $_SESSION['success'] = 1;

    $conn->close();
    header("Location: index.php");
}

I am trying to upload a csv file with some contacts details. but once the details is uploaded i don't want it would repeat without selecting file. However even i have not selected any file to upload only by just click on submit button it uploading my previous file automatically to the server.

I have tried all the ethics possible but unable to figure out the root cause of this problem.

Kindly help me to figure it out.

I have also given reset button in my form. but even i have clicked on that button. still my form is not reset and taking the value of previously file uploaded on submit. enter image description here

Ethane
  • 3
  • 3
  • Who so ever given -1 has not read my question entirely. – Ethane Dec 11 '18 at 05:45
  • Possible duplicate of [How to reset a form using jQuery with .reset() method](https://stackoverflow.com/questions/16452699/how-to-reset-a-form-using-jquery-with-reset-method) – Zakaria Chabihi Dec 11 '18 at 05:56
  • @ZakariaChabihi - can you just check my code. I have tried all these methods .. Also i am not using any JQuery in my code. – Ethane Dec 11 '18 at 05:58

3 Answers3

1

You need to reset the form after submitting to the server.

  1. If you are uploading using javascript document.getElementById("contact-form").reset();

    1. If you are doing through PHP please reload the current div so that element and value can be reset.
Singham
  • 303
  • 2
  • 8
0

You could use the

form.reset()

function. An example would be

function resetForm() {
    document.getElementById("form").reset();
}
<!DOCTYPE html>
<html>
<body>
<button onClick="resetForm()">Reset Form</button>
<form id="form">
<input type="text" name="textInHere">
</form>
</body>
</html>

This uses the reset() function in javascript. I hope it helps you!

Sebastian Power
  • 85
  • 3
  • 10
0

This behavior is caused by location.reload which triggers a form resubmit.

The solution would be, instead of:

        location.reload();

Use:

        location.href = location.href;