-1

I'm working with this project and I need to pass an array as an URL parameter and I actually manage to do it with javascript.

function guardarTodo()
{
var datos = [];

var txtfechaCap = document.getElementById("txtfechaCap").value;
datos.push(txtfechaCap);
var cbxLocalidad = document.getElementById("cbxLocalidad").value;
datos.push(cbxLocalidad);
var txtapellidoP = document.getElementById("txtapellidoP").value;
datos.push(txtapellidoP);
var txtapellidoM = document.getElementById("txtapellidoM").value;
datos.push(txtapellidoM);
var txtNombres = document.getElementById("txtNombres").value;
datos.push(txtNombres);
var txtCurp = document.getElementById("txtCurp").value;
datos.push(txtCurp);

var chkSexo;
if(document.getElementById("chkHombre").checked)
    chkSexo = 'H';
else
    chkSexo = 'M';
datos.push(chkSexo);
var txtfecha = document.getElementById("txtfecha").value;
datos.push(txtfecha);
var txtEdad = document.getElementById("txtEdad").value;
datos.push(txtEdad);
var txtPeso = document.getElementById("txtPeso").value;
datos.push(txtPeso);
var txtTalla = document.getElementById("txtTalla").value;
datos.push(txtTalla);
var txtCC = document.getElementById("txtCC").value;
datos.push(txtCC);
for (var i=0;i<document.getElementById('table_depProg').rows.length;i++) 
{
    var prog = [];
    for (var j=0;j<1;j++) 
    {

        var programa = document.getElementById('table_depProg').rows[i].cells[j].innerHTML;
        alert(programa);
        prog.push(programa);
    }
    window.location.href = "funciones/guardar.php?prog="+prog+"&datos[]="+datos;
}
}

Now the problem is that when I try to take the array with the GET method I can't take the index one by one.

<?php
include("funciones.php");
//Persona
print_r($_GET['datos']);
@$fechacaptura = $_GET['datos']['txtfechaCap'];
@$idlocalidad = $_GET['datos']['cbxLocalidad'];
@$apaterno = $_GET['datos']['txtapellidoP'];
@$amaterno = $_GET['datos']['txtapellidoM'];
@$nombre = $_GET['datos']['txtNombres'];
@$curp = $_GET['datos']['txtCurp'];
@$sexo = $_GET['datos']['chkSexo'];
@$fechanacimiento = $_GET['datos']['txtfecha'];
@$edad = $_GET['datos']['txtEdad'];
@$peso = $_GET['datos']['txtPeso'];
@$talla = $_GET['datos']['txtTalla'];
@$cc = $_GET['datos']['txtCC'];

$sql = "CALL personasAdd($idlocalidad,'$fechacaptura','$apaterno','$amaterno','$nombre','$curp','$sexo',    '$fechanacimiento',$edad,$peso,$talla,$cc);";
@$idpersona = personasAdd($sql);
?>

As you can see, I print the array to check how it working but this is the output:

Array ( [0] => 2013-07-03,8,LastName1,LastName2,Name(s),123456789012345678,H,2013-07-01,24,34,45,56 ).

It contains the data but when I print the $sql variable i get this:

CALL personasAdd(,'','','','','','','',,,,);

I've been working in this the whole day and couldn't find what I'm doing wrong, I would really apreciate any help or clue to find my mistake.

Aaron Daw
  • 72
  • 2
  • 9
  • You're not passing `datos` correctly. Take a look at [how to use a `$_GET` array](http://stackoverflow.com/questions/1833330/how-to-get-php-get-array). – Jason McCreary Jul 28 '13 at 02:30

1 Answers1

0

To access each value by name, you have to include the name in the URL parameter. Otherwise, PHP has now way of knowing that the <input> had name="txtfechaCap" when you're just passing the value.

var txtfechaCap = document.getElementById("txtfechaCap").value;
datos.push('datos[txtfechaCap]=' + txtfechaCap);

var cbxLocalidad = document.getElementById("cbxLocalidad").value;
datos.push('datos[cbxLocalidad]=' + cbxLocalidad);

// etc.

encodeURIComponent() would also be a good idea since input.value can certainly include special characters for URLs:

var txtfechaCap = document.getElementById("txtfechaCap").value;
datos.push('datos[txtfechaCap]=' + encodeURIComponent(txtfechaCap));

// etc.

But, with the names now included in datos, just join the Array with '&' and append:

window.location.href = "funciones/guardar.php?prog="+prog+"&"+datos.join('&');

You may also want to do similar to prog:

// ...
    prog.push('prog[]=' + encodeURIComponent(programa));
}

window.location.href = "funciones/guardar.php?"+prog.join("&")+"&"+datos.join('&');
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • It worked perfect, thank you so much for your help, I realized of all my mistakes. – Aaron Daw Jul 28 '13 at 04:55
  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results" My question wasn't asking for code, was asking for help, an user could have said "your problem is this or that", I guess my mistake was that I didn't share what I exactly did after getting help, but the reason is that I realized of my mistakes after seeing Jonathan Lonowski's help and that was because I just followed his solution, and that doesn't mean I didn't understand what he did and what I was doing. – Aaron Daw Jul 28 '13 at 14:12