0

How to call a PHP function from angular 4? I have written the below code in my files and it returns nothing.

    .component.ts
constructor(private _server:ServerConnectService) {..}
    $('#fileUpload').on('click', (e)=>
            {
              this._server.uploadImage("Testing..").done((res)=>{ 
                console.log("uploadImage Response "+res); // I need a result->"PHP -> uploadImage() calling " and data here
              });
          });


    .service.ts
export class ServerConnectService {
uploadImageUrl:string = "http://localhost/toolapp/uploadImage.php";
constructor(private _http:HttpClient) { 
  }

    uploadImage(d){
        var myFunction = 'uploadImage';
        var data = {"data":d};
        return $.post(this.uploadImageUrl+"?action="+myFunction,data).then((res)=>{ return res; })
    }


    uploadImage.php
    <?php
     header("Access-Control-Allow-Origin: *");
    function uploadImage($data){
        $res= "PHP -> uploadImage() calling ".$data;
        echo $res;
    }

    ?>

how to achieve this?

Thanks, Guru

3
  • 1
    If you want to upload an image, a GET request is not the right method to do that. Use a PUT or POST request. Commented Feb 4, 2019 at 7:09
  • If you want to upload an image, a GET request is not the right method to do that. Use a PUT or POST request. Commented Feb 4, 2019 at 7:23
  • Hi Henry and Aron, thanks for your quick reply. Im sorry its post method. I have changed my code. Commented Feb 4, 2019 at 7:26

2 Answers 2

1

You have to call HTTP get or post api and then you can use php function.

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

4 Comments

Hi Sachin, Thanks, for your reply. I have used POST method in that. and I have added "url?action=funcName". But its not calling the function in php. If I give echo "..."; before the function on the same .php page, its working. The only thing is function is not calling from angular 4. How to make it? How to use http post in that? Please help. Thanks, Guru
You can not access or call function from angular. You have to manage or call from php code.
Welcome @Guru. Let me know once it done or need any help
It's done calling like the below one. in .service.ts var callFunction = 'uploadImage'; var data = {"data":d, "function":callFunction}; return $.post(this.uploadImageUrl, data).done((res)=>{ return res; }) in PHP $fn = $_POST["function"]; if($fn == "uploadImage"){ $da = $_POST["data"]; uploadImage($da); } else if($fn == "uploadDummy"){ uploadDummy(); } function uploadImage($datas){ $res= "PHP -> uploadImage() calling ".$datas; echo $res; } function uploadDummy(){ echo "uploadDummy() calling..."; }
1

Angular is not server side rendered and hence the following code will not be parsed by the php engine to generate the required javascript. You will have to expose a REST API with POST interface that accepts the image as a file or base64 encoded string.

Then using Angular HttpClientModule or HttpModule you can send the image to the appropriate service.

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.