0

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.

Brian Fleming
  • 63
  • 1
  • 6
  • One problem that i see, your php is excepting post action but you are using http.get(). could you try with post method? – mohit uprim Mar 07 '18 at 04:15
  • Just tried it with a post, no change. Thanks for the suggestion. – Brian Fleming Mar 07 '18 at 15:29
  • Could you try with "PostMan" by specifying url and parameter because most probably data is not coming from the back-end. https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en – mohit uprim Mar 07 '18 at 16:10

2 Answers2

0

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!

Brian Fleming
  • 63
  • 1
  • 6
0

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

Brian Fleming
  • 63
  • 1
  • 6