Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/03/2009, 12:52
V-P
 
Fecha de Ingreso: noviembre-2008
Mensajes: 21
Antigüedad: 16 años, 2 meses
Puntos: 0
Ayuda a los gurus de javascript

Bueno tenia la necesidad de enviar datos por método POST sin tener que usar un formulario, y buscando en Internet encontré un script que lo hacia y me sirvió de mucho pero ahora tengo la necesidad de enviar los datos pulsando un botón y no mediante un enlace como lo hace actualmente

Aquí esta el código

Código HTML:
<html> 
<head> 
<title>Enviando información por post</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 
 
<body> 
<script type="text/javascript" language="javascript"> 
	
	var form = document.createElement("form"); // crear un form
	with(form) {
		setAttribute("name", "myform"); //nombre del form
		setAttribute("action", ""); // action por defecto
		setAttribute("method", "post"); // method POST
	}
	
	
	var input = document.createElement("input"); // Crea un elemento input
	with(input) {
		setAttribute("name", "theInput"); //nombre del input
		setAttribute("type", "hidden"); // tipo hidden
		setAttribute("value", ""); // valor por defecto
	}
	form.appendChild(input); // añade el input al formulario
	
 
	document.getElementsByTagName("body")[0].appendChild(form); // añade el formulario al documento
	
window.onload=function(){
var my_links = document.getElementsByTagName("a");
for (var a = 0; a < my_links.length; a++) {
if (my_links[a].name=="post") my_links[a].onclick = function() {
	document.myform.action=this.href;
	document.myform.theInput.value=this.title;
	document.myform.submit();
	return false;}
}
 
}
 
</script> 
<h3>Enviando información por post</h3> 
<a href="ejemplo_post.php"  name="post" title="Este valor se enviará por post" >Pulsa aquí </a> para hacer POST<br> 
También puedes <a href="ejemplo_post.php"  name="post" title="Este valor también se envía por post" >pulsar aquí</a> para hacer otro POST<br> 
Esto es un<a href="ejemplo_post.php" > link normal</a> 
</body> 
 
</html> 
hago cambios para que me envíe pulsando un botón pero no me funciona que es lo que debo cambiar para hacerlo.

Se que en esta linea

Código:
document.getElementsByTagName("a");
toma como objeto el dato del enlace