Foros del Web » Programando para Internet » PHP »

Enviar formulario con fecha igual o inferior a la del servidor

Estas en el tema de Enviar formulario con fecha igual o inferior a la del servidor en el foro de PHP en Foros del Web. Hola gente, a ver si me podéis echar una mano. Os adelanto que soy muy novato y que mi web la estoy haciendo con DW, ...
  #1 (permalink)  
Antiguo 10/05/2010, 05:06
Avatar de elratonperez  
Fecha de Ingreso: octubre-2004
Ubicación: La Ciudad Imperial
Mensajes: 332
Antigüedad: 19 años, 6 meses
Puntos: 0
Enviar formulario con fecha igual o inferior a la del servidor

Hola gente, a ver si me podéis echar una mano. Os adelanto que soy muy novato y que mi web la estoy haciendo con DW, pero ésto creo que lo tendré que hacer "a mano".

Tengo un formulario en PHP y necesito validar la fecha para que no pueda ser enviado a partir de cierto día a una hora determinada. Supongo que tengo que comparar la fecha y hora del servidor, con la fecha y hora límite, pero no sé cómo hacerlo.

¿Me podéis orientar un poco? Gracias por adelantado.

¡¡Saludos!!
  #2 (permalink)  
Antiguo 10/05/2010, 05:15
Avatar de Adler
Colaborador
 
Fecha de Ingreso: diciembre-2006
Mensajes: 4.671
Antigüedad: 17 años, 4 meses
Puntos: 126
Respuesta: Enviar formulario con fecha igual o inferior a la del servidor

Hola

Sería algo así

Código PHP:
Ver original
  1. $fecha_actual = strtotime(date("d-m-Y H:i:00",time()));  
  2. $fecha_limite = strtotime("11-5-2010 21:00:00");  
  3. if($fecha_actual > $fecha_limite){  
  4. echo "Limite pasado";  
  5. } else {
  6. ....
  7. }

Suerte
__________________
Los formularios se envían/validan con un botón Submit
<input type="submit" value="Enviar" style="background-color:#0B5795; font:bold 10px verdana; color:#FFF;" />
  #3 (permalink)  
Antiguo 11/05/2010, 15:55
Avatar de elratonperez  
Fecha de Ingreso: octubre-2004
Ubicación: La Ciudad Imperial
Mensajes: 332
Antigüedad: 19 años, 6 meses
Puntos: 0
Pregunta Respuesta: Enviar formulario con fecha igual o inferior a la del servidor

Hola Adler, gracias por contestar.

He intentado probar el código que me has indicado, pero no doy con ello. Perdona mi ignorancia, pero realmente es que no sé dónde ponerlo exactamente para que funcione. Bueno, en realidad sí que he conseguido que no se guarden los datos del formulario si se sobrepasa la fecha límite, pero el mensaje de "Límite pasado" se carga desde el inicio con el resto de la página... vamos, una chapuza que no vale.

Pongo el código de un formulario de ejemplo por si alguien me puede echar una mano. Es muy simple: nombre, apellidos y el botón Enviar. Lo he realizado con Dreamweaver.

Código PHP:
Ver original
  1. <?php require_once('Connections/basedatos.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6.   if (PHP_VERSION < 6) {
  7.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8.   }
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. $editFormAction = $_SERVER['PHP_SELF'];
  35. if (isset($_SERVER['QUERY_STRING'])) {
  36.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  37. }
  38.  
  39. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formulario1")) {
  40.   $insertSQL = sprintf("INSERT INTO bdprueba (nombre, apellido1, apellido2) VALUES (%s, %s, %s)",
  41.                        GetSQLValueString($_POST['nombre'], "text"),
  42.                        GetSQLValueString($_POST['apellido1'], "text"),
  43.                        GetSQLValueString($_POST['apellido2'], "text"));
  44.  
  45.   mysql_select_db($database_basedatos, $basedatos);
  46.   $Result1 = mysql_query($insertSQL, $basedatos) or die(mysql_error());
  47.  
  48.   $insertGoTo = "inicio.php";
  49.   if (isset($_SERVER['QUERY_STRING'])) {
  50.     $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
  51.     $insertGoTo .= $_SERVER['QUERY_STRING'];
  52.   }
  53.   header(sprintf("Location: %s", $insertGoTo));
  54. }
  55. ?>
  56. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  57. <html xmlns="http://www.w3.org/1999/xhtml">
  58. <head>
  59. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  60. <title>prueba2</title>
  61. </head>
  62.  
  63. <body>
  64. <form id="formulario1" name="formulario1" method="POST" action="<?php echo $editFormAction; ?>">
  65.   <p>
  66.     <label>Nombre
  67.       <input type="text" name="nombre" id="nombre" />
  68.     </label>
  69.   </p>
  70.   <p>
  71.     <label>Apellido 1
  72.       <input type="text" name="apellido1" id="apellido1" />
  73.     </label>
  74.   </p>
  75.   <p>
  76.     <label>Apellido 2
  77.       <input type="text" name="apellido2" id="apellido2" />
  78.     </label>
  79.   </p>
  80.   <p>
  81.     <label>
  82.       <input type="submit" name="enviar" id="enviar" value="Enviar" />
  83.     </label>
  84.   </p>
  85.   <input type="hidden" name="MM_insert" value="formulario1" />
  86. </form>
  87. </body>
  88. </html>

Saludos.
  #4 (permalink)  
Antiguo 12/05/2010, 06:42
Avatar de Adler
Colaborador
 
Fecha de Ingreso: diciembre-2006
Mensajes: 4.671
Antigüedad: 17 años, 4 meses
Puntos: 126
Respuesta: Enviar formulario con fecha igual o inferior a la del servidor

Hola

Prueba de esta manera

Cita:
<?php require_once('Connections/basedatos.php'); ?>
<?php
$fecha_actual = strtotime(date("d-m-Y H:i:00",time()));
$fecha_limite = strtotime("11-5-2010 21:00:00");


if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formulario1")) {

if($fecha_actual > $fecha_limite){
echo "Limite pasado";
} else {


$insertSQL = sprintf("INSERT INTO bdprueba (nombre, apellido1, apellido2) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['nombre'], "text"),
GetSQLValueString($_POST['apellido1'], "text"),
GetSQLValueString($_POST['apellido2'], "text"));

mysql_select_db($database_basedatos, $basedatos);
$Result1 = mysql_query($insertSQL, $basedatos) or die(mysql_error());

$insertGoTo = "inicio.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
}
?>
Suerte
__________________
Los formularios se envían/validan con un botón Submit
<input type="submit" value="Enviar" style="background-color:#0B5795; font:bold 10px verdana; color:#FFF;" />
  #5 (permalink)  
Antiguo 13/05/2010, 14:34
Avatar de elratonperez  
Fecha de Ingreso: octubre-2004
Ubicación: La Ciudad Imperial
Mensajes: 332
Antigüedad: 19 años, 6 meses
Puntos: 0
Respuesta: Enviar formulario con fecha igual o inferior a la del servidor

Muchas gracias por tu ayuda, ¡¡lo he conseguido!! Aunque no lo creas, me ha costado un montón por un detalle de lo más tonto: la fecha la ponía tal y como me indicabas (11-5-2010) y resulta que tiene que ser de esta forma 11-05-2010. Mira que he hecho pruebas... hasta que me he dado cuenta.

Gracias de nuevo y un saludo.

Etiquetas: enviar, fechas, servidores, formulario
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:43.