A
A
Anton2015-10-31 14:02:13
PHP
Anton, 2015-10-31 14:02:13

How to make a feedback form in one php file?

Hello. There is a simple e-mail form sent through the handler email.php

<form name="form" id="form" method="post" action='email.php'>
 <input type="text" name="name" id="name">
 <input type="text" name="mail" id="mail">
   <button type="button" id="button">Отправить</button>
</form>

The form is validated via ajax.
$.post("email.php", $("#form").serialize(), function(result){
                    if(result == 'success'){
$('#form').fadeOut(500);
  }else{
$('#form').html("<span class='red'>Fail!</span>");
}
  });

Well, a letter is already sent in the email.php file.
$subject = $_REQUEST['subject'] . 'Новое письмо';
$to = '[email protected]';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: [email protected]'. "\r\n"
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

$message .= '<b>Имя:</b> ' . $_REQUEST['name'] . "<br><br>";
$message .= '<b>Почта:</b> ' . $_REQUEST['mail'];

if (@mail($to, $subject, $message, $headers))
{
  echo 'success';
}
else
{
  echo 'fail';
}

I want to display all this in one php file, but I don’t understand how to check for ajax then, because the email.php file will no longer exist.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dokuro, 2015-10-31
Chan @iDokuro

If primitive:
And in index.php before displaying the form:

<?php
if (isset($_GET['action']))
{
   /* Обработка и ответ */
}

A
Anton, 2015-10-31
@DigitalEmotions

Dokuro-chan So? For some reason it doesn't work.

<?php
if (isset($_POST['action']))
{
   subject = $_REQUEST['subject'] . 'Новое письмо';
$to = '[email protected]';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: [email protected]'. "\r\n"
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

$message .= '<b>Имя:</b> ' . $_REQUEST['name'] . "<br><br>";
$message .= '<b>Почта:</b> ' . $_REQUEST['mail'];

if (@mail($to, $subject, $message, $headers))
{
  echo 'success';
}
else
{
  echo 'fail';
}
}
?>

<script type="text/javascript">
$.post("index.php?action", $("#form").serialize(), function(result){
                    if(result == 'success'){
$('#form').fadeOut(500);
  }else{
$('#form').html("<span class='red'>Fail!</span>");
}
  });
</script>
<form name="form" id="form" method="post" action='email.php'>
 <input type="text" name="name" id="name">
 <input type="text" name="mail" id="mail">
   <button type="button" id="button">Отправить</button>
</form>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question