Ver Mensaje Individual
  #3 (permalink)  
Antiguo 16/01/2013, 13:04
summerblack
 
Fecha de Ingreso: diciembre-2012
Mensajes: 249
Antigüedad: 11 años, 4 meses
Puntos: 2
Respuesta: Como enlazar paginas .html .php y .js

bueno posteare el codigo y he hecho unos cambios reduciendo la cantidad de archivos muchas gracias si alguien me puede colaborar, la idea es que me guarde en la BD y me muestre los datos sin necesidad de formato en el div que dice "guardando".
*Lo de un formulario sin form es que quisiera saber como puedo remplazar la propiedad action sin necesidad de usar la etiqueta <form>


index.html


<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
</head>
<body>

<div id="Div1" >
<form action="javascript:guardarUsuario()" method="post">
<label for="documento"> Documento</label> <input name="documento" type="text" size="30" maxlength="30" tabindex="1">
<label for="expedicion">Expedici&oacute;n</label> <input name="expedicion" type="text" size="30"maxlength="30" tabindex="2">
</form>
</div>
<div id="guardando" >
aca va el contenido
</div>
<script src="js/ajax.js" type="text/javascript"> </script>
</body>
</html>



////////////////////////////////////////////////////////////////////////////////////////////////////////
funciones.php

<?php

$servidor ="servidor";
$username ="root";
$password ="pw";
$database ="db";


$con = mysql_connect($servidor, $username, $password) or die("Problema al conectar con la BD");
mysql_select_db($database, $con)or die("Problema con la BD");

//variables

if (isset($_POST['documento']) && !empty ($_POST['documento']) && isset($_POST['expedicion']) && !empty($_POST['expedicion'])){

$tabla= 'usuarios';
$campo1= 'Documento';
$campo2= 'Expedicion';
$dato1 = $_POST['documento'];
$dato2 = $_POST['expedicion'];

//Agregar datos a una tabla

function addItem($tabla, $campo1, $campo2, $dato1, $dato2) {
$query = "INSERT INTO " . $tabla . " (" . $campo1 . "," . $campo2 . ") VALUES('{$dato1}' , '{$dato2}')";
$result = mysql_query($query);
if($result){
echo '<script type="text/javascript">alert("Usuario registrado correctamente");</script>';
} else{
echo '<script type="text/javascript">alert("Error al registrar el usuario");</script>';
}
}
}
?>

//////////////////////////////////////////////////////////////////////////////////////////////////////////

ajax.js


function Ajax(){
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 guardarUsuario()
{
var guardando = document.getElementById('guardando');
guardando= '<img src="img/loading.gif">';
var documento= encodeURI(document.getElementByName('documento').v alue);
var expedicion = encodeURI(document.getElementByName('expedicion'). value);
var ajax = Ajax();
ajax.open("POST", "funciones.php",true);
ajax.onreadystatechange=guardarUsuarioBD;
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.send("documento="+documento+"&expedicion="+ex pedicion)
}
function guardarUsuarioBD() {
if (ajax.readyState==4){
if (http_request.status == 200) {
guardando.innerHTML= ajax.responseText;
LimpiarCampos();
} }


}

function LimpiarCampos(){
document.getElementsByName("documento").value="";
document.getElementsByName("expedicion").value="";

}