Foros del Web » Programando para Internet » Jquery »

Enviar con JQUERY mediante JSONP variables para procesar en un PHP

Estas en el tema de Enviar con JQUERY mediante JSONP variables para procesar en un PHP en el foro de Jquery en Foros del Web. Hola compañeros! Mi objetivo es crear dos ficheros, cada uno en un dominio distinto. El primer fichero será un JQuery que extraerá unas variables dadas ...
  #1 (permalink)  
Antiguo 28/06/2012, 06:24
 
Fecha de Ingreso: mayo-2012
Ubicación: ISLAS CANARIAS
Mensajes: 5
Antigüedad: 12 años
Puntos: 0
Pregunta Enviar con JQUERY mediante JSONP variables para procesar en un PHP

Hola compañeros!

Mi objetivo es crear dos ficheros, cada uno en un dominio distinto. El primer fichero será un JQuery que extraerá unas variables dadas y posteriormente enviará a un fichero PHP, ubicado en otro dominio, el cual deberá agregar dichas variables en una base de datos.

El fichero PHP está destinado exclusivamente a agregar esos datos al MySQL, por tanto no devolverá nada.

Problema, he conseguido hacerlo con ambos ficheros en el mismo dominio, pero no logro hacerlo como debo.

¿Alguien me puede ayudar?

Thanks!

Código Javascript:
Ver original
  1. function QJ()
  2. {
  3.     //Variables generales
  4.     var IP        = '192.168.56.3';
  5.     var urlclip   = 'http://'+IP+'/almacenar.db.php';
  6.     var divid     = 'UT'+Math.round(Math.random()*100);
  7.  
  8.     //Obtenemos el texto seleccionado
  9.     x = document.createElement('div');
  10.     x.appendChild(window.getSelection().getRangeAt(0).cloneContents());
  11.  
  12.     //Extraemos todo el Tag HEAD
  13.     var head      = document.getElementsByTagName('head')[0];
  14.  
  15.     //Agregamos fichero JQuery
  16.     var jquery    = document.createElement('script');
  17.     jquery.type   = 'text/javascript';
  18.     jquery.src    = 'http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js';
  19.  
  20.     //Agregamos fichero JQuery-UI (.dialog)
  21.     var jqueryui  = document.createElement('script');
  22.     jqueryui.type = 'text/javascript';
  23.     jqueryui.src  = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js';
  24.  
  25.     head.appendChild(jquery);
  26.     head.appendChild(jqueryui);
  27.  
  28.     jquery.onload = function()
  29.     {
  30.         jqueryui.onload = function()
  31.         {
  32.             $.ajax(
  33.             {
  34.                 url:        urlclip,
  35.                 type:       'post',
  36.                 data:       'q='+escape(x.innerText)+
  37.                             '&u='+location.href+
  38.                             '&t='+escape(document.title)+
  39.                             '&i='+divid,
  40.                 async:      'false',
  41.                 success:function(data)
  42.                 {
  43.                     alert ("enviado");
  44.                 }
  45.             });
  46.         }
  47.     }
  48. }
Código PHP:
Ver original
  1. <?php
  2.     header('Access-Control-Allow-Origin: *');
  3.  
  4.     $DB['host'] = "localhost";
  5.     $DB['user'] = "root";
  6.     $DB['pass'] = "";
  7.     $DB['name'] = "temporal";
  8.  
  9.     $dba = mysql_connect ($DB['host'], $DB['user'], $DB['pass']) or die ("ERROR MYSQL_CONNECT: ". MYSQL_ERROR());
  10.     mysql_select_db($DB['name'],$dba) or die ("ERROR MYSQL_SELECT_DB". MYSQL_ERROR());
  11.  
  12.     $sql = "INSERT INTO JS_Temp VALUES ('".$_POST['i']."','".$_POST['t']."','".$_POST['u']."','".$_POST['q']."');";
  13.     mysql_query ($sql,$dba) or die ("ERROR MYSQL_QUERY<br>$sql<br>".MYSQL_ERROR());
  14.  
  15.     mysql_close ($dba) or die ("ERROR MYSQL_CLOSE: ".MYSQL_ERROR());
  16. ?>
  #2 (permalink)  
Antiguo 28/06/2012, 07:45
 
