0

Below is the JS:

function sendEmail(event) {
  event.preventDefault();
  var $name = $('#name');
  var $email = $('#email');
  var $phone = $('#phone');
  var $message = $('#message');

  if (isNotEmpty($name) && isNotEmpty($email) && isNotEmpty($phone) && isNotEmpty($message)) {
    $.ajax({
      url: 'sendEmail.php',
      method: 'POST',
      dataType: 'json',
      data: {
        name: $name.val(),
        email: $email.val(),
        phone: $phone.val(),
        message: $message.val()
      }, success: function (response){
        console.log(response);
      }
    })
  }
}

The browser says sendEmail.php is not found. jquery.min.js:2 POST http://localhost:3000/sendEmail.php 404 (Not Found)

This is the folder structure of my project:

Folder structure of my project

The js code is from the file assets/mail/contact_form.js and the sendEmail.php is at the src folder.

Harish
  • 490
  • 2
  • 11
  • 19
  • Which php page are you running the JS in? Remember that the path needs to be relative to the exectuing page, not the .js file – Rory McCrossan Jan 08 '21 at 15:08
  • The js is being run from ```index.html``` which is in the dist folder (not shown here). – Harish Jan 08 '21 at 15:14
  • Then you need to update the path relative from index.html - or better yet make every path relative to the root of the site by prefixing it with ```/```, eg. `/sendEmail.php`, assuming the `src` folder is the root – Rory McCrossan Jan 08 '21 at 15:18
  • I've tried changing the URL to ```'/sendEmail.php'``` but the error still occurs. – Harish Jan 08 '21 at 15:20
  • Unfortunately that's all the help that can be provided with the information shown. All we can tell you is that the path is wrong, and what you need to do to fix it – Rory McCrossan Jan 08 '21 at 15:21
  • Did you try to open this URL in your browser ?http://localhost:3000/sendEmail.php – Alexander Dobernig Jan 08 '21 at 15:54
  • Pls open in your browser http://localhost:3000/assets/sendEmail.php – Alexander Dobernig Jan 08 '21 at 15:59
  • I have copied the ```sendEmail.php``` file into the dist folder and now visiting ```localhost:3000/sendEmail.php ``` downloads the ```sendEmail.php``` file – Harish Jan 08 '21 at 16:12

1 Answers1

0

I think the main problem is outside of the information you shared, so I'll try my best to guess at what's going on, and hopefully that will point you in the right direction.

How are you compiling your code?

You have a src/ directory. Somehow, your computer compiles the files in the src/ directory and outputs the results into the dist/ directory. For example, src/pug/index.pug becomes dist/index.html.

When you run your webserver, if dist/index.html is available at http://localhost:3000, then the dist/ folder is your public root. Look in the dist/ folder. Is sendEmail.php in there? My guess is, it's not.

This brings us back to the question of "how are you compiling your code?". However you're doing it, perhaps a command line tool, you'll want to add a step in which you copy over the missing files. You could do this by configuring your compiler to include the missing php files, or you could do it manually via the command line:

cp src/sendEmail.php dist

Hope that helps. If you provide details on what compiler/framework/server/etc you're running, the community will be able to give you a more specific answer.

bmdev
  • 386
  • 1
  • 7
  • are you using a PHP server? You'll need to run some code to make your computer deliver the PHP to the browser. Perhaps instead of the current server you're running, after compiling, you can run `cd dist && php -S localhost:3000` -- see https://stackoverflow.com/questions/1678010/php-server-on-local-machine for more – bmdev Jan 08 '21 at 16:35
  • 1
    Also, it may not matter but it's best-practice to keep URLs lowercase, so I also suggest renaming the php file to something like `send-email.php` – bmdev Jan 08 '21 at 16:37
  • It's a node.js server that I am running and like you mentioned, I will take a look at how the code is being compiled. The file might be ignored somewhere. – Harish Jan 08 '21 at 17:04
  • Hmm the thing is, the node.js server won't interpret the php -- you'll need to run the php server (which will also serve your html and js files), or, instead of using php scripts, you can write your server-side scripts in js and use a node.js server like https://expressjs.com/ to serve them – bmdev Jan 08 '21 at 17:30