1
How can I transfer a Javascript array to a PHP array ?
Javascript function
array of expiration dates (datasvenc) in the registration file.php:
function calculamensalidades(){
var datasvenc = new Array(7);
var tabela;
tabela = "<br><table border='0' width='30%' style='text-align:center'><tr><td bgcolor='#4682B4'>Parcela</td><td bgcolor='#4682B4' >Valor</td><td bgcolor='#4682B4'>Vencimento</td></tr>";
for(var a=0; a<document.getElementById("select_parcelas").value; a++)
{
var n_date = new Date(date.getFullYear(), eval(a+mesvencimento), diavencimento);
var diavenc = date.getDate();
var mesvenc = n_date.getMonth()+1;
var anovenc = n_date.getFullYear();
tabela = tabela + "<tr><td bgcolor='#9AC0CD'>"+(a+1)+"</td><td bgcolor='#9AC0CD'>R$ "+valorparcela.toFixed(2)+"</td><td bgcolor='#9AC0CD'>"+diavenc+"/"+mesvenc+"/"+anovenc+"</td></tr>";
datasvenc[a] = diavenc+"/"+mesvenc+"/"+anovenc;
}
}
I need to insert the correct expiration date for each table record in the same registration file.php:
if (isset($_GET['cadastra']) && $_GET['cadastra'] == 'add') {
$dataVencimento = filter_input(INPUT_GET, 'datasvenc');
$dataVencimento = unserialize(base64_decode($dataVencimento));//Decode para array
for($numparcelas=1; $numparcelas <= $parcelas; $numparcelas++ ){
$cadastraparcelas = $conn->prepare("INSERT INTO t_cadparcelas (NumContrato, NumParcela, ValorParcela, DataVencimento, Status) VALUES (?, ?, ?, ?, ?)");
$cadastraparcelas->execute(array($IDultimocontrato, $numparcelas, $valorparc, $dataVencimento[1], $status));
}
...
Can use
JSON.stringify(array)
in the JS andjson_decode($_GET['array'], true);
in PHP, already tested?– Sergio
hello @Sergio thanks for the return, never tested, can post some example you can use with my code above? thanks again
– Rafael Assmann
The answer I gave was what I was looking for?
– Sergio
@Sergio helped, but I simply passed the javascript variable to an input and soon after treated the string of dates in php for insertion in the database, thanks a lot for the help!
– Rafael Assmann