4 votes

How to use $ http.post and $ http.get in the same function?

A few weeks ago I started using AngularJs for an application of the typical management style.

My doubt is the following:

I use it with PHP, by using the method $http.post sent the parameters to a file 'archivo.php' who takes the variables and run a query on MySQL.

I also use $http.get to receive arrays from another file .php.

Now the question, how do I use a $http.post and $http.get in the same function?

Let's say that in php I need to do something like this:

<?php
 include('conn.php');
 $data = json_decode(file_get_contents('php://input'));

 $variable = $conn->real_escape_string($data->variable);

 $query = 'INSERT INTO datos VALUES ("'.$variable.'")';
 $conn->query($query);

 print $query;

?>

I need to pass parameters to php, than php I run what I need, and that returns to me something "x" by means of print, therefore, in Angular, I need to send the parameters, and after receiving them, all in the same file, any ideas?

2voto

Marcos Points 15902

To detect the method in which the request was made, you can use:

 $_SERVER['REQUEST_METHOD']
 

For example:

 if ($_SERVER['REQUEST_METHOD'] === 'POST') {
     // Logica para solicitudes POST
} else if ($_SERVER['REQUEST_METHOD'] === 'GET') {
     // Logica para solicitudes GET
}
 

More info here .

0voto

Barcho Points 1

You can use $_REQUEST to read the data of both GET and POST. Then you combine them with an is and you have solved the problem. http://php.net/manual/es/reserved.variables.request.php

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X