Error when redirecting after sending form

Asked

Viewed 200 times

0

My code:

     <?php
        header('Content-Type: text/html; charset=utf-8');
        
        $email=$_POST[email];
        $nome=$_POST[nome];
        $cor=$_POST[cor];
        $nascimento=$_POST[nascimento];
        $cep=$_POST[cep];
        $endereco=$_POST[endereco];
        $numero=$_POST[numero];
        $complemento=$_POST[complemento];
        $referencia=$_POST[referencia];
        $bairro=$_POST[bairro];
        $cidade=$_POST[cidade];
        $uf=$_POST[uf];
         
        mail("[email protected]","Assunto do email","
        Nome: $nome
        Data de nascimento: $nascimento
        CEP: $cep
        Endereço: $endereco
        Número endereço: $numero
        Complemento: $complemento
        Referência: $referencia
        Bairro: $bairro
        Cidade: $cidade
        Estado: $uf 
        ");

    /* Este header faz o redirecionamento, com alguns GET's que
serão usados na página na qual foi redirecionada. Exemplo, no
formulário foi colocado "João" no campo name="nome",
então nome é = a João. */
    
        header ("location: obrigado/?cor=$cor&nome=$nome&email=$email&cep=$cep&endereco=$endereco&numero=$numero&complemento=$complemento&bairro=$bairro&referencia=$referencia&cidade=$cidade&uf=$uf"); 
         
        ?>

As you can see, in this example he takes the data that is filled in, he sends it to this script PHP through a <form name="formulario" method="post" action="concluido.php">.

This script besides sending an email to me with this data registered, it also takes this data and inserts us GET's contained in the url to which it will be redirected.

It used to work in the old days, but I can’t make it work anymore. I always thought he was too simple, maybe that’s why he’s having problems.

It’s possible to make this work?

Note: You are redirecting perfectly when using on localhost, but used in a hospedagem web occurs THIS MISTAKE:

inserir a descrição da imagem aqui It doesn’t send the email to me, and it doesn’t redirect.

  • What mistake happens?

  • I will post here in the comments the error :) Just a little while...

  • @rray https://s11.postimg.org/9h98l00s3/Sem_T_tulo20161026093345.png

  • I removed the title tag as it does not need;

5 answers

5

The page php that you set as an example this getting via POST to capture the bad "attributes" by the example you posted from the form seems to be sending via GET.

Change the method from your form to POST simple.

  • Lauro, in fact it was POST, i had it wrong. I updated the question :)

2


Alexandre,

try this code formatted in (UTF-8 without GOOD) and redone.

<?php
    $email              = addslashes($_POST['email']);
    $nome               = addslashes($_POST['nome']);
    $cor                = addslashes($_POST['cor']);
    $nascimento     = addslashes($_POST['nascimento']);
    $cep                = addslashes($_POST['cep']);
    $endereco           = addslashes($_POST['endereco']);
    $numero         = addslashes($_POST['numero']);
    $complemento        = addslashes($_POST['complemento']);
    $referencia     = addslashes($_POST['referencia']);
    $bairro         = addslashes($_POST['bairro']);
    $cidade         = addslashes($_POST['cidade']);
    $uf             = addslashes($_POST['uf']);


    $destinatario    = "[email protected]";
    $assunto         = "Assunto Exemplo";
    $msg             = "Nome: $nome
    Data de nascimento: $nascimento
    CEP: $cep
    Endereço: $endereco
    Número endereço: $numero
    Complemento: $complemento
    Referência: $referencia
    Bairro: $bairro
    Cidade: $cidade
    Estado: $uf ";

    $headers         = "MIME-Version: 1.1\r\n";
    $headers        .= "Content-type: text/plain; charset=UTF-8\r\n";
    $headers        .= "From: [email protected]\r\n"; // remetente
    $headers        .= "Return-Path: [email protected]\r\n"; // return-path
    $envio           = mail($destinatario, $assunto, $msg, $headers);

    if($envio){
       header ("location: obrigado/?cor=$cor&nome=$nome&email=$email&cep=$cep&endereco=$endereco&numero=$numero&complemento=$complemento&bairro=$bairro&referencia=$referencia&cidade=$cidade&uf=$uf"); 
    }else{
     echo "A mensagem não pode ser enviada";
    } 
?>
  • You’re making the mistake Parse error: syntax error, unexpected ';' in /home/xnshoptm/public_html/br/p/124678139/concluido.php

  • 1

    Okay I’m editing

  • The error is in the lines that contain these = addslashes, because the error appears in one, and I delete the line, when I try again, the error jumps to the next line with = addslashes

  • 1

    @Alexandrelopes test now

  • I will test! Luke :D

  • 1

    I hope for certain ;)

  • It worked!!! It is redirecting and there was no error. Only problem is that the email is not coming to me, I am using my Gmail email. :/

  • It’s here!!! Only it’s going to the spam box! : ) Kkkkkk...

  • 1

    @Alexandrelopes This can be solved by using SMTP or some other method of sending emails. I’ll do a search and send you! I’m glad it worked! HEHE :P

  • I think it’s because it’s like my own e-mail sent to himself, kkkkkk

  • But there is not much problem, the important thing is to arrive, even in the spam box, kkk :D

Show 6 more comments

2

On top there’s this:

