This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

leonfury9560712
leonfury9560712

Hello, so i wrote AJAX feedback form, but when i press "Send" button, it shows 404 error.

I know for a fact that problem is somewhere in CMS itself, since i tried the script on the hosting i use without CMS, and it worked. But as soon as i installed October there and placed the same script there, it just stopped working.

Maybe i need to change some settings somewhere? I already changed mail settings in CMS backend to "PHP mail" (since the script working on it). Do i need to change something else? Maybe something is conflicting with default hosting files?

mjauvin
mjauvin

Show some code

leonfury9560712
leonfury9560712

HTML:

   <form id="contact-form">
    <input type="text" name="name" class="form-control" placeholder="Ваше имя">
    <input type="text" name="email" class="form-control" placeholder="Email">
      <input type="text" name="phone" class="form-control" placeholder="+7 (...) ...-..-..">
  <button class="surveyformbtn btn mt-2" type="submit" id="send"></button>
  <div class="answer"></div>
</form> 

JS :

 <script>
    $(function(){
          $("#send").click(function(){
        var msg = $('#contact-form').serialize();
        $.ajax({
          type: 'POST',
          url: 'main/themes/theme/assets/php/action.php',
          dаta: msg,
          success: function(data) {
            $('.answer').html(data).slideDown(300);
            console.log(data);
          },
          error:  function(xhr, str){
                $('.answer').html('Возникла ошибка: ' + xhr.responseCode);
            }
        });
        return false;
    })

});
  </script> 

PHP :

 <?php

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

$to = "lk@gmail.com"; 

if(!empty($_POST["name"]) && !empty($_POST["phone"]) && !empty($_POST["email"])){ 

    $name = trim(htmlspecialchars(strip_tags($_POST["name"])));
    $phone = trim(htmlspecialchars(strip_tags($_POST["phone"])));
    $email = trim(htmlspecialchars(strip_tags($_POST["email"])));

    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 
        echo "<div class='i_false'>E-mail указан не верно!</div>"; 
        exit; 
    }

    $headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'To: Название сайта <info@site.ru>' . "\r\n";
$headers .= 'From: Название сайта <info@site.ru>' . "\r\n";
$headers .= 'Bcc: info@site.ru' . "\r\n";

    $message = '
    <html>
        <head>
            <title>Новая заявка</title>
        </head>
        <body style="font-family:Arial,sans-serif; background: #E7F5FA;">
            <div style="background: #E7F5FA; color: #227E9B; border: 1px #AFD5E2 solid; padding: 10px;">
                <p><b>Имя:</b> "'.$name.'"</p>
                <p><b>Телефон:</b> "'.$phone.'"</p>
                <p><b>E-mail:</b> "'.$email.'"</p>
            </div>
        </body>
    </html>
    ';

    if(mail($to, "Новая заявка", $message, $headers)){ 
        echo "<div class='i_true'>Сообщение отправлено!<br>Мы свяжемся с Вами в ближайшее время.</div>"; 
    } else{ 
        echo "<div class='i_false'>Не получилось отправить сообщение</div>"; 
    }

} else{ 
    echo "<div class='i_false'>Необходимо заполнить все поля!</div>"; 
}

?>

Last updated

mjauvin
mjauvin

You're re-inventig the wheel... I suggest reading this on how to do it with October CMS:

https://octobercms.com/docs/ajax/introduction

leonfury9560712

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.