El index.php
Código PHP:
<?php
function generarEmpresas()
{
$conexion = mysql_connect("localhost","braian","terminal");
if(!$conexion){
die('No se pudo conectar'.mysql_error());
}
mysql_select_db("empresas",$conexion);
echo "<select name='nomEmpresas' id='nomEmpresas'>";
echo "<option value='0'>Elegir empresa</option>";
$consulta=mysql_query("SELECT `nombre` FROM `terminal`.`empresas` ORDER BY `nombre` ASC") or die(mysql_error());
while($registro=mysql_fetch_row($consulta))
{
echo "<option value='".$registro[0]."'>".$registro[0]."</option>";
}
echo "</select>";
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<script language="JavaScript" type="text/javascript" src="ajax.js"></script>
<title>Formulario</title>
</head>
<body>
<div id="botones">
<a href="/vista.html"><button> Vistas </button></a>
<a href="/index.php"><button> Lista </button><br></a>
</div>
<div>
<form id="formEmpresa" name="formEmpresa" action="" onsubmit="crearEmpresa(); return false">
<h3>Ingresar nueva Empresa:</h3>
Nombre: <input name="nombreEmpresa" type="text"/>
<input type="submit" name="Submit" value="Guardar"/>
</form>
</div>
<div>
<form id="formViaje" name="formViaje" action="" onsubmit="inserViaje(); return false">
<h3>Viajes:</h3>
<div id="empresa">
<?php
generarEmpresas();
?>
</div>
<br>
Origen: <input name="origen" type="text"/>
Destino: <input name="destino" tpe="text"/>
Plataforma: <input name="plataforma" type="text" />
Hora: <input name="hora" type="time" />
<br><br>
Lunes <input id="lunes" name="lunes" type="checkbox" />
Martes <input id="martes" name="martes" type="checkbox" />
Miercoles: <input id="miercoles" name="miercoles" type="checkbox" />
Jueves: <input id="jueves" name="jueves" type="checkbox" />
Viernes: <input id="viernes" name="viernes" type="checkbox" />
Sabado: <input id="sabado" name="sabado" type="checkbox" />
Domingo: <input id="domingo" name="domingo" type="checkbox" />
<br><br>
<input type="submit" id="insertarViaje" name="insertarViaje" value="Insertar"/>
<br><br>
<button type="reset" onclick="limpiarFormulario();">Limpiar Formulario</button>
<br><br>
</form>
<form id="formFeriado" name="formFeriado" action="" onsubmit="crearFeriado(); return false">
<h3>Feriado</h3>
<input type="checkbox" name="check" id="check" value="1" onchange="javascript:showContent()" />
<div id="content" style="display: none;">
<input id="fechaFeriado" name="fecha" type="date"/><br><br>
<input id="insertarFeriado" type="submit" name="insertarFeriado" value="Insertar" />
</div>
</form>
</div>
</body>
</html> el ajax.js:
Código:
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;
}
function crearEmpresa(){
nom=document.formEmpresa.nombreEmpresa.value;
ajax=objetoAjax();
ajax.open("POST", "insertar_empresa.php",true);
ajax.onreadystatechange=function() {
if (ajax.readyState==4) {
//divResultado.innerHTML = ajax.responseText
}
}
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send("nombreEmpresa="+nom+"")
}
function inserViaje(){
emp=document.formViaje.nomEmpresas.value;
ori=document.formViaje.origen.value;
dest=document.formViaje.destino.value;
plat=document.formViaje.plataforma.value;
hor=document.formViaje.hora.value;
lun=document.formViaje.lunes.value;
mar=document.formViaje.martes.value;
mie=document.formViaje.miercoles.value;
jue=document.formViaje.jueves.value;
vie=document.formViaje.viernes.value;
sab=document.formViaje.sabado.value;
dom=document.formViaje.domingo.value;
ajax=objetoAjax();
ajax.open("POST", "insertar_viaje.php",true);
ajax.onreadystatechange=function() {
if (ajax.readyState==4) {
//divResultado.innerHTML = ajax.responseText
}
}
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send("nomEmpresas="+emp+"&origen="+ori+"&destino="+dest+"&plataforma="+plat+"&hora="+hor+"&lunes"+lun+"&martes"+mar+"&miercoles"+mie+"&jueves"+jue+"&viernes"+vie+"&sabado"+sab+"&domingo"+dom+"")
}
function crearFeriado(){
emp=document.formViaje.nomEmpresas.value;
ori=document.formViaje.origen.value;
dest=document.formViaje.destino.value;
plat=document.formViaje.plataforma.value;
hor=document.formViaje.hora.value;
fec=document.formFeriado.fechaFeriado.value;
ajax=objetoAjax();
ajax.open("POST", "insertar_feriado.php",true);
ajax.onreadystatechange=function() {
if (ajax.readyState==4) {
//divResultado.innerHTML = ajax.responseText
}
}
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send("nomEmpresas="+emp+"&origen="+ori+"&destino="+dest+"&plataforma="+plat+"&hora="+hor+"&fecha"+fec+"")
}
function limpiarFormulario(){
document.getElementById("myForm").reset();
document.getElementById("fechaFeriado").reset();
}
function showContent() {
element = document.getElementById("content");
check = document.getElementById("check");
ins = document.getElementById("insertarViaje");
if (check.checked) {
element.style.display='block';
ins.style.display='none';
}
else {
element.style.display='none';
ins.style.display='block';
}
}
function mostrarInfo(cod){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("datos").innerHTML=xmlhttp.responseText;
}else{
document.getElementById("datos").innerHTML='Cargando...';
}
}
xmlhttp.open("POST","crear_tabla_empresa.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("empresa="+cod);
}
Y el insert a la base de datos:
Código PHP:
<?php
$conexion = mysql_connect("localhost","braian","terminal");
if(!$conexion){
die('No se pudo conectar'.mysql_error());
}
mysql_select_db("viajes",$conexion);
//variables POST
$emp=$_POST['nomEmpresas'];
$ori=$_POST['origen'];
$dest=$_POST['destino'];
$plat=$_POST['plataforma'];
$hor=$_POST['hora'];
$lun=$_POST['lunes'];
$mar=$_POST['martes'];
$mie=$_POST['miercoles'];
$jue=$_POST['jueves'];
$vie=$_POST['viernes'];
$sab=$_POST['sabado'];
$dom=$_POST['domingo'];
if ($lun == "on") {
mysql_query("INSERT INTO `terminal`.`viajes` (`id`, `empresa`, `origen`, `destino`, `plataforma`, `dia`, `hora`) VALUES (NULL, '$emp', '$ori', '$dest', 'Monday', '$hor');") or die(mysql_error());
}
if ($lun == "on") {
mysql_query("INSERT INTO `terminal`.`viajes` (`id`, `empresa`, `origen`, `destino`, `plataforma`, `dia`, `hora`) VALUES (NULL, '$emp', '$ori', '$dest', 'Tuesday', '$hor');") or die(mysql_error());
}
if ($lun == "on") {
mysql_query("INSERT INTO `terminal`.`viajes` (`id`, `empresa`, `origen`, `destino`, `plataforma`, `dia`, `hora`) VALUES (NULL, '$emp', '$ori', '$dest', 'Wednesday', '$hor');") or die(mysql_error());
}
if ($lun == "on") {
mysql_query("INSERT INTO `terminal`.`viajes` (`id`, `empresa`, `origen`, `destino`, `plataforma`, `dia`, `hora`) VALUES (NULL, '$emp', '$ori', '$dest', 'Thursday', '$hor');") or die(mysql_error());
}
if ($lun == "on") {
mysql_query("INSERT INTO `terminal`.`viajes` (`id`, `empresa`, `origen`, `destino`, `plataforma`, `dia`, `hora`) VALUES (NULL, '$emp', '$ori', '$dest', 'Friday', '$hor');") or die(mysql_error());
}
if ($lun == "on") {
mysql_query("INSERT INTO `terminal`.`viajes` (`id`, `empresa`, `origen`, `destino`, `plataforma`, `dia`, `hora`) VALUES (NULL, '$emp', '$ori', '$dest', 'Saturday', '$hor');") or die(mysql_error());
}
if ($lun == "on") {
mysql_query("INSERT INTO `terminal`.`viajes` (`id`, `empresa`, `origen`, `destino`, `plataforma`, `dia`, `hora`) VALUES (NULL, '$emp', '$ori', '$dest', 'Sunday', '$hor');") or die(mysql_error());
}
mysql_close($conexion);
?> Espero que sea legible. En el ultimo archivo antes de agregar todos esos IF´s ya no andaba. Aunque probando en el navegador invocando al archivo con todos los parametros puedo hacer que ingrese el dato. Le problema esta en algo del Ajax a mi parecer. Desde ya muchas gracias, un saludo!
Braian


