Foros del Web » Programando para Internet » Javascript » Frameworks JS »

Envio de datos de formulario a PHP

Estas en el tema de Envio de datos de formulario a PHP en el foro de Frameworks JS en Foros del Web. Buenos dias, tengo un problema con el envio de datos de un formulario con javascript a PHP e insertar en una BD MySql. Explico... Mi ...
  #1 (permalink)  
Antiguo 28/08/2014, 05:36
 
Fecha de Ingreso: febrero-2014
Ubicación: Madrid
Mensajes: 19
Antigüedad: 10 años, 2 meses
Puntos: 0
Envio de datos de formulario a PHP

Buenos dias, tengo un problema con el envio de datos de un formulario con javascript a PHP e insertar en una BD MySql. Explico...

Mi formulario
Código HTML:
Ver original
  1. <head><script type="text/javascript" src="js/locs.js"></script></head>
  2. <div id="page_add_location" data-role="page" data-title="Page Save Location">
  3.             <div class="content-padded">
  4.                 <form method="post" id="submit-form">
  5.                     <input type="hidden" name="location_id" id="location_id">
  6.                     <input type="text" id="form-title" name="form-title" placeholder="Título">
  7.                     <textarea rows="5" id="form-desc" name="form-desc" placeholder="Descripción"></textarea>
  8.                     <div id="mi-lat"></div>
  9.                     <div id="mi-lng"></div>
  10.                     <div id="mi-adr"></div>
  11.                     <div id="mi-dat"></div>
  12.                     <a id="form-add" class="button-positive button-block" href="#page_save_location">Añadir</a>
  13.                     <span id="show-location-address" class="count">No se ha detectado la ubicación</span>
  14.                    
  15.                 </form>
  16.             </div>
  17.         </div>

Al pulsar <a id="form-add" class="button-positive button-block" href="#page_save_location">Añadir</a> llamando a una función de otro script apps.js
Código Javascript:
Ver original
  1. $("#form-add").bind("click",function(){    
  2.     addMyLocation();
  3.     envioForm();
  4. });
Concretamente la función que no me funciona es envioForm(), que se encuentra en locs.js

Código Javascript:
Ver original
  1. var urlRequest = "http://myserver.net/m/admin/";
  2. var urlService  = urlRequest+"index.php/";
  3.     function addMyLocation(){
  4.         var client = JSON.stringify({
  5.             Title : $("#form-title").val(),
  6.             Desc : $("#form-desc").val(),
  7.             Lon : myLong,
  8.             Lat : myLat,
  9.             Location : address,
  10.             Dates : now
  11.         });
  12.         var mititle = $("#form-title").val();
  13.         var midesc = $("#form-desc").val();
  14.         var milat = myLat;
  15.         var milng = myLong;
  16.         var miadr = address;
  17.         //var midat = calendarDate+" "+clockTime;
  18.         var calendarDate = getCalendarDate();
  19.         var clockTime = getClockTime();
  20.         var midat = calendarDate+" "+clockTime;
  21.         //var urlUbicacion = urlService+'service/insert?id=1&form-title='+mititle+'&form-desc='+midesc+'&mi-lat='+milat+'&mi-lng='+milng+'&mi-adr='+miadr+'&mi-dat='+midat;
  22.         tbSaveLocation.push(client);
  23.         localStorage.setItem("tbSaveLocation", JSON.stringify(tbSaveLocation));
  24.         alert("Se han guardado los datos. -->id=&mi-lat="+mititle+"&mi-dat="+midesc+"&lat="+milat+"&lng="+milng+"&adr="+miadr+" "+midat);
  25.         return true;
  26.     }
  27.  
  28.     function envioForm(){
  29.  $("#form-add").click(function(){
  30.  var url = urlService+'ubicacion_list/insert'; // El script a dónde se realizará la petición.
  31.     $.ajax({
  32.            type: "POST",
  33.            url: url,
  34.            data: $("#submit-form").serialize() // Adjuntar los campos del formulario enviado.
  35.          });
  36.  
  37.     return false; // Evitar ejecutar el submit del formulario.
  38.  });
  39.  
  40.     }

El archivo ubicacion_list.php es este
Código PHP:
Ver original
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Ubicacion_list extends CI_Controller
  4. {
  5.  
  6.     public function index()
  7.     {  
  8.  
  9.        
  10.         $this->load->view('locations');
  11.     }
  12.  
  13.     public function insert()
  14.     {  
  15.         $data_id = $this->input->post('location_id');
  16.  
  17.         $data = array();
  18.         $data['location_title']  = $this->input->post('form-title');
  19.         $data['location_desc']  = $this->input->post('form-desc');
  20.         $data['location_lat']  = $this->input->post('mi-lat');
  21.         $data['location_lng']  = $this->input->post('mi-lng');
  22.         $data['location_address']  = $this->input->post('mi-adr');
  23.         $data['location_date']  = $this->input->post('mi-dat');
  24.        
  25.         $this->load->model('ubicacion_list_model');
  26.         $result = $this->ubicacion_list_model->insert($data,$data_id);
  27.        
  28.         /*if(!$data_id)
  29.             if($result)
  30.             if($result)
  31.                 echo "Datos insertados correctamente!";
  32.             else
  33.                 echo "No se ha podido insertar los datos!";
  34.         else
  35.             if($result)
  36.                 echo "Datos actualizados correctamente!";
  37.             else
  38.                 echo "Datos actualizados correctamente!";*/
  39.     }
  40.  
  41.     public function remove()
  42.     {
  43.         $data_id = $this->input->post('remove_ubicacion_list_id');
  44.        
  45.         $this->load->model('ubicacion_list_model');
  46.         $result = $this->ubicacion_list_model->remove($data_id);
  47.        
  48.         if($result)
  49.             echo "Datos actualizados correctamente!";
  50.         else
  51.             echo "Datos actualizados correctamente!";
  52.        
  53.     }
  54.            
  55.    
  56. }

Y el archivo ubicacion_list_model.php
Código PHP:
Ver original
  1. <?php if ( ! defined('BASEPATH')) exit('No esta permitido acceder directamente al script');
  2.  
  3. class Ubicacion_list_model extends CI_Model
  4. {
  5.     function __construct()
  6.     {
  7.         parent::__construct();
  8.     }  
  9.    
  10.     /*
  11.     Action insert or update
  12.     */
  13.     function insert($data,$data_id)
  14.     {
  15.         if ($data_id == '')
  16.         {
  17.             $result = $this->db->insert('locations',$data);
  18.            
  19.             return $result;
  20.            
  21.         }else{
  22.        
  23.             $this->db->where('location_id', $data_id);
  24.             $result = $this->db->update('locations',$data);
  25.        
  26.             return $result;
  27.            
  28.             }  
  29.         }
  30.        
  31.    
  32.     /*
  33.     Remove
  34.     */
  35.     function remove($data_id)
  36.     {
  37.        
  38.         return $this->db->delete('locations', array('location_id' => $data_id));
  39.                
  40.     }
  41.    
  42.    
  43.    
  44. }

Me estoy rompiendo la cabeza pero no hay manera de que funcione.
¿Podeis ayudarme?

Última edición por tdcp; 28/08/2014 a las 09:08 Razón: Mensaje cortado

Etiquetas: formulario, funcion, input, javascript, js, php
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 16:56.