0
Good afternoon, I am trying to create a page on my site is wordpress in which the user chooses a file and this file updates a table in the database.
I got code down, but when I click to load data, nothing happens. Function is not being called.
<div style="width: 50%;margin: 20px auto; ">
<form action="" method="POST">
Selecione o arquivo desejado:
<select>
<option value="" selected="selected">-----</option>
<?php
foreach(glob('files/*') as
$filename){
$filename = basename($filename);
echo "<option value='" . $filename . "'>".$filename."</option>";
}
?>
</select>
<input type='submit' value='Carregar dados'>
</form>
</div>
<?php
if(isset($_POST)){
function insere() {
echo $filename;
echo "apagando dados da tabela...<br>";
$delete = $wpdb->query("TRUNCATE TABLE `i_renda`");
$ler=file("files/".$filename);
foreach($ler as $linha) {
$dados=explode(';',$linha);
list($nome, $cpf, $nascimento, $valor, $ano_vigencia)=$dados;
$wpdb->query("INSERT INTO i_renda (nome, cpf, nascimento, valor, ano_vigencia) VALUES ('".utf8_encode($nome)."', '$cpf', '$nascimento', '$valor', '$ano_vigencia')" );
}
echo "Dados incluidos com sucesso!"; exit;
}
}
?>
Voce wants to insert what was selected on this page on another page ? or only that it communicates with another page at the time of Submit ?
– Victor
You’re not calling her at any time on this page you sent, there you just created, call after creation
– arllondias
Victor, my idea is that everything happens on the same page. The user selects the file and clicks to load in the database. In this case, the form function is only to select the file and pass this parameter to the PHP function that will insert the data in the database.
– Nathalia Negrão
arllondias, it’s true. I added the function call inside if. But another error occurred. It is running the function when loading the page, before the user chooses the file and click the button.
– Nathalia Negrão
This one of yours
if(isset($_POST)){
works? here with me does not work, ie, enters the if without submitting the form.– user60252