index.html
<?php include "php/mail.php";?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Отправка форм AJAX</title> </head> <body> <form method="post" action=""> <table> <tr> <td>Текст: </td><td><input type="text" name="text" required/> </td> </tr> <tr> <td>Выражение: </td><td><input type="text" name="res" required/></td> </tr> <tr> <td><?php echo $a. ' + ' .$b. ' = ';?></td> <td><input type="text" name="res" /></td> </tr> <tr> <td colspan="2"><input type="submit" name="submit" value="отправить" /></td> </tr> </table> </form> mail.php
<?php session_start(); if($_POST['submit']){ if($_POST['res'] == $_SESSION['res']){ $to = 'mail@mail.ru'; $subject = 'Письмо с сайта'; $body = $_POST['text']; $headers = 'Content-type:text/plain; Charset=windows-1251'; if(mail($to, $subject, $body, $headers)){ $_SESSION['mes'] = '<p>Письмо отправлено!</p>'; header("Location: http://localhost:3000"); exit(); }else{ $_SESSION['mes'] = '<p>Ошибка!</p>'; header("Location: http://localhost:3000"); exit(); } }else{ echo "<script type='text/javascript'> alert('Вы ввели неверный логин или пароль.'); </script>"; header("Location: http://localhost:3000"); exit(); } } $a = rand(1,10); $b = rand(1,10); $_SESSION['res'] = $a + $b; echo $_SESSION['mes']; ?> The script does not run.
alert('Вы ввели ,,,.');}What is the end of the brace? - teranheader('location: ..')should be the first output on the page, if you want to redirect somewhere. You have a notice there should show that the headers have already been sent. - teran