0
Top of the page:
<?php
include "funcoes.php";
session_start();
$grupo_produto = $_SESSION['grupo_produto'];
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<script type="text/javascript" language="javascript" src="Scripts/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('#tabela_produtos').dataTable();
} );
</script>
I am making a system to perform orders and I am having a problem with datatable and PHP. I have a list of all the products.
I search for a product and put the desired amount as shown below:
Until then, no big problems. I look for another product, put the quantity and finalize the order.
The problem is that, at the end of the order, the only products it puts in the array are the last ones I chose (in this case, the 2 Guaraná Lata). He doesn’t consider those Kermit I chose in the beginning.
If I place the order without using the search field or if I search, put the quantity and delete what I searched (going back to the form with all products), it inserts normal.
I won’t put the Datatable code here because it’s too long.
Could you help me, please? Thanks in advance.
Code to choose product:
<body>
<form method="post" action="?acao=escolher_produto">
<table id="tabela_produtos" >
<thead>
<tr>
<td>
Cod.
</td>
<td>
Nome
</td>
<td>
Preço
</td>
<?php
if($grupo_produto == '0'){
?>
<td>
Preço Broto
</td>
<td>
Preço Média
</td>
<td>
Preço Giga
</td>
<?php
}
?>
<td>
Quantidade
</td>
</tr>
</thead>
<?php
$array_produtos = mostrarProdutos($grupo_produto);
?>
<tbody>
<?php
for($i=0; $i< count($array_produtos);$i++){
?>
<tr>
<td>
<?php echo $array_produtos[$i][0]; ?>
</td>
<td>
<?php echo $array_produtos[$i][1]; ?>
</td>
<td>
<?php echo formataValor($array_produtos[$i][2]); ?>
</td>
<?php
if($grupo_produto == '0'){
?>
<td>
<?php echo formataValor($array_produtos[$i][3]);?>
</td>
<td>
<?php echo formataValor($array_produtos[$i][4]);?>
</td>
<td>
<?php echo formataValor($array_produtos[$i][5]);?>
</td>
<?php
}
?>
<td>
<input type="button" value="+" onclick="mais('<?php echo $i;?>')"/> <input type="number" id="<?php echo $i?>" name="<?php echo $i?>" />
<input type="button" value="-" onclick="menos('<?php echo $i;?>')"/>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<input type="submit" value="Escolher">
</form>
</body>
Action by clicking choose:
if(@$_GET['acao'] == 'escolher_produto'){
$array_produtos = mostrarProdutos($_SESSION['grupo_produto']);
if(isset($_SESSION['array_pedido'])){
$array_pedido = $_SESSION['array_pedido'];
}else{
$array_pedido = array();
}
$nenhum_produto = 0;
for($i=0; $i< count($array_produtos);$i++){
if($_POST[$i] != 0){
$codigo_produto = $array_produtos[$i][0];
$descricao = $array_produtos[$i][1];
$valor_unitario = $array_produtos[$i][2];
$quantidade = $_POST[$i];
while($quantidade > 0){
array_push($array_pedido, array($codigo_produto,$descricao, $valor_unitario));
$quantidade--;
}
}
$nenhum_produto++;
}
if($nenhum_produto == 0){
echo "<script> alert('Escolha algum produto'); history.back(); </script>";
}else{
$_SESSION['array_pedido'] = $array_pedido;
echo "<script> location.href ='grupo_produtos.php'; </script>";
}
}
The selected products are in
$_SESSION['array_pedido']
?– Andrei Coelho
Just after I choose all products and click on "Choose" button, it stores in this $_SESSION['array_request'].
– Victor Iroski
And where are the products chosen before the order is closed? Are you in any session?
– Andrei Coelho
They don’t stay anywhere. The products are only registered after clicking on the button (I choose all products and then click on the button choose)... For example: I type guarana, I choose 2... I type coca, I choose 2... Then when I click choose it should put all these 4 products in the array, but it does not.. he understands that the first product researched is not chosen. Now if I type guarana, I choose 2... Type coca, choose 2.. I delete what’s in the search field.. It goes back to the complete form and registers all products
– Victor Iroski
Yes... but when you do the DB search it is done where? Where is this script? Is it in php or javascript? You could post the search script?
– Andrei Coelho
session_start() is where?
– Andrei Coelho
I edited the question and put the code snippet where it initializes Session and where it calls the javascript files. The search is done by the Datatable component.
– Victor Iroski
Just a comment about the HTML there, with the
input type="number"
you wouldn’t need those increment and quantity decrease buttons, it already has those buttons there.– Dudaskank
Thanks for the tip. I’ll fix it.
– Victor Iroski