2

I'm trying to send information to a PHP page, but it returns me a 404 error status.

<form name="registerForm" novalidate>
    <div class="field"><label for="name">name : </label><input type="text" ng-model="register.registerInfo.name" name="name" id="name" autocomplete="off" required ></div>
    <div class="field"><label for="number">number : </label><input type="text" ng-model="register.registerInfo.number" name="number" id="number" autocomplete="off" required ></div>
    <br>
    <button ng-click='registerForm.$valid && register.register()'>send</button>
</form>

A part of app.js, I'm using ngrouting

    .controller('RegisterCtrl', ['$http', function($http){

    this.registerInfo = {
        name: '',
        number: ''
    };

    this.register = function() {
        var data = {
            name: this.registerInfo.name,
            number: this.registerInfo.number
        };

        $http.post('register.php', data).then(function(data) {
            console.log(data);
        }, function(error) {
            console.error(error);
        });
    };
}]);

register.php

<?php 

$data = json_decode(file_get_contents('php://input'));
echo 'slt';
echo $data->data->name;

http://puu.sh/m4FVO/3e607fd137.png

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

I want to send register information (the name and a number), analyze data in PHP and if they are okay save them with MySQL. I don't know why it can't find register.php since the path seems correct, maybe an issue with the server?

halfer
  • 19,824
  • 17
  • 99
  • 186
garwan50
  • 43
  • 1
  • 1
  • 5
  • Use '/register.php' for `$http.post();` – AddWeb Solution Pvt Ltd Dec 23 '15 at 06:13
  • hey, i tried, doesn't work :c http://puu.sh/m5Gt5/2366eae01c.png – garwan50 Dec 23 '15 at 06:17
  • Ok, just try with full path: http://localhost/APPNAME/scripts/register.php Here APPNAME is your project folder name. I think there is some issue with URL. If it not worked then correct it with application url. – AddWeb Solution Pvt Ltd Dec 23 '15 at 06:25
  • i tried a lot of url none works: http://localhost:9000/orion/scripts/register.php, localhost:9000/orion/scripts/register.php, localhost/orion/scripts/register.php, localhost:9000/scripts/register.php, http://localhost/orion/scripts/register.php, localhost:9000/orion/scripts/register.php, etc – garwan50 Dec 23 '15 at 06:41
  • http://localhost/orion/scripts/register.php should work as per your folder map. – AddWeb Solution Pvt Ltd Dec 23 '15 at 06:47
  • You are using grunt? If not, it looks like you're using $http.post to deliver a local file instead of making an http request to the server, so Apache isn't being called (and therefore PHP is not running). $http.post should be pointed at whatever URL would execute that script, like http://localhost/orion/scripts/register.php – AddWeb Solution Pvt Ltd Dec 23 '15 at 06:51
  • i'm using grunt but i don't have any server for processing php files, i need to use apache, i'm gonna search for more info online, thanks for helping – garwan50 Dec 23 '15 at 07:08

2 Answers2

0

You are addressing register.php in $http as if the register.php script is in the same directory as your base html file. However, your file list seems to indicate that your index.html file is in the root directory, while the the register.php file is in the "scripts" subdirectory.

Assuming you don't have any special server-side routing logic, you should update the address in the $http call from:

$http.post('register.php', data)

to

$http.post('scripts/register.php', data)



EDIT: Added info based on comments from OP

Above answer is still correct -- original code was improperly referencing the register.php file residing in "scripts" directory. Once fixed, another issue exists.

OP is using "grunt serve"; however, the grunt server doesn't process php files out of the box. It's a simple server meant to serve static files (html, js, css, etc.) without server side processing/execution (php). Check out this question/answers for discussion on php under grunt:

However, consider installing/running a more full featured web server (Apache, IIS, etc.), especially if you're wanting to deploy this at some point.

Community
  • 1
  • 1
Mike
  • 1,211
  • 1
  • 7
  • 6
  • Hey, i tried this but still doesn't work, same 404 status, i really thought it was gonna work, any other ideas ? Thanks! – garwan50 Dec 23 '15 at 06:01
  • so, when you manually type "http://whatever.com/index.html" into your browser address bar, you get the angular app page, right? What happens when you manually type "http://whatever.com/scripts/register.php" into the bar? Also, how are you referencing the app.js file (you should have a script tag in your index.html file that is referencing the app.js file -- what is the src attribute equal to)? – Mike Dec 23 '15 at 06:07
  • hey, when i type "localhost:9000/scripts/register.php" it downloads the file register.php. When i type http://localhost:9000/index.html it redirects me to http://localhost:9000/index.html#/main with the correct template., i have this in my index.html ` `, my full code of app.js : http://pastebin.com/jeUXunwb , thanks for helping! – garwan50 Dec 23 '15 at 06:14
  • It sounds like your server is not setup to process the PHP file as a server side script. Instead, it's simply treating it as a text/data file and is serving it up when referenced (and I'm further guessing that even when you did reference it correctly with "scripts/register.php", it's giving an error b/c you're attempting to POST to a text file). You need to check your server configuration to either process all *.php files, or move them into a subdirectory that it's expecting to house server side scripts. – Mike Dec 23 '15 at 06:22
  • And, just to clarify, when you hit "localhost:9000/scripts/register.php", it shouldn't download the actual register.php source file. It should attempt to run the php script on the server and return the result. The fact that you actually downloaded the register.php source file itself when you hit that address seems to indicate that your server is not executing *.php scripts in that directory. What server/environment/host are you on? – Mike Dec 23 '15 at 06:27
  • i'm using 'grunt serve' to launch the server. It's all pretty new to me all the server things. (my OS is windows 10 64 bits if it helps), maybe there is a command like npm install php, i don't know, i'm sorry it is new to me – garwan50 Dec 23 '15 at 06:32
  • The grunt server doesn't process php files out of the box. It's a simple server meant to serve static files (html, js, css, etc.) without server side processing/execution (php). Check out this question/answers for discussion on php under grunt: http://stackoverflow.com/questions/23343292/grunt-serve-php . However, consider installing/running a more full featured web server (Apache, IIS, etc.), especially if you're wanting to deploy this at some point. – Mike Dec 23 '15 at 06:44
0

So i've installed WAMP, edited C:\wamp\bin\apache\apache2.4.9\conf\httpd.conf and changed c:\wamp\www to my workspace folder, restarted wamp, i use localhost and not localhost:9000 now, and i have to use absolute path: scripts/register.php won't work, i have to use http://localhost/orion/app/scripts/register.php

thanks y'all for helping.

garwan50
  • 43
  • 1
  • 1
  • 5