ronoc4
ronoc4

Reputation: 637

angularjs can't connectwith php using http

I'm adding a contact form found on a site that could help me on the php side. My Only issue seems to be I can't seem to connect my JS to my PHP in the console I get. Any Ideas on how I can get both to talk to each other and work : ) Thank you :)

angular.js:8661 POST http://localhost:3000/processForm.php 404 (Not Found)

my js http look liks

$scope.sendMessage = function( input ) {
        $http({
            method: 'POST',
            url: 'processForm.php',
            data: input,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }
        })
        .success( function(data) {
          if ( data.success ) {
            $scope.success = true;
          } else {
            $scope.error = true;
          }
        });
      }

my php looks like

<?php
 $response = array( 'success' => false );
 $formData = file_get_contents( 'php://input' );
 $data = json_decode( $formData );
 if ( $data->submit && empty( $data->honeypot ) ) {
    $name = $data->name;
    $email = $data->email;
    $message = $data->message;

    if ( $name != '' && $email != '' && $message != '' ) {
        $mailTo = '[email protected]';
        $subject = 'New contact form submission';
        $body  = 'From: ' . $name . "\n";
        $body .= 'Email: ' . $email . "\n";
        $body .= "Message:\n" . $message . "\n\n";

        $success = mail( $mailTo, $subject, $body );

        if ( $success ) {
            $response[ 'success' ] = true;
        }
    }
 }
 echo json_encode( $response );

?>

file structure is

  │   404.html
  │   favicon.ico
  │   index.html
  │   robots.txt
  │   processForm.php
  │
  ├───images
  │       yeoman.png
  │
  ├───scripts
  │   │   contact.js
  │   │   
  │   │   
  │   │
  │   └───controllers
  ├───styles
  │       main.css
  │       style.css
  │
  └───views
         contact.html
         main.html

Upvotes: 0

Views: 54

Answers (1)

Mikołaj Woźniak
Mikołaj Woźniak

Reputation: 113

From your directory stucture I guess that you are using Yeoman angular generator and built in server. Please make sure that you are able to access php scripts at all.

You might to reference this post

How to integrate PHP with yeoman angular project

Upvotes: 1

Related Questions