# Angularjs post to PHP

**URL:** <https://forum.ionicframework.com/t/angularjs-post-to-php/37677>\
**Category:** uncategorized\
**Created:** [November 23, 2015, 12:04am UTC](https://forum.ionicframework.com/t/angularjs-post-to-php/37677 "2015-11-23T00:04:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexis791](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@alexis791](https://forum.ionicframework.com/u/alexis791)\
**Post date:** [November 23, 2015, 12:04am UTC](https://forum.ionicframework.com/t/angularjs-post-to-php/37677/1 "2015-11-23T00:04:49Z")

</div>

I have this console error.

POST [http://localhost:8100/send.php](http://localhost:8100/send.php) 404 (Not Found)

 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/c/3/c3ca31acb2e051c8ed43750103914ccb27f4a0a2.PNG)

And these is my script JS

 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/1/d/1dbc1cda2ad087ae63c38b0d19bfaf400d98c9f0.PNG)

And script PHP send.php  
 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/3/7/37b92bea30e4aac70fd119c7ed2b3201dad87822.PNG)

Someone can help me?. Thanks.

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [November 23, 2015, 12:18am UTC](https://forum.ionicframework.com/t/angularjs-post-to-php/37677/2 "2015-11-23T00:18:42Z")

</div>

`send.php` is obviously not available on [http://localhost:8100/send.php](http://localhost:8100/send.php) - that’s what the Error message `404 - Not Found` is telling you.

Is this both on the same computer?  
Can you open `http://localhost:8100/send.php` in a normal browser?

---

<div class="post-metadata">

**Author:** ![alexis791](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@alexis791](https://forum.ionicframework.com/u/alexis791)\
**Post date:** [November 23, 2015, 12:35am UTC](https://forum.ionicframework.com/t/angularjs-post-to-php/37677/3 "2015-11-23T00:35:32Z")

</div>

Yes, There are in the same computer.

And when I try to put the url. The script of send.php it download automatically.I can’t open it in the browser.

The is this POST [http://localhost:8100/php/send.php](http://localhost:8100/php/send.php) 404 (Not Found).

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [November 23, 2015, 12:53am UTC](https://forum.ionicframework.com/t/angularjs-post-to-php/37677/4 "2015-11-23T00:53:44Z")

</div>

Ah wait. 8100 is the port of `ionic serve`. And now I also see that the `php` folder is part of your ionic project.

You have to put the PHP script into another folder that is served by a webserver. Ionic won’t take care of that. Mobile app (Ionic) and backend app (PHP via Webserver) are two different projects.

---

<div class="post-metadata">

**Author:** ![alexis791](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@alexis791](https://forum.ionicframework.com/u/alexis791)\
**Post date:** [December 11, 2015, 5:01am UTC](https://forum.ionicframework.com/t/angularjs-post-to-php/37677/5 "2015-12-11T05:01:23Z")

</div>

It’s true. One more last question. I collocated the php file in the web serve, but it show me this error.

“XMLHttpRequest cannot load [http://localhost/php/correo.php](http://localhost/php/correo.php). Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘[http://localhost:8100](http://localhost:8100)’ is therefore not allowed access.”

I don’t know. Maybe need add some more to my php file?

Thanks. Sujan12

---

<div class="post-metadata">

**Author:** ![cybernatic](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cybernatic/32/138808_2.png) [@cybernatic](https://forum.ionicframework.com/u/cybernatic)\
**Post date:** [December 11, 2015, 7:58am UTC](https://forum.ionicframework.com/t/angularjs-post-to-php/37677/6 "2015-12-11T07:58:57Z")

</div>

Search CORS php htaccess  
its will get help

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [December 11, 2015, 9:40am UTC](https://forum.ionicframework.com/t/angularjs-post-to-php/37677/7 "2015-12-11T09:40:22Z")

</div>

This should help:

```
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  header('Access-Control-Allow-Origin: *');
  header('Access-Control-Allow-Headers: Authorization, Content-Type');
  exit;
}
header('Access-Control-Allow-Origin: *');
```

---

<div class="post-metadata">

**Author:** ![krzkz94](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@krzkz94](https://forum.ionicframework.com/u/krzkz94)\
**Post date:** [December 11, 2015, 9:56am UTC](https://forum.ionicframework.com/t/angularjs-post-to-php/37677/8 "2015-12-11T09:56:33Z")

</div>

You need to have a PHP server up and running, for a quick solution on windows install LAMP and drop the file where it tells you.

Then you need to enter the full url of the file, should be something like 127.0.0.1/yourfile.php, lamp will guide you.

Then you will most likely face a CORS issue (maybe not since it’s localhost), but if you get error codes 0 try @Sujan12’s solution

---

<div class="post-metadata">

**Author:** ![alexis791](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@alexis791](https://forum.ionicframework.com/u/alexis791)\
**Post date:** [January 14, 2016, 5:17pm UTC](https://forum.ionicframework.com/t/angularjs-post-to-php/37677/9 "2016-01-14T17:17:50Z")

</div>

Hi guys, Really I can’t solve it. And these is my code.

This is my angular code:

> var app = angular.module(‘starter’, [‘ionic’]);

> app.controller(‘send\_email’,function($scope,$http){

> ```
> var config = {
> method:'POST',
> url:'http://www.hobbylacuesta.com/correo.php',
> data:'correo=alexis.970991@gmail.com',
> headers:{'Content-Type':'application/x-www-form-urlencoded'}
> }
> 
> $scope.sendemail = function(){
> var response = $http(config);
>     
> response.success(function(data,status){
> console.log('Done');
> });
>     
> response.error(function(data,status){
> console.log('Error');
> });
> }
> 
> ```
> 
> });

This is my php file (It is in the server):

> ```
> <? php
> 
> ```

> $message = "email: ".$\_POST[“correo”];  
> $message = htmlspecialchars($mensaje);  
> $message = stripslashes($mensaje);

> $to = “[alexis.970991@gmail.com](mailto:alexis.970991@gmail.com)”;  
> $subject = “You have new email”;  
> $header = “[From:alexis.970991@gmail.com](mailto:From:alexis.970991@gmail.com)”;  
> $enviar = mail($to,$subject,$message,$header);

> ```
> ?> 
> 
> ```

And this is the error.  
 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/d/0/d030769004e1c0280321b888798c2964c3e9c11d.PNG)

Do you believe could you help me? … Thanks a lot! 😁

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [January 15, 2016, 10:33am UTC](https://forum.ionicframework.com/t/angularjs-post-to-php/37677/10 "2016-01-15T10:33:30Z")

</div>

Just copy the code I posted on line two of your PHP - done.

---

<div class="post-metadata">

**Author:** ![Destroy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/destroy/32/94194_2.png) [@Destroy](https://forum.ionicframework.com/u/Destroy)\
**Post date:** [January 15, 2016, 11:42am UTC](https://forum.ionicframework.com/t/angularjs-post-to-php/37677/11 "2016-01-15T11:42:43Z")

</div>

Try to put this in your php file

`<?php header("access-control-allow-origin: *"); ?>`

or this in .htaccess file

`<ifModule mod_headers.c> Header set Access-Control-Allow-Origin: * </ifModule>`