Fecha de Ingreso: mayo-2012
Ubicación: ISLAS CANARIAS
Mensajes: 5
Antigüedad: 12 años
Puntos: 0
Enviar con JQUERY mediante JSONP variables para procesar en un PHP

En Java script, existe un algoritmo que crea un formulario dinámico e invisible y lo envía por post automáticamente. Mi objetivo es lograr hacer lo mismo, pero en jquery.

Este es el algoritmo:
Código Javascript:
Ver original
  1. var windows=window.open('','"+"','width=640,height=480');
  2.     var form=windows.document.createElement('form');
  3.     form.setAttribute('method','post');
  4.     form.setAttribute('action','http://www.dominio.com/test_post.php');
  5.     for(var k in vars)
  6.     {
  7.         var input = document.createElement('input');
  8.         input.setAttribute('type','hidden');
  9.         input.setAttribute('name',k);
  10.         input.setAttribute('value',vars[k]);
  11.         form.appendChild(input);
  12.     }
  13.     windows.document.body.appendChild(form);
  14.     form.submit();
  #3 (permalink)  
Antiguo 28/06/2012, 09:51
 
Fecha de Ingreso: mayo-2012
Ubicación: ISLAS CANARIAS
Mensajes: 5
Antigüedad: 12 años
Puntos: 0
Respuesta: Enviar con JQUERY mediante JSONP variables para procesar en un PHP

Al final no he logrado el hito marcado empleando integramente JQuery, dado que la put-ada del crossdomain es muy gorda.

Por tanto he empleado una mezcla de mi primer source con el nuevo.

Código Javascript:
Ver original
  1. function QJ()
  2. {
  3.     //Variables generales
  4.     var IP        = '192.168.56.3';
  5.     var urlclip   = 'http://'+IP+'/almacenar.db.php';
  6.     var divid     = 'UT'+Math.round(Math.random()*1000000);
  7.  
  8.     //Obtenemos el texto seleccionado
  9.     x = document.createElement('div');
  10.     x.appendChild(window.getSelection().getRangeAt(0).cloneContents());
  11.  
  12.     //Extraemos todo el Tag HEAD
  13.     var head      = document.getElementsByTagName('head')[0];
  14.  
  15.     //Agregamos fichero JQuery
  16.     var jquery    = document.createElement('script');
  17.     jquery.type   = 'text/javascript';
  18.     jquery.src    = 'http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js';
  19.  
  20.     //Agregamos fichero JQuery-UI (.dialog)
  21.     var jqueryui  = document.createElement('script');
  22.     jqueryui.type = 'text/javascript';
  23.     jqueryui.src  = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js';
  24.  
  25.     head.appendChild(jquery);
  26.     head.appendChild(jqueryui);
  27.  
  28.     jquery.onload = function()
  29.     {
  30.         jqueryui.onload = function()
  31.         {
  32.             var ptu=function(vars)
  33.             {
  34.  
  35.                 var div = document.createElement('div');
  36.  
  37.                 div.id            = divid+'FATHER';
  38.                 div.innerHTML     = '<iframe></iframe>';
  39.  
  40.                 document.body.appendChild(div);
  41.  
  42.                 var ifram = div.firstChild;
  43.                 var iframe = ifram.contentDocument || ifram.contentWindow.document;
  44.                 iframe.open();
  45.                 iframe.close();
  46.  
  47.                 var form = iframe.createElement('form');
  48.                 iframe.body.appendChild(form);
  49.  
  50.                 form.setAttribute('method','post');
  51.                 form.setAttribute('action',urlclip);
  52.  
  53.                 for(var k in vars)
  54.                 {
  55.                     var input = iframe.createElement('input');
  56.                     input.setAttribute('type','hidden');
  57.                     input.setAttribute('name',k);
  58.                     input.setAttribute('value',vars[k]);
  59.                     form.appendChild(input);
  60.                 }
  61.                 form.submit();
  62.             };
  63.  
  64.             ptu(
  65.             {
  66.                 a:'YES',
  67.                 q:escape(x.innerText),
  68.                 u:location.href,
  69.                 t:escape(document.title),
  70.                 i:divid
  71.             });
  72.         }
  73.     }
  74. }

Ahora voy a intentar adjuntar a este fichero un .DIALOG que abra un iframe 100% y este llame a un fichero externo.

Etiquetas: crossdomain, php, post
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 04:45.