How to access an array and export variables?

Asked

Viewed 115 times

1

I have a code that receives the data via $_POST on a form as follows:

...
$certificate = $_POST['certificate'];
...

If you make a print_r($certificate) the result is as follows:

Array (
        ['cliente'] => White Martins Gases Industriais Ltda 
        ['entrega'] => 2017-04-11
        ['utilizacao'] => Oxigênio
        ['norma'] => White Martins - PR029
      )

The fields in the form are as follows:

<select type="select" class="form-control" name="certificate['cliente']" id="cliente">
<input type="date" class="form-control" name="certificate['entrega']" id="entrega"> 
<select type="select" class="form-control" name="certificate['utilizacao']" id="utilizacao">
<select type="select" class="form-control" name="certificate['norma']" id="norma">

How to assign the values of this array to 4 separate variables, type:

$cliente = ????;
$entrega = ????;
$utilizacao = ????;
$norma = ????;

I don’t know how to access the array.

  • The fields in the form are as follows: <select type="select" class="form-control" name="Certificate['client']" id="customer"> <input type="date" class="form-control" name="Certificate['delivery']" id="delivery"> <select="select type" class="form-control" name="Certificate['usage']" id="usage"> <select type="select" class="form-control" name="Certificate['norm']" id="norm"> but I can’t access it at all.

  • I can’t access the indexes...

3 answers

3

One way to access the content would be to inform the name of the array,:

echo $certificate['cliente'];

Another way would also be using the Extract , that performs the function that Voce wants, of extracting the content and transforms into variables, thus:

 extract($certificate);
 echo $cliente;

A very simple example for your case, using array:

HTML

<form action="" method="post"><select type="select" class="form-control" name="certificate[]" id="cliente" value="cliente">
    <input type="text" class="form-control" name="certificate[entrega]" id="entrega" value="entrega"> 
    <input type="text" class="form-control" name="certificate[utilizacao]" value="utilizacao">
    <input type="submit">
</form>

Ai in PHP, Voce would take the data as follows:

<?php 
    $certificate=$_POST['certificate'];
    echo $certificate['entrega'];
?>

I hope I’ve helped

  • Notice: Undefined variable: client in C: xampp htdocs lince Certificates pdf.php on line 4

  • @Fláviokowalske Not solved yet? None of the ways worked?

  • @Fláviokowalske saw there how are the inputs/selects Names... leave them without the certificate let alone <select type="select" class="form-control" name="cliente" id="cliente">

  • 1

    Recalling that the extract can be dangerous, the first solution (just like @Divoso’s is safer). I recommend reading this at https://davidnoren.com/post/php-extract-vulnerability.html.

  • @Localhost nothing worked out....

  • @Localhost tested here the way you suggested, and it worked, but I need to take the variables one by one on the other side. the idea is to take the array with all...

  • @Fláviokowalske I get it, I’ll see if I can make an example here, and I already put...

  • @Fláviokowalske take a look now... If this is it, mark as answer...

Show 3 more comments

1


You can use the list(). However, as you do not use numeric Dexes you will need to use the array_values, as an example:

list($cliente, $entrega, $utilizacao, $norma) = array_values($_POST['certificate']);

Test it out here.

Then you can use $cliente, $entrega normally.

1

Have you tried that?

$cliente = $certificate['cliente'];
$entrega = $certificate['entrega'];
$utilizacao = $certificate['utilizacao'];
$norma = $certificate['norma'];
  • Gives the following error: Notice: Undefined index: client in C: xampp htdocs lince Certificates pdf.php on line 3 Notice: Undefined index: delivery in C: xampp htdocs lince Certificates pdf.php on line 4 Notice: Undefined index: utilization in C: xampp htdocs lince Certificates pdf.php on line 5 Notice: Undefined index: norma in C: xampp htdocs lince Certificates pdf.php on line 6

Browser other questions tagged

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