html - Cannot modify header information - headers already sent by: PHP -


<!doctype html> <html lang="en">     <head>         <meta charset="utf-8"/>         <title></title>     </head>     <body> <?php     if ($_post['password'] != $_post['confirmpassword'])     {                 $password = md5($_post['password']);         $confirmpass = md5($_post['confirmpassword']);                 echo "<script type='text/javascript'>alert('error. passwords not match.');</script>";             header("refresh:0; registration.html");      }     else      {         $password = md5($_post['password']);         $confirmpass = md5($_post['confirmpassword']);         echo 'name: '.$_post['firstname'].' '.$_post['lastname']. '<br>';          if($_post['agerange'] == 1)         {             echo "under 18<br>";         }         elseif($_post['agerange'] == 2)         {             echo "age 18-24<br>";         }         elseif($_post['agerange'] == 3)         {             echo "age 25-34<br>";         }         elseif($_post['agerange'] == 4)         {             echo "age 35-44<br>";         }         elseif($_post['agerange'] == 5)         {             echo "age 45-54<br>";         }         elseif($_post['agerange'] == 6)         {             echo "age 55-64<br>";         }         else         {             echo "age 65 or older<br>";         }          if ($_post['sex'] == 'male')         {             echo "gender: male<br>";         }         else         {             echo "gender: female<br>";         }          echo 'phone number: '.$_post['daytimephone']. '<br>';         echo 'email: '.$_post['email']. '<br>';         echo 'username: '.$_post['username']. '<br>';          if(isset($_post['specialoffers']))         {             echo "you recieve special offers via email.";         }         else         {             echo "you not recieve special offers via email.";         }     } ?>       </body> </html> 

so php script calls registration.html form. when password field not match confirm password field, alert posted saying not match page should refreshed. reason can't figure out why it's kicking out error: cannot modify header information - headers sent (output started @ documents\my web sites\week 2\exercise 4\register.php:8) in documents\my web sites\week 2\exercise 4\register.php on line 14 have suggestion on how fix this? want refresh registration form if password fields not match.

you can't output before modify headers. means this:

<!doctype html> <html lang="en">     <head>         <meta charset="utf-8"/>         <title></title>     </head>     <body> 

and this:

 echo "<script type='text/javascript'>alert('error. passwords not match.');</script>";   

are no good.

aside security aspects others mentioned, best way make sure both passwords match before form submitted using jquery validate or similar.

if had this, better store message in session variable , output on page:

if ($_post['password'] != $_post['confirmpassword'])     {                 $password = md5($_post['password']);         $confirmpass = md5($_post['confirmpassword']);                 $_session["msg"] =  "error. passwords not match.";         header("location: registration.php");     } 

on registration.php:

if (isset($_session["msg"])){  echo "<script type='text/javascript'>alert('" . $_session["msg"] . "');</script>"; unset($_session["msg"]); } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -