获取POST参数并将其发送到第二个PHP文件

时间:2017-12-19 03:12:47

标签: php

我需要使用以下PHP文件:

leer_directorio_todos_filtrados.php

<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

// include database and object files
include_once 'database.php';
include_once 'directorio_todo_filtrado.php';

// instantiate database and product object
$database = new Database();
$db = $database->getConnection();

// initialize object
$product = new Directorio($db);

// query products
$stmt = $product->read();
$num = $stmt->rowCount();

//here the POST request parameter
    $received = $_POST['valor']:


// check if more than 0 record found
if($num>0){

    // products array
    $products_arr=array();
   // $products_arr["records"]=array();

    // retrieve our table contents
    // fetch() is faster than fetchAll()
    // http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        // extract row
        // this will make $row['name'] to
        // just $name only
        extract($row);

        $product_item=array(
            "id" => $id_directorio,
            "nombre" => $nombre,
            "apellidos" => $apellidos,
            "apodo" => $apodo,
            "cumple" => $cumple,
            "conyuge" => $conyuge,
            "cumple_conyuge" => $cumple_conyuge,
            "aniversario_bodas" => $aniversario_bodas,
            "empresa" => $empresa,
            "direccion_empresa" => $direccion_empresa,
            "tel_negeocio" => $tel_negocio,
            "fecha_ingreso" => $fecha_ingreso,
            "num_rotario" => $num_rotario,
            "padrino" => $padrino,
            "direccion_casa" => $direccion_casa,
            "tel_casa" => $tel_casa,
            "celular" => $celular,
            "email" => $email,
            "email_privado" => $email_privado,
            "clasificacion" => $clasificacion,
            "imagen" => $imagen
        );

        array_push($products_arr, $product_item);
    }

    echo json_encode($products_arr);
}

else{
    echo json_encode(
        array("message" => "No items found.")
    );
}
?>

和directorio_todo_filtrado.php:

<?php
class Directorio{

    // database connection and table name
    private $conn;
    private $table_name = "tb_directorio";

    // object properties
    public $id_directorio;
    public $nombre;
    public $apellidos;
    public $apodo;
    public $cumple;
    public $conyuge;
    public $cumple_conyuge;
    public $aniversario_bodas;
    public $empresa;
    public $direccion_empresa;
    public $tel_negocio;
    public $fecha_ingreso;
    public $num_rotario;
    public $padrino;
    public $direccion_casa;
    public $tel_casa;
    public $celular;
    public $email;
    public $email_privado;
    public $clasificacion;
    public $imagen;
    public $usuario_app;


    // constructor with $db as database connection
    public function __construct($db){
        $this->conn = $db;
    }

    function read(){

           // select all query
           $query = "SELECT
                       *
                   FROM
                       " . $this->table_name . " 
                     ORDER BY apellidos   ";

           // prepare query statement
           $stmt = $this->conn->prepare($query);

           // execute query
           $stmt->execute();

           return $stmt;
       }
}

我想向第一个文件发送POST请求,但后来我不知道将POST参数作为变量传递给第二个文件,以便过滤SELECT查询。

1 个答案:

答案 0 :(得分:1)

您可以将POST参数传递给read对象的Directorio方法。

leer_directorio_todos_filtrados.php

<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

// include database and object files
include_once 'database.php';
include_once 'directorio_todo_filtrado.php';

// instantiate database and product object
$database = new Database();
$db = $database->getConnection();

// initialize object
$product = new Directorio($db);

//here the POST request parameter
$received = $_POST['valor']:

// query products
$stmt = $product->read($received);
$num = $stmt->rowCount();

// check if more than 0 record found
if($num>0){

    // products array
    $products_arr=array();
   // $products_arr["records"]=array();

    // retrieve our table contents
    // fetch() is faster than fetchAll()
    // http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        // extract row
        // this will make $row['name'] to
        // just $name only
        extract($row);

        $product_item=array(
            "id" => $id_directorio,
            "nombre" => $nombre,
            "apellidos" => $apellidos,
            "apodo" => $apodo,
            "cumple" => $cumple,
            "conyuge" => $conyuge,
            "cumple_conyuge" => $cumple_conyuge,
            "aniversario_bodas" => $aniversario_bodas,
            "empresa" => $empresa,
            "direccion_empresa" => $direccion_empresa,
            "tel_negeocio" => $tel_negocio,
            "fecha_ingreso" => $fecha_ingreso,
            "num_rotario" => $num_rotario,
            "padrino" => $padrino,
            "direccion_casa" => $direccion_casa,
            "tel_casa" => $tel_casa,
            "celular" => $celular,
            "email" => $email,
            "email_privado" => $email_privado,
            "clasificacion" => $clasificacion,
            "imagen" => $imagen
        );

        array_push($products_arr, $product_item);
    }

    echo json_encode($products_arr);
}

else{
    echo json_encode(
        array("message" => "No items found.")
    );
}
?>

和directorio_todo_filtrado.php:

<?php
class Directorio{

// database connection and table name
private $conn;
private $table_name = "tb_directorio";

// object properties
public $id_directorio;
public $nombre;
public $apellidos;
public $apodo;
public $cumple;
public $conyuge;
public $cumple_conyuge;
public $aniversario_bodas;
public $empresa;
public $direccion_empresa;
public $tel_negocio;
public $fecha_ingreso;
public $num_rotario;
public $padrino;
public $direccion_casa;
public $tel_casa;
public $celular;
public $email;
public $email_privado;
public $clasificacion;
public $imagen;
public $usuario_app;


// constructor with $db as database connection
public function __construct($db){
    $this->conn = $db;
}

function read($filter){

       // select all query
       $query = "SELECT
                   *
               FROM
                   " . $this->table_name . " 
                 ORDER BY apellidos   ";

       // prepare query statement
       $stmt = $this->conn->prepare($query);

       // execute query
       $stmt->execute();

       return $stmt;
   }
}