how to handle an array and pass it with $_post method

how to handle an array and pass it with $_post method

by Anita Hernandez -
Number of replies: 5

Hey, i'm working with the 2.2 version of moodle and i'm trying to get through a form the values of an array.

What i'm doing with is:

--page1.php--

...

if(!isset($_POST['firstname']) && $_POST['lastname']=="" ) {
$rs = $DB->get_recordset_sql( $query );
foreach( $rs as $r ) {
$nom[] = $r->firstname;
$ape[] = $r->lastname;

         }

    }


else  {

$nom = unserialize(stripslashes($_POST['firstname']));
$ape = unserialize(stripslashes($_POST['lastname']));

}

echo '<FORM METHOD="post" ACTION="crear3.php">
<input type="text" name="tipo" size="20" value="prueba">
<input type="hidden" name="shortname" value="'.$curso.'">
<input type="hidden" name="firstname" value="'.serialize($nom).'">
<input type="hidden" name="lastname" value="'.serialize($ape).'">
<p><input type="submit" value="Enviar datos" name="enviar">
</FORM> ' ;

 

--crear3.php--

...

$titulo = $_POST["tipo"];
$curso = $_POST["shortname"];
$nombre = $_POST["firstname"];
$apellido = $_POST["lastname"];

...

$pdf->Write(1,"Tipo:".$titulo);
$pdf->SetXY(50, 10);
$pdf->Write(1,"Name: ".$nombre." ".$apellido);
$pdf->SetXY(50, 14);
$pdf->Write(1,"Rut: ");
$pdf->SetXY(50, 18);
$pdf->Write(1,"Curso: ".$curso);
$pdf->Image("acf$k.png",168,0,22);

....

 

It doesn't show any error field but instead, in the name area, it shows something like this:

Nombre: a:13:{i:0;s:10: a:13:{i:0;s:11:

any ideas of what i'm doing wrong? 

Average of ratings: -
In reply to Anita Hernandez

Re: how to handle an array and pass it with $_post method

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Do you have Debugging set to DEVELOPER level? If so, that should tell you what to do. Use optional/required_param_array().

In reply to Anita Hernandez

Re: how to handle an array and pass it with $_post method

by Hubert Chathi -

You have: 

<input type="hidden" name="firstname" value="'.serialize($nom).'">
<input type="hidden" name="lastname" value="'.serialize($ape).'">

and then:

$nombre = $_POST["firstname"];
$apellido = $_POST["lastname"];

Which means that $nombre and $apellido will be serialized.  In order to get the actual values, you would need to unserialize them.  But unserializing random POST data is probably a bad idea.

But note that PHP has built-in array handling magic for $_GET and $_POST data, so you can avoid serializing and unserializing.  If you do something like:

foreach ($nom as $i => $n) {
echo '<input type="hidden" name="firstname['.$i.']" value="'.$n.'" />';
}

(and similarly for $ape), then $_POST['firstname'] will magically be an array in crear3.php.

(And as Tim mentions, you should use optional_param_array or required_param_array instead.)

In reply to Hubert Chathi

Re: how to handle an array and pass it with $_post method

by Anita Hernandez -

Hey Hubert and Tim, thanks for yor answers.

I've been trying to do what you told me, and the code i have now is this:

--view.php--

$rs = $DB->get_recordset_sql( $query );

echo '<FORM METHOD="post" ACTION="crear3.php">
<input type="text" name="tipo" size="20" value="prueba">
<input type="hidden" name="shortname" value="'.$curso.'">';
foreach( $rs as $r ) {
$nom[] = $r->firstname;{
foreach ($nom as $n => $o) {
echo '<input type="hidden" name="firstname['.$n.']" value="'.$o.'" />';
}}
$ape[] = $r->lastname;{
foreach ($ape as $a => $p) {
echo '<input type="hidden" name="lastname['.$a.']" value="'.$p.'" />';
}}
$id[] = $r->id;{
foreach ($id as $i => $d){
echo '<input type="hidden" name="id['.$i.']" value="'.$d.'">';
}
}
}
'<p><input type="submit" value="Enviar datos" name="enviar">
</FORM> ' ;

 

--crear3.php--

 

$titulo = $_POST["tipo"];
$curso = $_POST["shortname"];
$nombre = $_POST["firstname"];
$apellido = $_POST["lastname"];
$id = $_POST['id'];


$pdf = new FPDI();

$pdf->setSourceFile('control3.pdf');

$cp=$pdf->setSourceFile('control3.pdf');
for($k=0; $k<=4; $k++) {
QRcode::png(" $nombre - $apellido - $curso - $titulo", "acf$k.png") ;
for($i=1; $i<=$cp; $i=$i+1)

{
if ($i<=$cp){
$tplIdx = $pdf->importPage($i);
// agregar nueva pagina
$pdf->AddPage();
// usar la pagina importada y dejarla en el punto de un auto-ajuste
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, $adjustPageSize=true);
$pdf->SetFont('Arial','',9);
$pdf->SetXY(50, 6);
$pdf->Write(1,"Tipo:".$titulo);
$pdf->SetXY(50, 10);
$pdf->Write(1,"Nombre: ".$nombre." ".$apellido);
$pdf->SetXY(50, 14);
$pdf->Write(1,"Rut: " .$id);
$pdf->SetXY(50, 18);
$pdf->Write(1,"Curso: ".$curso);
$pdf->Image("acf$k.png",168,0,22);
}
...

 

The only problem I have now, is that localhost keeps giving these mistakes:

Notice: Undefined index: firstname in C:\xampp\htdocs\webcursos\blocks\vent\crear3.php on line 26

Notice: Undefined index: lastname in C:\xampp\htdocs\webcursos\blocks\vent\crear3.php on line 27

Notice: Undefined index: id in C:\xampp\htdocs\webcursos\blocks\vent\crear3.php on line 28


I've tried to use the optional_param_array() but i still can't make it work.

Also i've been trying to $_POST the entire query and then, into crear3.php, do the foreach loop. This seems worse than the code i already have.

Any suggestions?

In reply to Anita Hernandez

Re: how to handle an array and pass it with $_post method

by Hubert Chathi -

First of all, you don't want the "foreach ($nom ...)" loop inside the "foreach ($rs ...)" loop.

Secondly, sending the entire query via POST is a VERY BAD IDEA, and is just asking for your site to get hacked.  The first rule of web applications is to never trust user input.

Have you checked the output of view.php to make sure that it is actually writing out the '<input>' lines?  It could be that no records were found.

In reply to Hubert Chathi

Re: how to handle an array and pass it with $_post method

by Anita Hernandez -

Thanks for your help.

Finally i resolved my problem by keeping the url of the course, and making the db query into crear3.php

I changed some parts of the code and now i have what i wanted.

smile