Foros del Web » Programando para Internet » PHP »

usar variables de formulario antes de enviar

Estas en el tema de usar variables de formulario antes de enviar en el foro de PHP en Foros del Web. hola, estoy intentando hacer un formaulario que casi tengo listo @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código PHP: Ver original <?php require_once ( 'Connections/conexxion.php' ) ; ?> <?php if ...
  #1 (permalink)  
Antiguo 26/12/2014, 12:25
 
Fecha de Ingreso: noviembre-2014
Mensajes: 76
Antigüedad: 9 años, 4 meses
Puntos: 0
Busqueda usar variables de formulario antes de enviar

hola, estoy intentando hacer un formaulario que casi tengo listo

Código PHP:
Ver original
  1. <?php require_once('Connections/conexxion.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"] == "form1")) {
  40.   $insertSQL = sprintf("INSERT INTO movimiento (venta, compra, taller, regula_mas, regula_menos, id_lente, id_cil, id_esf) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
  41.                        GetSQLValueString($_POST['venta'], "int"),
  42.                        GetSQLValueString($_POST['compra'], "int"),
  43.                        GetSQLValueString($_POST['taller'], "int"),
  44.                        GetSQLValueString($_POST['regula_mas'], "int"),
  45.                        GetSQLValueString($_POST['regula_menos'], "int"),
  46.                        GetSQLValueString($_POST['id_lente'], "int"),
  47.                        GetSQLValueString($_POST['id_cil'], "int"),
  48.                        GetSQLValueString($_POST['id_esf'], "int"));
  49.  
  50.   mysql_select_db($database_conexxion, $conexxion);
  51.   $Result1 = mysql_query($insertSQL, $conexxion) or die(mysql_error());
  52. }
  53.  
  54. mysql_select_db($database_conexxion, $conexxion);
  55. $query_lente = "select id_lente, lente from lentes";
  56. $lente = mysql_query($query_lente, $conexxion) or die(mysql_error());
  57. $row_lente = mysql_fetch_assoc($lente);
  58. $totalRows_lente = mysql_num_rows($lente);
  59.  
  60. mysql_select_db($database_conexxion, $conexxion);
  61. $query_esfera = "SELECT  * FROM esfera";
  62. $esfera = mysql_query($query_esfera, $conexxion) or die(mysql_error());
  63.  
  64. mysql_select_db($database_conexxion, $conexxion);
  65. $query_cilindro = "SELECT * FROM cilindro";
  66. $cilindro = mysql_query($query_cilindro, $conexxion) or die(mysql_error());
  67.  
  68. $query_item = "select id_item from item
  69. inner join rx on rx.id_rx = item.id_rx
  70. inner join cilindro on cilindro.id_cil = rx.id_cil
  71. inner join esfera on esfera.id_esf = rx.id_esf
  72. where cilindro = ".$_POST['sel_cil']." and esfera = ".$_POST['sel_esf']." and id_lente =". $_POST['sel_lente'];
  73. $idlente = mysql_query($query_lente, $conexxion) or die (mysql_error());
  74.  
  75.  
  76. ?>
  77.  
  78. <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  79.   <table align="center">
  80.     <tr valign="baseline">
  81.       <td nowrap="nowrap" align="right">Venta:</td>
  82.       <td><input type="text" name="venta" value="" size="32" /></td>
  83.     </tr>
  84.     <tr valign="baseline">
  85.       <td nowrap="nowrap" align="right">Compra:</td>
  86.       <td><input type="text" name="compra" value="" size="32" /></td>
  87.     </tr>
  88.     <tr valign="baseline">
  89.       <td nowrap="nowrap" align="right">Taller:</td>
  90.       <td><input type="text" name="taller" value="" size="32" /></td>
  91.     </tr>
  92.     <tr valign="baseline">
  93.       <td nowrap="nowrap" align="right">Regula_mas:</td>
  94.       <td><input type="text" name="regula_mas" value="" size="32" /></td>
  95.     </tr>
  96.     <tr valign="baseline">
  97.       <td nowrap="nowrap" align="right">Regula_menos:</td>
  98.       <td><input type="text" name="regula_menos" value="" size="32" /></td>
  99.     </tr>
  100.     <tr valign="baseline">
  101.       <td nowrap="nowrap" align="right">lente:</td>
  102.       <td><?php echo "<select name='sel_lente'>";
  103. while($row= mysql_fetch_array($lente)){?>
  104. <option value=" <?php echo $row['id_lente'] ;?> " > <?php echo $row['lente']; ?> </option>
  105.     <?php } ?> </select>
  106.     <label for="id_lente"></label></td>
  107.     </tr>
  108.     <tr valign="baseline">
  109.       <td nowrap="nowrap" align="right">cilindro:</td>
  110.       <td><?php echo "<select name='sel_cil'>";
  111. while($row= mysql_fetch_array($cilindro)){?>
  112. <option value=" <?php echo $row['id_cil'] ;?> " > <?php echo $row['cilindro']; ?> </option>
  113.     <?php } ?> </select>
  114.     <label for="id_cil"></label></td>
  115.     </tr>
  116.     <tr valign="baseline">
  117.       <td nowrap="nowrap" align="right">Esfera:</td>
  118.       <td><?php echo "<select name='sel_esf'>";
  119. while($row= mysql_fetch_array($esfera)){?>
  120. <option value=" <?php echo $row['id_esf'] ;?> " > <?php echo $row['esfera']; ?> </option>
  121.     <?php } ?> </select></td>
  122.     </tr>
  123.     <tr valign="baseline">
  124.       <td nowrap="nowrap" align="right">id_lente</td>
  125.       <td><label for="id_lente3"></label>
  126.       <input type="text" name="id_lente" id="id_lente3" /></td>
  127.     </tr>
  128.     <tr valign="baseline">
  129.       <td nowrap="nowrap" align="right">&nbsp;</td>
  130.       <td><input type="submit" value="Insert record" /></td>
  131.     </tr>
  132.   </table>
  133.   <input type="hidden" name="MM_insert" value="form1" />
  134. </form>
  135. <?php
  136.  
  137. mysql_free_result($cilindro);
  138.  
  139. ?>