header('Content-Type: text/html; charset=utf-8');

This line writes information in the header.

At the end there is another header that serves to redirect to another page.

header ("location: obrigado/?cor=$cor&nome=$....

This second header also needs to write in the header, but since the header in the first header() has already been fired, this conflict happens, emitting the famous error

Cannot modify header information..

To resolve this specific error, remove or disable the first header()

//header('Content-Type: text/html; charset=utf-8');

This first header makes no sense because if you are redirecting without having to print anything on the current page, you do not need to invoke it.

1

The error seems to be time to get the information from $_POST, where simple quotes are missing in key names in older versions this was allowed and worked in newer ones a Warning is generated ex: Notice: Use of undefined constant email - assumed 'email'.

Since your code has a call from header() and an unwanted text output (Warning), error happens Cannot Modify header information - headers already sent by.

To correct change:

$email=$_POST[email];

To:

 $email=$_POST['email'];

Suggested improvement:

Instead of generating this querystring in hand use the function http_build_query() to simplify the work, passed an associative array it generates querystring.

Change:

header ("location: obrigado/?cor=$cor&nome=$nome&email=$email&cep=$cep&endereco=$endereco&numero=$numero&complemento=$complemento&bairro=$bairro&referencia=$referencia&cidade=$cidade&uf=$uf"); 

For:

$queryString = '?'. http_build_query($_POST);
header('location: obrigado/'. $queryString);

Don’t trust the function mail() make a check if the email was actually sent. As for example:

if(!mail(......)){
   die(print_r(error_get_last()));
}
  • I’m going to test it now :)

  • Same error: https://s11.postimg.org/9h98l00s3/Sem_T_tulo20161026093345.png

  • @Alexandrelopes the Find from your files is utf-8 without GOOD?

  • Is there a problem or something wrong in the answer? can I correct

  • charset=utf-8, @Would you be able to edit your answer? putting the code in full, I’m new with PHP, so I didn’t quite get how to make it work, if you do this, I’ll understand you perfectly :)

  • @Alexandrelopes which text editor you use? Usually the file’s NCODE is at the bottom. you need to add simple quotes in all keys, from $_POST['email'] up to the $_POST['uf']. To assemble the quick test, comment on the header() so you can see the errors before it directs the page.

  • I use Sublime Text. I added all the simple quotes as you mentioned.

  • @Alexandrelopes now that I saw, remove that first header('Content-Type: text/html; charset=utf-8');

  • See error remains: https://s13.postimg.org/3ylky6u4n/Sem_T_tulo20161026100711.png

  • @Alexandrelopes line 43 and earlier are which?

  • It seems to have an invisible character in the first line of your file, try to change the Encode from it to utf-8 without GOOD.

  • @Alexandrelopes line 31 is from echo?

  • The header line with Location. The error seems to be there.

  • See: https://s12.postimg.org/rqp5q7s3x/Sem_T_tulo20161026102248.png

  • @Alexandrelopes on how to change the file find that question

  • Now I’ve saved here. I’ll test :)

  • The error remains the same: https://s12.postimg.org/rqp5q7s3x/Sem_T_tulo20161026102248.png that line 31, is the line of header ("location:

  • @Alexandrelopes error says some text was sent in the first of concluido.php I guess it’s the question file or he’s a include?

Show 13 more comments

1

Good valos there,

First look at the message :

"Cannot Modify header information - headers already sent by"*

"Unable to modify header information - directories already sent by (completed.php line 1)

What you can do is use one echo "<script type='javascript'></script>";

<?php
    header('Content-Type: text/html; charset=utf-8');

    $email       = $_POST['email'];
    $nome        = $_POST['nome'];
    $cor         = $_POST['cor'];
    $nascimento  = $_POST['nascimento'];
    $cep         = $_POST['cep'];
    $endereco    = $_POST['endereco'];
    $numero      = $_POST['numero'];
    $complemento = $_POST['complemento'];
    $referencia  = $_POST['referencia'];
    $bairro      = $_POST['bairro'];
    $cidade      = $_POST['cidade'];
    $uf          = $_POST['uf'];

    mail("[email protected]","Assunto do email","
    Nome: $nome
    Data de nascimento: $nascimento
    CEP: $cep
    Endereço: $endereco
    Número endereço: $numero
    Complemento: $complemento
    Referência: $referencia
    Bairro: $bairro
    Cidade: $cidade
    Estado: $uf 
    ");

/* Este header faz o redirecionamento, com alguns GET's que serão usados na página na qual foi redirecionada. Exemplo, no formulário foi colocado "João" no campo name="nome", então nome é = a João. */

    echo "<script type='text/javascript'> window.location='obrigado/?cor=$cor&nome=$nome&email=$email&cep=$cep&endereco=$endereco&numero=$numero&complemento=$complemento&bairro=$bairro&referencia=$referencia&cidade=$cidade&uf=$uf';</script>"; 

    ?>

Ps: wouldn’t be the totally ideal way, but serves!

  • It keeps giving the same error, the difference is that after displaying the error, it is redirected. Just this :/

  • @Alexandrelopes Copy this code now, this problem should be given because before opening the tag <?php was with white spaces! test now!

  • That’s not the problem :(

  • @Alexandrelopes is the complete code of your page ?

Browser other questions tagged

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