How to take this data from filter_input_array and insert it into another php file

Asked

Viewed 32 times

-2

My filter_input_array receives all the data from the form, but I don’t know how to take this data and insert it into another php file

<?php
    $data = filter_input_array(INPUT_POST, FILTER_DEFAULT);

    if (!empty($data['SendAddMsg'])) {
        //var_dump($data);
        //die();
        $query_msg = "INSERT INTO contacts_msgs (name, email, cpf, contato, created) VALUES (:name, :email, :cpf, :contato, NOW())";
        $add_msg = $conn->prepare($query_msg);

        $add_msg->bindParam(':name', $data['name'], PDO::PARAM_STR);
        $add_msg->bindParam(':email', $data['email'], PDO::PARAM_STR);
        $add_msg->bindParam(':cpf', $data['cpf'], PDO::PARAM_STR);
        $add_msg->bindParam(':contato', $data['contato'], PDO::PARAM_STR);

        $add_msg->execute();

1 answer

-1

Case 1 - If you refer to "another file" as being a file in the same request, then you can create a function (or a class with one method) in the other file to receive the array as parameter. Once this is done, simply include or require the file containing the function (or class) and call the function (or instantiate the class).

https://www.php.net/manual/en/function.include.php

Example:

1 - The one who receives the request:

<?php
    $data = filter_input_array(INPUT_POST, FILTER_DEFAULT);

    if (!empty($data['SendAddMsg'])) {
        //var_dump($data);
        //die();
        $query_msg = "INSERT INTO contacts_msgs (name, email, cpf, contato, created) VALUES (:name, :email, :cpf, :contato, NOW())";
        $add_msg = $conn->prepare($query_msg);

        $add_msg->bindParam(':name', $data['name'], PDO::PARAM_STR);
        $add_msg->bindParam(':email', $data['email'], PDO::PARAM_STR);
        $add_msg->bindParam(':cpf', $data['cpf'], PDO::PARAM_STR);
        $add_msg->bindParam(':contato', $data['contato'], PDO::PARAM_STR);

        $add_msg->execute();

        //Chama o arquivo dois com a função a ser utilizada.
        require_once('arquivo2.php');

        //Chama a função e passa os valores como parâmetro.
        funexemplo($data);
    }
?>

File 2 - The one that contains the function you want to use the data.

<?php
    funexemplo($data){
         //Utilize a variável "data" para utilizar os valores.
    }
?>

Case 2 - If you refer to "another file" as another page that will be loaded, then you can leave it stored in Session or Cookie, in PHP, or in Localstorage, in Javascript.

https://www.php.net/manual/en/book.session.php https://www.php.net/manual/en/features.cookies.php

Example:

1 - The one who receives the first request:

<?php
    $data = filter_input_array(INPUT_POST, FILTER_DEFAULT);

    if (!empty($data['SendAddMsg'])) {
        //var_dump($data);
        //die();
        $query_msg = "INSERT INTO contacts_msgs (name, email, cpf, contato, created) VALUES (:name, :email, :cpf, :contato, NOW())";
        $add_msg = $conn->prepare($query_msg);

        $add_msg->bindParam(':name', $data['name'], PDO::PARAM_STR);
        $add_msg->bindParam(':email', $data['email'], PDO::PARAM_STR);
        $add_msg->bindParam(':cpf', $data['cpf'], PDO::PARAM_STR);
        $add_msg->bindParam(':contato', $data['contato'], PDO::PARAM_STR);

        $add_msg->execute();

        //Armazena o valor em Cookie (Nesse exemplo ele tem validade de 30 dias).
        setcookie("cookie_teste", serialize($data), time() + (86400 * 30), "/");

    }
?>

file 2 - The one who receives the second request (where the data needs to be used):

<?php
    $saved_data = unserialize($_COOKIE['cookie_teste']);

    var_dump($saved_data )
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.