me encuentro que necesito un dato que el usuario desconocerá pero que puedo extraer de los datos por el usuario seleccionados y que necesito enviar con el formulario a la DB, es el id_lente. He preparado una consulta,

Código PHP:
Ver original
  1. $query_item = "select id_item from item
  2. inner join rx on rx.id_rx = item.id_rx
  3. inner join cilindro on cilindro.id_cil = rx.id_cil
  4. inner join esfera on esfera.id_esf = rx.id_esf
  5. where cilindro = ".$_POST['sel_cil']." and esfera = ".$_POST['sel_esf']." and id_lente =". $_POST['sel_lente'];
  6. $idlente = mysql_query($query_lente, $conexxion) or die (mysql_error());

pero ahora no se como hacer que el formulario recoja, de forma transparente para el usuario, y lo envie.
  #2 (permalink)  
Antiguo 26/12/2014, 14:03
Avatar de enlinea777  
Fecha de Ingreso: mayo-2008
Ubicación: frente al pc
Mensajes: 1.830
Antigüedad: 15 años, 10 meses
Puntos: 127
Respuesta: usar variables de formulario antes de enviar

prueba usar ajax
  #3 (permalink)  
Antiguo 27/12/2014, 03:06
 
Fecha de Ingreso: noviembre-2014
Mensajes: 76
Antigüedad: 9 años, 4 meses
Puntos: 0
Respuesta: usar variables de formulario antes de enviar

Ya me gustaría, pero si mis conocimientos de php y mysql son de aprendiz, con ajax todavía no he empezado.

No hay forma de solucionarlo con php?
  #4 (permalink)  
Antiguo 27/12/2014, 03:15
 
Fecha de Ingreso: julio-2010
Mensajes: 275
Antigüedad: 13 años, 9 meses
Puntos: 21
Respuesta: usar variables de formulario antes de enviar

La solución que yo le veo, es usar sesiones y un botón para buscar el id_del_lente y que se recargue todo el formulario otra vez con los parámetros almacenados en las variables de sesión.
  #5 (permalink)  
Antiguo 27/12/2014, 04:55
 
Fecha de Ingreso: noviembre-2014
Mensajes: 76
Antigüedad: 9 años, 4 meses
Puntos: 0
Respuesta: usar variables de formulario antes de enviar

y enviar las variables a otra página antes que al formulario y ahí usar esas variables para obtener mi id_item (que no id_lente, que este ya lo tengo)?
  #6 (permalink)  
Antiguo 27/12/2014, 11:52
 
Fecha de Ingreso: noviembre-2014
Mensajes: 76
Antigüedad: 9 años, 4 meses
Puntos: 0
Respuesta: usar variables de formulario antes de enviar

de omento lo he solucionado de modo "chapucero" mandando el formulario a una página, allí obteniendo el id_item y desde un formulario con campos hidden pues enviando a la db, pero me gustaría una solución más elegante
  #7 (permalink)  
Antiguo 28/12/2014, 21:42
Avatar de enlinea777  
Fecha de Ingreso: mayo-2008
Ubicación: frente al pc
Mensajes: 1.830
Antigüedad: 15 años, 10 meses
Puntos: 127
Respuesta: usar variables de formulario antes de enviar

mas elegante pues usando ajax
las otras soluciones como dices son chapuceras o soluciones parche
  #8 (permalink)  
Antiguo 29/12/2014, 04:12
 
Fecha de Ingreso: noviembre-2014
Mensajes: 76
Antigüedad: 9 años, 4 meses
Puntos: 0
Respuesta: usar variables de formulario antes de enviar

Muy bien ussndo ajax. Me puedes dar alguna pista de al menos como empezar?
  #9 (permalink)  
Antiguo 29/12/2014, 07:37
Avatar de enlinea777  
Fecha de Ingreso: mayo-2008
Ubicación: frente al pc
Mensajes: 1.830
Antigüedad: 15 años, 10 meses
Puntos: 127
Respuesta: usar variables de formulario antes de enviar

Comprendiendo mejor tu pregunta lo mejor es usar sesiones [recomendado]
o input aocultos para transferir el dato.

Etiquetas: formulario, html, mysql, select, sql, usar, variables
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 10:15.