PHP - Problemas con session jquery y php

 
Vista:
sin imagen de perfil

Problemas con session jquery y php

Publicado por Ernesto (5 intervenciones) el 17/11/2015 08:32:23
Buena noche, de nuevo acudo a ustedes con problemas, he buscado por todo lado pero no ha sido posible encontrar una solucion, estoy tratando de hacer un programa de facturacion donde para completar los campos use jquery para que se vayan agregando los productos desde una base de datos sin actualizar la pagina, en wamp funcionaba todo bien pero al subirlo al hosting mantiene los datos de sesiones que se han cargado en la factura anterior, para evitar que eso pase, borro las session almacenadas al darle clic en el boton de facturar, el problema es que al llenar la factura y enviarla aparecen mensajes de sesiones no definidas, actualizo la pagina y toca volver a cargar todos los datos y ahi si funciona, pero siempre el primer intento falla y toca actializar, agradezco de antemano cualquier apoyo, muchas gracias y exitos.

adjunto los archivos con el codigo que use.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Miguel Angel

Problemas con session jquery y php

Publicado por Miguel Angel (13 intervenciones) el 17/11/2015 11:05:55
Pon el código. No se entiende bien lo que dices. Si usas ajax para cargar datos, porque no lo usas para guardar la factura? Lo que llamas facturar.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Problemas con session jquery y php

Publicado por Ernesto (5 intervenciones) el 17/11/2015 17:40:59
Buena tarde, gracias por responder, claro adjunto esta un archivo codigo .rar donde estan los 4 archivos que use para este proposito, si quieres pego el codigo aca, aunque es largo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Problemas con session jquery y php

Publicado por Ernesto (5 intervenciones) el 18/11/2015 21:16:50
este es mi codigo:

