Formulario Php llamado
reporte_usuarios.php
Código PHP:
<html>
<head>
<style type="text/css">
<!--
body,td,th {
color: #FFFFFF;
}
body {
background-color: #3cc35d;
background-image: url(Fondo.jpg);
background-repeat: repeat-x;
}
div.page
{
writing-mode: tb-rl;
height: 10%;
margin: 80% 0%;
}
-->
</style>
<title>Sistema de Inventario</title>
</head>
<body>
<script language="javascript" type="text/javascript">
function formValidator(){
// Make quick references to our fields
var cedula = document.getElementById("cedula");
var nombre = document.getElementById("nombre");
var cargo = document.getElementById("cargo");
// Check each input in the order that it appears in the form!
if(isNumeric(cedula, "Por favor sólo ingrese números para la cédula")){
return true;
}
return false;
}
function notEmpty(elem, helperMsg)
{
if(elem.value.length == 0)
{
alert(helperMsg);
elem.focus(); // Devuelvo al usuario al input
return false;
}
return true;
}
function isNumeric(elem, helperMsg)
{
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}
function isNotNumeric(elem, helperMsg)
{
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression))
{
alert(helperMsg);
elem.focus();
return false;
}
else
{
return true;
}
}
function isAlphabet(elem, helperMsg)
{
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var cedula = document.getElementById('cedula').value;
var nombre = document.getElementById('nombre').value;
var cargo = document.getElementById('cargo').value;
var queryString = "?cedula=" + cedula + "&nombre=" + nombre + "&cargo=" + cargo;
ajaxRequest.open("GET", "reportar_usuario.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
<p>Cédula:
<input name="cedula" type='text' id='cedula' />
</p>
<p>
Nombre:
<input name="nombre" type='text' id='nombre' />
</p>
<p>
Cargo:
<input name="cargo" type='text' id='cargo' />
</p>
<p> </br>
<input type='button' onclick='ajaxFunction()' value='Buscar' />
</p>
</form>
<div id='ajaxDiv'>
<p>Los resultados se mostraran aquí</p>
</div>
</body>
</html>
reportar_usuario.php
Código PHP:
<?php
include "conectarse.php";
// Retrieve data from Query String
$cedula = $_GET['cedula'];
$nombre = $_GET['nombre'];
$cargo = $_GET['cargo'];
// Escape User Input to help prevent SQL Injection
$cedula = mysql_real_escape_string($cedula);
$nombre = mysql_real_escape_string($nombre);
$cargo = mysql_real_escape_string($cargo);
//build query
$query = "SELECT * FROM `responsable`" ;
if(!empty($cedula) and empty($nombre) and empty($cargo))
{
$query .= " WHERE `cedula` = '$cedula' ORDER BY `cedula`";
}
elseif(!empty($nombre) and empty($cedula) and empty($cargo))
{
$query .= " WHERE `nombre` LIKE '%$nombre%' ORDER BY `cedula`";
}
elseif(!empty($cargo) and empty($nombre) and empty($cedula))
{
$query .= " WHERE `cargo` LIKE '%$cargo%' ORDER BY `cedula`";
}
elseif(!empty($cargo) and empty($nombre) and empty($cedula))
{
$query .= " WHERE `cargo` LIKE '%$cargo%' ORDER BY `cedula`";
}
elseif(!empty($cedula) and !empty($nombre) and empty($cargo))
{
$query .= " WHERE `cedula` = '$cedula' AND `nombre` LIKE '%$nombre%' ORDER BY `cedula`";
}
elseif(!empty($cedula) and !empty($cargo) and empty($nombre))
{
$query .= " WHERE `cedula` = '$cedula' AND `cargo` LIKE '%$cargo%' ORDER BY `cedula`";
}
elseif(!empty($nombre) and !empty($cargo) and empty($cedula))
{
$query .= " WHERE `nombre` LIKE '%$nombre%' AND `cargo` LIKE '%$cargo%' ORDER BY `cedula`";
}
elseif(!empty($nombre) and !empty($cargo) and !empty($cedula))
{
$query .= " WHERE `cedula` = '%$cedula%' AND `nombre` LIKE '%$cedula%' AND `cargo` LIKE '%$cargo%' ORDER BY `cedula`";
}
$qry_result = mysql_query($query) or die(mysql_error());
//Build Result String
$display_string = "<table width=100% border=1 cellspacing=1 cellpadding=1>";
$display_string .= "<tr bgcolor=#009900 align=center>";
$display_string .= "<th>CÉDULA</th>";
$display_string .= "<th>NOMBRE</th>";
$display_string .= "<th>CARGO</th>";
$display_string .= "</tr>";
// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
$display_string .= "<tr>";
$display_string .= "<td>$row[cedula]</td>";
$display_string .= "<td>$row[nombre]</td>";
$display_string .= "<td>$row[cargo]</td>";
$display_string .= "</tr>";
}
$display_string .= "</table>";
echo $display_string;
?>