这是我的index.php代码
<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>
这是我的upload.php代码
<?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");
}
我正在尝试上传包含某些联系人详细信息的csv文件。但是一旦详细信息上传,我不希望重复执行而不选择文件。 但是,即使我还没有选择要上传的任何文件,只需单击“提交”按钮即可将我之前的文件自动上传到服务器。
我已经尝试了所有可能的道德规范,但无法找出造成此问题的根本原因。
请帮我弄清楚。
我还在表单中提供了重置按钮。但即使我单击了该按钮。仍然没有重置我的表单,并使用提交时上传的先前文件的值。 enter image description here
答案 0 :(得分:1)
提交到服务器后,您需要重置表单。
如果您使用的是JavaScript tmp = cumsum(Result)
tmp(end) == 27.50
sum(Result) == 27.50
答案 1 :(得分:0)
您可以使用
form.reset()
功能。一个例子是
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>
这使用javascript中的reset()函数。希望对您有帮助!
答案 2 :(得分:0)
此行为是由location.reload
引起的,它触发了表单重新提交。
解决方案将是:
location.reload();
使用:
location.href = location.href;