receive form with var_dump

Asked

Viewed 432 times

1

Guys, currently I send one form with several POST, I receive them all and add them in the BD. However I am seeing a method using the var_dump, but I don’t know how to implant it into my code.

An example of how I do today:

            <?php
        if ((!empty($action)) and ( $action == "envia")) {

            // Recebe dados
            $nome1 = filter_input(INPUT_POST, 'nome1', FILTER_DEFAULT);
            $nome2 = filter_input(INPUT_POST, 'nome2', FILTER_DEFAULT);

            // Adino no BD mysql
            $mysqli->query("INSERT INTO nomes VALUES ('0', '$nome1')");
            $mysqli->query("INSERT INTO nomes VALUES ('0', '$nome2')");
        }
        ?>

        <form name="form" method="post" action="index.php?action=envia">
            <input  type='text' name='nome1'>
            <input  type='text' name='nome2'>      
            <button type='submit'>Enviar</button>
        </form>

Is there any way I can simplify this with var_dump. This is a simple example, because I have a form with about 30 POST.

  • 2

    the var_dump() doesn’t seem to solve your problem, it’s more like a array!

  • You use an Insert in the same table for each element of your form?

  • @Fleuquerlima this is an example, I actually have one form where I can register up to 10 user at once, so wanted to change the method I do understood.

  • 1

    var_dump() was not meant to receive or send anything, but rather to print an array / object on the screen.

  • @rray as would this method with array?

  • 1

    Can do thus. Your form field should send an array, so just add brackets in the name make a check, after make a foreach with the Insert or mount the string of the Insert and send at once to the bank.

  • @rray can answer the question with an example? so I can accept and finish the question.

  • But he already put an example in the post that gave you the link, a look there

  • Okay, I hadn’t seen hahahha

Show 4 more comments

1 answer

3


I didn’t test because mine php is not recognizing the filter_input, but so you must take:

<?php
// povoando post (para teste)
$_POST['nome1'] = "o";
$_POST['nome2'] = "v";
$_POST['nome3'] = "e";
$_POST['nome4'] = "r";
$_POST['nome5'] = "f";
$_POST['nome6'] = "l";
$_POST['nome7'] = "o";
$_POST['nome8'] = "w";

var_dump($_POST);

$valores = array();
foreach($_POST as $key => $value)
    array_push($valores, filter_input(INPUT_POST, $key, FILTER_DEFAULT));

var_dump($valores);

foreach($valores as $value)
    $mysqli->query("INSERT INTO nomes VALUES ('0', '$value')");

As quoted in the comments, the var_dump serves only for display, according to the documentation:

var_dump - Displays information about the variable

This function will show a structured representation over one or more expressions, including type and value. Arrays and objects are recursively explored with idented values in the structure shown.

That is, it is only used to test the system, see the values of its variables.

In the code I used a foreach to go through the $_POST data, thus filtering the data and putting the filtration result on a list, after that I have another foreach who will perform in the mysqli. The same could be done with:

foreach($_POST as $key => $value)
{
    $value = filter_input(INPUT_POST, $key, FILTER_DEFAULT);
    $mysqli->query("INSERT INTO nomes VALUES ('0', '$value')");
}

Or else:

foreach($_POST as $key => $value)
    $mysqli->query("INSERT INTO nomes VALUES ('0', '" . filter_input(INPUT_POST, $key, FILTER_DEFAULT) . "')");
  • 2

    Stylish this settlement there...

Browser other questions tagged

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