0

I have an issue when sending form data over Ajax to PHP script. When sending data i get this kind of error

Fatal Error: Class Libs\Controller not found

I have written my own MVC project structure and its working fine if i'm sending data just with POST request but when sending with AJAX getting this error

use Libs\Controller;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;


class Contact extends Controller {
  //rest code to send email with PHPMailer
}

And here is the js script

$("#contact-form").on('submit', function (e) {
    $(".validmessage").css("display", "block");
    e.preventDefault();

    $.ajax({
        url: "/../../app/controllers/Contact.php",
        type: "POST",
        data: $(this).serialize(),
        success: function (data) {
            $("#form_output").html(data);
        },
        error: function (jXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
            });
        });

My file structure:

controllers
 - Contact.php
-libraries
  - Controllers.php
  - Core.php
  - Database.php

Using PSR autoloader to load my classes inside index.php file

7
  • problem in your php code. ajax is just fine. Commented Nov 29, 2018 at 9:26
  • php code is working fine without sending data over ajax Commented Nov 29, 2018 at 9:27
  • The error message is pretty clear. Can you provide us with your file structure? You reach the php script but the path of your Libs\Controller is not correct. Commented Nov 29, 2018 at 9:27
  • URL should be proper eg : url: "example.com/contact.php" Commented Nov 29, 2018 at 9:27
  • check this file Libs\Controller, does it exist ? Commented Nov 29, 2018 at 9:30

1 Answer 1

3
   require '../libraries/Controllers.php'

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;


    class Contact extends Controller {
      //rest code to send email with PHPMailer
    }

You need to move one step back to access the Controllers. At the moment you are in contact.php so you can not "see" the libraries folder. You need to move 1 step back using ../ and then access the folder and it's files.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.