Angular Material. How do I pass a parameter to PHP

2019-08-17 17:38发布

I am trying to replicate an app in Angular Material that I have had running for a couple years with HTTP/JavaScript etc. The app is what could be called a database type app with CRUD operations on a MySQL database on a remote server which uses PHP between the App and the Database.

Several of the tables which are returned from the server in JSON are range limited by a parameter sent from the JavaScript, and then received by the PHP, and used in the where clause of the Select statement like this code snippet:

$registration      = $_POST['REGISTRATION'] ;
$query  = "SELECT sysid, REGISTRATION, TYPE, COMPONENT, SERIAL, PART_NUMBER FROM status_hours WHERE REGISTRATION='$registration' ";

This HTTP/JavaScript/PHP code is working perfectly, however I would like to replicate the app in Angular Material.

With Angular Material I can retrieve recors in JSON perfectly, but the problem is that I can not send a parameter and get it working so that it can be used in the WHERE clause in the PHP.

Here is the Type Script code on the app which gets a table of rows from the server.

const url     = 'http://mydatabase.com/angular/php/status_hours.php'               ;
    const options = { params: new HttpParams().set('REGISTRATION', this.REGISTRATION) }   ;
    this.http.get<Hours[]> (url, options)
    .subscribe(hours => 
    {
        console.log(hours)            ;
        this.dataSource.data = hours  ; 
    } )  ;

Here is the PHP code I have tried, to receive the parameter which is called REGISTRATION so that it can be used in the WHERE statement.

$postdata   = file_get_contents("php://input");
$request    = json_decode($postdata);
$registration   = $request->REGISTRATION;

The problem is either the parameter is not being sent by the Angular app, or it is not being received by the PHP so that it can be used in the WHERE clause.
I can hardcode for sumulation purposes, $registration = 'hardCodedData' ; and it will return valid JSON data.

Thank-you for your assistance.

2条回答
Ridiculous、
2楼-- · 2019-08-17 18:02

Here is a link which I have used to enable me to pass multiple parameters. The same PHP code applies of course.

Angular 4 HttpClient Query Parameters

查看更多
\"骚年 ilove
3楼-- · 2019-08-17 18:10

Here is the answer to my question about proper procedure to pass parameters to PHP from Angular/TypeScript.

The PHP code must contain $registration = $_GET['REGISTRATION'] ; not $registration = $_POST['REGISTRATION'] ; which was working for me with HTML/JavaScript, and which I was using from published example code. Note, REGISTRATION is just the name of the parameter which was being sent by Angular.

Thanks for the assistance everyone!

查看更多
登录 后发表回答