Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] insertar checkbox secundarios en mysql con php

Estas en el tema de insertar checkbox secundarios en mysql con php en el foro de PHP en Foros del Web. hola buen dia Estoy realizando un formulario de alta de cliente, en el cual tengo varios text y para insertar en mysql utilizo la funcion ...
  #1 (permalink)  
Antiguo 11/09/2014, 09:10
 
Fecha de Ingreso: febrero-2013
Mensajes: 2
Antigüedad: 11 años, 2 meses
Puntos: 0
Exclamación insertar checkbox secundarios en mysql con php

hola buen dia

Estoy realizando un formulario de alta de cliente, en el cual tengo varios text y para insertar en mysql utilizo la funcion GetSQLValueString, en donde tengo un problema es con los checkbox principales pues los tengo con un div para aparescan cada ves que esta selecionados aparecen los secundarios. los checkbox si los recibe el array pero al insertarlos en la base de datos solo toma los principales.

nota: los checkbox principales son texto y los secundarios son numeros.

a continuacion les dejo mi codigo saludos!!!

Código PHP:
Ver original
  1. <?php
  2.        
  3.         error_reporting(E_ALL^E_NOTICE);
  4.         require_once('Connections/con_imag.php');  
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9.     <meta charset="utf-8">
  10.     <title>Crear Clientes</title>
  11.    
  12. </head>
  13. <body data-spy="scroll" data-target=".bs-docs-sidebar">
  14.  <script type="text/javascript">
  15.         function toggle(elemento)
  16.         {
  17.           if(elemento.value=="menor")
  18.           {
  19.               document.getElementById("menor").style.display = "block";
  20.               document.getElementById("intermedio").style.display = "none";
  21.               document.getElementById("mayor").style.display = "none";
  22.               document.getElementById("otro").style.display = "none";
  23.            }else
  24.            {
  25.                if(elemento.value=="intermedio")
  26.                {
  27.                    document.getElementById("menor").style.display = "none";
  28.                    document.getElementById("intermedio").style.display = "block";
  29.                    document.getElementById("mayor").style.display = "none";
  30.                    document.getElementById("otro").style.display = "none";             
  31.                }else
  32.                {
  33.                    if(elemento.value=="mayor")
  34.                    {
  35.                         document.getElementById("menor").style.display = "none";
  36.                         document.getElementById("intermedio").style.display = "none";
  37.                         document.getElementById("mayor").style.display = "block";
  38.                         document.getElementById("otro").style.display = "none";
  39.                     }else
  40.                     {
  41.                         if(elemento.value=="otro")
  42.                         {  
  43.                         document.getElementById("menor").style.display = "none";
  44.                         document.getElementById("intermedio").style.display = "none";
  45.                         document.getElementById("mayor").style.display = "none";
  46.                         document.getElementById("otro").style.display = "block";
  47.                         }  
  48.                     }                  
  49.                 }
  50.             }
  51.         }
  52. </script>
  53.   <?php
  54.     if (!function_exists("GetSQLValueString")) {
  55. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  56. {
  57.   if (PHP_VERSION < 6) {
  58.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  59.   }
  60.  
  61.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  62.  
  63.   switch ($theType) {
  64.     case "text":
  65.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  66.       break;    
  67.     case "long":
  68.     case "int":
  69.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  70.       break;
  71.     case "double":
  72.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  73.       break;
  74.     case "date":
  75.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  76.       break;
  77.     case "defined":
  78.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  79.       break;
  80.   }
  81.   return $theValue;
  82. }
  83. }
  84.  
  85. $editFormAction = $_SERVER['PHP_SELF'];
  86. if (isset($_SERVER['QUERY_STRING'])) {
  87.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  88. }
  89.  
  90. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"))
  91. {
  92.    
  93. if ($_SERVER["REQUEST_METHOD"] == "POST") {  
  94.     $tema=$_POST["tema"];
  95.    
  96.           for ($i = 0; $i <count($tema); $i++)
  97.         {
  98.             for($j = 0; $j <count($tema[$i]); $j++)
  99.                 {
  100.                     $tema[$i][$j].'<br/>';
  101.                     $modaser = $tema[$i][$j];
  102.                     echo $modaser;
  103.                 }
  104.         }
  105.            
  106.             $moa=   $tema[0][0] ;//menor 200
  107.             $ms=    $tema[0][1]  ;//menor 300
  108.             $msr=   $tema[0][2] ;//menor 500
  109.            
  110.             $moda=  $tema[1][0] ;//intermedio 1200
  111.             $mosr=  $tema[1][1] ;//intermedio 1000
  112.            
  113.             $mods=  $tema[2][0] ;//mayor 2400
  114.             $moser= $tema[2][1] ;//mayor 2000
  115.                    
  116.             $mdri=  $tema[3][0];//otro
  117.        
  118.  
  119.             $mdser= "$moa $ms $msr"; //menor 200  300  500
  120.             $mdr= "$moda $mosr"; //intermedio 1200  1000
  121.             $mdsr= "$mods $moser"; //mayor 2400 2000
  122.            
  123.            
  124.             $modser="$mdser $mdr $mdsr $mdri";// todos
  125.             echo $modser;          
  126. }
  127.  
  128.   $insertSQL = sprintf("INSERT INTO clientes (Nom_emp, Nom_contac, Dir, Telefono, Email, Descripcion, mod_serv)
  129.  VALUES (%s, %s, %s, %s, %s, %s, '$modaser')",
  130.                        GetSQLValueString($_POST['txtNom_emp'], "text"),
  131.                        GetSQLValueString($_POST['txtNom_contac'], "text"),
  132.                        GetSQLValueString($_POST['txtDir'], "text"),
  133.                        GetSQLValueString($_POST['txtTel'], "int"),
  134.                        GetSQLValueString($_POST['txtMail'], "text"),
  135.                        GetSQLValueString($_POST['txtDescripcion'], "text"),
  136.                        GetSQLValueString($_POST[$tema[$i][$j]], "text")      
  137.                        );
  138.  
  139.    mysql_select_db($database_con_imag, $con_imag);
  140.   $Result1 = mysql_query($insertSQL, $con_imag) or die(mysql_error());
  141.         if ($Result1 == true)
  142.         {
  143.             echo "Registro Guardado existosamente!!!";
  144.         }
  145.         else
  146.         {
  147.             echo "No se puede realizar el registro!!!";
  148.         }
  149.  
  150. }
  151. ?>
  152.  
  153. <div class="control-group info">
  154.   <h1>Alta de Clientes</h1>
  155. <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1" id="form1">
  156.   <table width="50%" border="1" class="table">
  157.     <tr class="info">
  158.       <td colspan="2"><center><strong>Nombre de la empresa:</strong></center></td>
  159.       <td><label for="txtNom_emp"></label>
  160.       <input type="text" name="txtNom_emp" id="txtNom_emp" /></td>
  161.     </tr>
  162.     <tr>
  163.       <td colspan="2"><center><strong>Nombre del contacto:</strong></center></td>
  164.       <td><label for="txtNom_contac"></label>
  165.       <input type="text" name="txtNom_contac" id="txtNom_contac" /></td>
  166.     </tr>
  167.     <tr>
  168.       <td colspan="2"><center><strong>Direccion:</strong></center></td>
  169.       <td><label for="txtDir"></label>
  170.       <input type="text" name="txtDir" id="txtDir" /></td>
  171.     </tr>
  172.     <tr>
  173.       <td colspan="2" width="152"><center><strong>Telefono:</strong></center></td>
  174.       <td><label for="txtTel"></label>
  175.       <input type="text" name="txtTel" id="txtTel" /></td>
  176.     </tr>
  177.     <tr>
  178.       <td colspan="2" width="152"><center><strong>E-mail:</strong></center></td>
  179.       <td><label for="txtMail"></label>
  180.       <input type="text" name="txtMail" id="txtMail" /></td>
  181.     </tr>
  182.     <tr>
  183.       <td colspan="2" width="152"><center><strong>Descripción:</strong></center></td>
  184.       <td><label for="txtDescripcion"></label>
  185.       <textarea name="txtDescripcion" id="txtDescripcion" cols="45" rows="2"></textarea></td>
  186.     </tr>
  187. </tr>
  188.   </table>
  189.  
  190.     <dl>
  191.         <dt><label>Modalidad:</label></dt>
  192.         <dd>
  193.             <input type="checkbox" name="tema[][]" onclick="toggle(this)" value="menor" >Servicio Menor
  194.             <input type="checkbox" name="tema[][]" onclick="toggle(this)" value="intermedio">Servicio Intermedio
  195.             <input type="checkbox" name="tema[][]" onclick="toggle(this)" value="mayor">Servicio Mayor
  196.              <input type="checkbox" name="tema[][]" onclick="toggle(this)" value="otro">Otro Servicio
  197.  
  198.         <div id="menor" style="display:none">
  199.         <p>
  200.             <input type="checkbox" name="tema[][]" onclick="toggle(this)" value="200"> 200 hr
  201.             <input type="checkbox" name="tema[][]" onclick="toggle(this)" value="300">300 hr
  202.             <input type="checkbox" name="tema[][]" onclick="toggle(this)" value="500">500 hr
  203.             </p>
  204.         </div>
  205.          
  206.         <div id="intermedio" style="display:none">
  207.         <p>
  208.             <input type="checkbox" name="tema[][]" onclick="toggle(this)" value="1200"> 1200 hr
  209.             <input type="checkbox" name="tema[][]" onclick="toggle(this)" value="1000">1000 hr
  210.         </p>
  211.         </div>
  212.        
  213.         <div id="mayor" style="display:none">
  214.         <p>
  215.             <input type="checkbox" name="tema[][]" onclick="toggle(this)" value="2400"> 2400 hr
  216.             <input type="checkbox" name="tema[][]" onclick="toggle(this)" value="2000">2000 hr
  217.         </p>
  218.         </div>
  219.         <div id="otro" style="display:none">
  220.         <p>
  221.             <input name="tema[][]"  type="text" value="" style="width:80px;" /><br />
  222.             Ingrese modalidad
  223.         </p>
  224.         </div>
  225.        </dd>
  226.     </dl>
  227.   <p>
  228.     <input type="submit" name="button"  id="button" value="Enviar" />
  229.   </p>
  230.   <input type="hidden" name="MM_insert" value="form1" />
  231. </form>
  232. <form id="Productos_menu" name="Productos_menu" action="Productos_menu.php" method="POST" >
  233. <table   BORDER=1 CELLSPACING=0 CELLPADDING=0 BACKGROUND='white'>
  234. <tr>
  235. <input type='submit' name='regresar' id='regresar' value='Regresar' />
  236. </tr>
  237. </table>
  238.  </form>
  239. </html>

Espero que puedan ayudarme saludos!!!

Última edición por Triby; 12/09/2014 a las 18:25 Razón: Código en highlight

Etiquetas: arraylist, formulariosenphp, php+bd+mysql
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 05:42.