facturacion.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
session_start();
require("conexion.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>VIRA</title>
<script src="js/funciones.js" type="text/javascript"></script>
<script src="js/mootools-core-1.3.2-full-compat-yc.js" type="text/javascript"></script>
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/ajax.js" language="JavaScript" type="text/javascript"></script>
<link href="css/estilo.css" rel="stylesheet" type="text/css" />
<link href='css/style.css' rel='stylesheet' />
<link href="css/estilosindex.css" rel="stylesheet">
 
</head>
<body>
	<div class="subtitulo">
	</div>
	<a class="logout" href="logout.php">Cerrar Sesion</a>
	<a href="inicio.php" class="regresar">REGRESAR</a>
	<center>
	<div class="cajaimagen">
 
		<h2 class="nombre"><?php echo $_COOKIE["nom"]; ?></h2>
 
	</div>
	</center>
	<div class="cajafactu">
		<div class="vendedor">
			<table align="center">
 
				<tr>
				<td width="15%">Vendedor:</td>
				<td width="35%"><select name="vendedor" id="vendedor">
				<?php
				$sql = mysqli_query($mysqli,"SELECT * FROM vendedores");
				while($result = mysqli_fetch_array($sql))
				{
				?>
				<option value="<?php echo $result['Vendedor']; ?>"><?php echo $result['Vendedor']; ?></option>
				<?php
				}
				?>
				</select></td>
				<td width="15%">Almacen:</td>
				<td width="35%"><select name="almacen" id="almacen">
				<?php
				$sql = mysqli_query($mysqli,"SELECT * FROM almacenes");
				while($result = mysqli_fetch_array($sql))
				{
				?>
				<option value="<?php echo $result['Almacen']; ?>"><?php echo $result['Almacen']; ?></option>
				<?php
				}
				?>
				</select></td>
				<td width="35%"><select name="pago" id="pago">
				<option>Tarjeta</option>
				<option>Efectivo</option>
				</select></td>
				</tr>
			</table>
		</div>
	<div class="doccliente">
		Documento del cliente:
		<br><br>
		<input class="doccliente1" type="text" value="<?php echo $_SESSION["cod1"] ?>" name="Doc" id="Doc" readonly>
		<input class="doccliente2" type="text" value="<?php echo $_SESSION["cod2"] ?>" name="Nombre" id="Nombre" readonly>
		<br><br>
	</div>
 
	<div id="cliente">
	</div>
	<div class="codigoproducto">
		Ingrese el codigo del producto:
		<br><br>
		<input type="text" id="codigo" autofocus name="codigo" onChange="MostrarConsulta('consulta.php?codigo='+this.value); return false">
		<br><br>
	</div>
	<div id="factu">
		<table class="tablafactura">
		<thead>
		<th  width="24%">
		Codigo:
		</th>
		<th width="24%">
		Nombre:
		</th>
		<th width="23%">
		Valor:
		</th>
		<th >
		Cantidad:
		</th>
		<th>
		</th>
		</thead>
		</table>
 
	<input type="hidden" id="tempcod" name="tempcod" />
	<input type="hidden" id="tempnom" name="tempnom" />
	<input type="hidden" id="tempval" name="tempval" />
	<input type="hidden" id="tempcan" name="tempcan" />
	</div>
	<div class="enviarfactura">
	<input type="button" value="Imprimir" class="enviar" onclick="imprimirfact()"/>
	</div>
	<br /><br />
	</div>
</div>
</html>

ajax.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
function objetoAjax(){
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
 
var x = 0;
var y=0;
function MostrarConsulta(datos)
{
divResultado = document.getElementById('factu');
ajax=objetoAjax();
ajax.open("GET", datos);
ajax.onreadystatechange=function()
{
if (ajax.readyState==4)
{
//divResultado.innerHTML = ajax.responseText
var NvoCampo= document.createElement("div");
NvoCampo.id= "factu"+x;
var mystr = ajax.responseText;
var myarr = mystr.split("-");
	NvoCampo.innerHTML=
		"<table class='tablafactura'>" +
		"   <tr>" +
		"     <td >" +
		"        <input type='text' name='codig" + x + "' id='codig" + x + "' required value='"+myarr[0]+"'>" +
		"     </td>" +
		"     <td >" +
		"        <input type='text' name='nombre" + x + "' id='nombre" + x + "' required value='"+myarr[1]+"'>" +
		"     </td>" +
		"     <td>" +
		"        <input type='text' name='valor" + x + "' id='valor" + x + "'required value='"+myarr[2]+"'>" +
		"     </td>" +
		"     <td>" +
		"        <input type='text' name='cantidad" + x + "' id='cantidad" + x + "' required value='1'>" +
		"     </td>" +
		"     <td>" +
		"        <a href='JavaScript:quitarCampo(" + x +");'> Quitar </a>" +
		"     </td>" +
		"   </tr>" +
		"</table>"
 
	var contenedor= document.getElementById("factu");
    contenedor.appendChild(NvoCampo);
	x=x+1;
	y=y+1;
}
}
ajax.send(null)
document.getElementById('codigo').value="";
document.getElementById('temp').value=x+1;
}
 
 function quitarCampo(iddiv)
{
  document.getElementById("codig" + iddiv).value="";
  document.getElementById("nombre" + iddiv).value="";
  document.getElementById("valor" + iddiv).value="";
  document.getElementById('cantidad'+iddiv).value="";
  document.getElementById('factu'+iddiv).style.display="none";
}
 
var codigos="";
var nombres="";
var valores="";
var cantidades="";
function imprimirfact()
{
	for (j=0;j<y;j++)
{
  if(!document.getElementById("codig" + j).value=="")
  {
 
	codigos=codigos+document.getElementById('codig'+j).value+'-';
	nombres=nombres+document.getElementById('nombre'+j).value+'-';
	valores=valores+document.getElementById('valor'+j).value+'-';
	cantidades=cantidades+document.getElementById('cantidad'+j).value+'-';
  }
		}
document.getElementById('tempcod').value=codigos;
document.getElementById('tempnom').value=nombres;
document.getElementById('tempval').value=valores;
document.getElementById('tempcan').value=cantidades;
var vendedor=document.getElementById('vendedor').value;
var almacen=document.getElementById('almacen').value;
var Doc=document.getElementById('Doc').value;
var Nombre=document.getElementById('Nombre').value;
var pago=document.getElementById('pago').value;
 
var datosPaises1 = ['',codigos,nombres,valores,cantidades,vendedor,almacen,Doc,Nombre,pago];
	window.addEvent('domready', function(){
		//convierto ese array en un JSON con Mootools
		var miJSON = JSON.encode(datosPaises1);
		//pondríamos esto para ver el JSON generado en la capa con id=resultado
		//$('resultado').set("html", "<pre>" + miJSON + "</pre>");
		//creo una conexión
		var miAjax = new Request({
			url: "cargarvarsessionjquery.php",
			data: "datos=" + miJSON,
		})
		miAjax.send();
	});
	window.open("generafacturapdf.php");
 
}

cargarvarsessionjquery.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
if($_POST){
   $misDatosJSON = json_decode($_POST["datos"]);
 
 
   session_start();
   $_SESSION['codigo']=$misDatosJSON[0];
   $_SESSION['codigos']=$misDatosJSON[1];
   $_SESSION['nombres']=$misDatosJSON[2];
   $_SESSION['valores']=$misDatosJSON[3];
   $_SESSION['cantidades']=$misDatosJSON[4];
   $_SESSION['vendedor']=$misDatosJSON[5];
   $_SESSION['almacen']=$misDatosJSON[6];
   $_SESSION['Doc']=$misDatosJSON[7];
   $_SESSION['Nombre']=$misDatosJSON[8];
   $_SESSION['pago']=$misDatosJSON[9];
 
   //print_r ($_SESSION)."<br>";
}else{
   echo "No recibí datos por POST";
}
?>

generarfactura.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
session_start();
error_reporting(E_ALL ^ E_DEPRECATED);
require('fpdf17/fpdf.php');
require("conexion.php");
$codigos=$_SESSION['codigos'];
$nombres=$_SESSION['nombres'];
$valores=$_SESSION['valores'];
$cantidades=$_SESSION['cantidades'];
$vendedor=$_SESSION['vendedor'];
$almacen=$_SESSION['almacen'];
$Doc=$_SESSION['Doc'];
$Nombre=$_SESSION['Nombre'];
$pago=$_SESSION['pago'];
 
 
//echo $codigos."<br>";
//echo $nombres."<br>";
//echo $valores."<br>";
//echo $cantidades."<br>";
//echo $vendedor."<br>";
//echo $almacen."<br>";
 
 
ob_start();
$pdf = new FPDF('P', 'mm', array(60,10));
$pdf->AddFont('PAPYRUS','','PAPYRUS.php');
$pdf->AddPage();
$pdf->Image('icono.png',3.5,1,3,3);
$pdf->setXY(0,4.5);
$pdf->SetFont('PAPYRUS','',4);
$pdf->Cell(10,1,'VIRA',0,1,'C');
$pdf->SetFont('Arial','B',2);
$pdf->Cell(0,2,'Joyas con magia para el alma',0,1,'C');
$pdf->SetFont('Arial','B',1.8);
$pdf->setXY(0,7.8);
$pdf->Cell(10,0.5,'',0,1,'C');
$pdf->setXY(0,8.5);
$pdf->Cell(10,0.5,' ',0,1,'C');
$pdf->setXY(0,9.2);
$pdf->Cell(10,0.5,'',0,1,'C');
$pdf->setXY(0,9.9);
$pdf->Cell(10,0.5,'',0,1,'C');
$pdf->setXY(0,10.6);
$pdf->Cell(10,0.5,utf8_decode('Bogotá D.C.'),0,1,'C');
$pdf->setXY(0,11.3);
$pdf->Cell(10,0.5,'',0,1,'C');
 
 
 
$archivo = "contador.txt";
$abre = fopen($archivo, "r");
$total = fread($abre, filesize($archivo));
fclose($abre);
$abre = fopen($archivo, "w");
$total = $total + 1;
$total = sprintf("%04d", $total);
$grabar = fwrite($abre, $total);
fclose($abre);
 
 
 
$pdf->SetFont('Arial','B',1.4);
$pdf->setXY(0,13);
$pdf->Cell(10,0.5,'Factura: N-'.$total,0,1);
$pdf->setXY(0,13.9);
$pdf->Cell(5,0.5,'Fecha: '.date('Y')." / " .date('m')." / " .date('d'),0,1);
$pdf->setXY(5,13.9);
$pdf->Cell(5,0.5,'Hora: '.date('H').":" .date('m'),0,1);
$pdf->setXY(0,14.8);
$pdf->Cell(5,0.5,'Vendedor: '.$vendedor,0,1);
$pdf->setXY(5,14.8);
$pdf->Cell(5,0.5,'Almacen: '.$almacen,0,1);
$pdf->setXY(0,15.7);
$pdf->Cell(5,0.5,'Cliente: ',0,1);
$pdf->setXY(3,15.7);
$pdf->Cell(7,0.5,$Nombre,0,1);
$pdf->setXY(0,16.6);
$pdf->Cell(10,0.5,'-----------------------------------------------------------------------------',0,1);
 
$pdf->setXY(0,18);
$pdf->Cell(7,0.5,'Cant.: ',0,1,'L');
$pdf->setXY(4,18);
$pdf->Cell(1,0.5,'Descripcion: ',0,1,'C');
$pdf->setXY(8,18);
$pdf->Cell(2,0.5,'Total: ',0,1,'C');
$ids=explode('-',$codigos);
$desc=explode('-',$nombres);
$uni=explode('-',$cantidades);
$vals=explode('-',$valores);
$numproductos=count($ids)-1;
 
for($x=0;$x<$numproductos;$x++)
{
$pdf->setXY(2,(19+(0.5*$x)));
$pdf->Cell(7,0.5,substr($desc[$x],0,13),0,1,'L');
$pdf->setXY(0,(19+(0.5*$x)));
$pdf->Cell(1,0.5,$uni[$x],0,1,'L');
$pdf->setXY(8,(19+(0.5*$x)));
$pdf->Cell(2,0.5,($uni[$x] * $vals[$x]),0,1,'C');
$grantotal=$uni[$x] * $vals[$x];
$cod=$ids[$x];
$descrip=$desc[$x];
$valor=$vals[$x];
$cant=$uni[$x];
$fecha=date('Y')."-".date('m')."-".date('d');
mysqli_query($mysqli,"INSERT INTO ventas (Codigo,Factura,descripcion,Cantidad,Fecha,Almacen,Vendedor,Doc,Nombre,Valor,pago) VALUES ('$cod','$total','$descrip','$cant','$fecha','$almacen','$vendedor','$Doc','$Nombre','$grantotal','$pago')");
$sql = mysqli_query($mysqli,"SELECT Existencias FROM productos where Codigo='$cod'");
$result = mysqli_fetch_array($sql);
$Nexixtencia=$result['Existencias']-$cant;
mysqli_query($mysqli,"UPDATE productos SET Existencias='$Nexixtencia' where Codigo='$cod'");
}
$pdf->setXY(0,(20+(0.5*$x)));
$pdf->Cell(0,0.5,'------------------------------------------------------------------------------------------------------------------------------',0,1);
$pdf->setXY(0,(21+(0.5*$x)));
$pdf->Cell(8,0.5,'Compra Neta: ',0,1);
$pdf->setXY(8,(21+(0.5*$x)));
$total=0;
$t1=0;
for($x=0;$x<$numproductos;$x++)
{
$total=$total+((($vals[$x]-(($vals[$x]*16)/100)))*$uni[$x]);
$t1=$t1+($vals[$x]*$uni[$x]);
}
$pdf->Cell(2,0.5,$total,0,1,'C');
$pdf->setXY(0,(22+(0.5*$x)));
$pdf->Cell(8,0.5,'IVA. (16%): ',0,1);
$pdf->setXY(8,(22+(0.5*$x)));
$totaliva=(($t1/100)*16);
$pdf->Cell(2,0.5,$totaliva,0,1,'C');
$pdf->setXY(0,(23+(0.5*$x)));
$pdf->Cell(10,0.5,'------------------------------------------------------------------------------------------------------------------------------',0,1);
$pdf->setXY(0,(24+(0.5*$x)));
$pdf->Cell(8,0.5,'TOTAL: ',0,1);
$pdf->setXY(8,(24+(0.5*$x)));
$pdf->Cell(2,0.5,($total+$totaliva),0,1,'C');
$pdf->setXY(0,(25+(0.5*$x)));
$pdf->Cell(10,0.5,'------------------------------------------------------------------------------------------------------------------------------',0,1);
$pdf->setXY(0,(26+(0.5*$x)));
$pdf->Cell(10,0.5,'RESOL: No. 320001223543',0,1,'C');
$pdf->setXY(0,(27+(0.5*$x)));
$pdf->Cell(10,0.5,'del : 02 / 01 / 2015',0,1,'C');
$pdf->setXY(0,(28+(0.5*$x)));
$pdf->Cell(10,0.5,'',0,1,'C');
$pdf->setXY(0,(29+(0.5*$x)));
$pdf->Cell(10,0.5,'',0,1,'C');
$pdf->Output();
ob_end_flush();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
</head>
 
<body>
</body>
</html>

Muchas gracias
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar