0

I try to execute a simple PHP file and get this return or echo, it is possible with Angular 8 ?

I well build my Angular project with ng build and put it in Nginx server with PHP configuration

My PHP file don't have a spell mistake error, and I put it inside the make-bulletin inside asset folder.

Actually I made a service sending a POST request in my PHP file locally I also try with, return this.http.post("assets/make-bulletin/test.php"... but it happen the same thing

My service:

public makeBulletin(ville: string, pollution: string, date: string) {
    return this.http.request("POST", "assets/make-bulletin/test.php", {
      params: {
        Ville: ville,
        Pollution: pollution,
        Date: date,
      },
    })
  }

My .ts for call my service and subscribe:

public onSubmit() {
    this.bs.makeBulletin("Lyon", "Atmospherique", "2019-06-01").subscribe(
      resp => {
        // How print PDF to the user after get the PDF ?
      },
      error => {
       // get error
      }
    )

My PHP file:

<?php
  $ville = $_POST["Ville"];
  echo $ville;
  return $ville;
?>

I tried to add: header("Content-type: text/html") and header("Access-Control-Allow-Origin: *")

But I get a 200 OK so good... but, with a null response in my resp...

How can I get/read a answer from my PHP file ?

Thanks in advance

1
  • PHP is server-side and angular is client-side which runs in browser so calling PHP file from assets folder won't do anything. you can place the php file in some apache server which is created locally like using wamp and call then call the url like :localhost:/some-url Commented Oct 20, 2019 at 17:29

2 Answers 2

1

You seem to be confused about the difference between server-side code and client (in-browser) code.

Your PHP code is intended to be run on a server. It cannot be executed in a web browser.

An Angular application (after being compiled to JavaScript) is run in the user's browser.

So, it makes no sense to embed PHP code in you Angular application, because the browser cannot run it.

Your server-side PHP code and you client-side Angular code need to be two separate applications. That said, they can be deployed side-by-side on the server, with Nginx both fronting your PHP application and serving the Angular application to client's browsers.

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

2 Comments

Thanks !, I don't realize, it's true we can't execute PHP in the frontend...
But I have doubt about something, because we actually can POST data with a from in HTML to a PHP file, like this <form method="post" action="pdf.php">, some old style but this thing work, I don't know why we can't with Angular...
0

A little bit too late but, Angular is expecting to get a JSON from PHP, so in your PHP you need to do an "echo json_encode($ville);"

public onSubmit() {
this.bs.makeBulletin("Lyon", "Atmospherique", "2019-06-01").subscribe({
   next: (data: any) => {
       console.log(data);
   },
   error: err => console.error(err)
});

Now you can do what you want with "data" wich contains anything you echoed from PHP.

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.