2012-07-26 70 views
0

我不能通过这个函数发送变量到ajax的php变量,但是我可以通过一个简单的帖子直接把它们发送到php并将它们插入到mysql中。用ajax发送变量到php

我已经用php测试了它,并且典型地向php文件提交了表单动作,并且工作正常。我不知道我错过了什么。提前致谢。此致敬礼

<form class="largeform" id="editform" name="editform" accept-charset="utf-8"  action="" onsubmit="enviarDatosEmpleado(); return false"> 

有什么想法吗? // JavaScript的文档 功能objetoAjax(){

var xmlhttp=false; 

try { 


xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 

} catch (e) { 

try { 

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 

} catch (E) { 

xmlhttp = false; 

} 


} 
if (!xmlhttp && typeof XMLHttpRequest!='undefined') { 

xmlhttp = new XMLHttpRequest(); 

} 

    return xmlhttp; 

    } 
    function enviarDatos(){ 

//donde se mostrará lo resultados 


    divresultado = document.getElementById('resultado'); 

    //valores de los inputs 

    post_title=document.editform.post_title.value; 

    post_metad=document.editform.post_metad.value; 

    post_metak=document.editform.post_metak.value; 

    post_special=document.editform.post_special.value; 

    post_content=document.editform.post_content.value; 

    post_private=document.editform.post_private.value; 

    post_parent=document.editform.post_parent.value; 

    post_template=document.editform.post_template.value; 

    post_id=document.editform.post_id.value; 

    post_menu=document.editform.post_menu.value; 

    post_menu_order=document.editform.post_menu_order.value; 




    //instanciamos el objetoAjax 

    ajax=objetoAjax(); 

    //uso del medotod POST 

    //archivo que realizará la operacion 

    //registro.php 


    ajax.open("POST", "insert.php",true); 

    ajax.onreadystatechange=function() { 

    if (ajax.readyState==4) { 

    //mostrar resultados en esta capa 

    divresultado.innerHTML = ajax.responseText 
    divresultado.innerHTML = "ok"; 
    divresultado.style.display="block"; 


    //llamar a funcion para limpiar los inputs 

    LimpiarCampos(); 

    } 

    } 

    ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 


    //enviando los valores 

    ajax.send     ("post_title="+post_title+"&post_metad="+post_metad+"&post_metak="+post_metak+"&post_special="+post_special+"&post_content="+post_content+"&post_private="+post_private+"&post_parent="+post_parent+"&post_template="+post_template+"&post_id="+post_id+"&post_menu="+post_menu+"&post_menu_order="+post_menu_order) 
// ajax.send("nombres="+nom+"&departamento="+dep+"&sueldo="+suel) 

    } 
+0

不够jQuery – 2012-07-26 16:32:11

+1

使用jQuery或mootools或Prototype。你绝对需要他们。 – FatalError 2012-07-26 16:39:12

+0

@webarto需要moar jquery – Lusitanian 2012-07-26 16:39:26

回答

0

尝试使用jQuery:

$.post("insert.php", { 
    var: value, 
    var2: value2 
}, function(responseText) { 
    $("#resultado").html(responseText); 
}); 

确保您包括jQuery库。仅供参考,您无需提前建立连接。

0

您没有正确引用表单的DOM元素。

你需要做的是这样的:

document.getElementById('idOfFormElement').value 

另外,作为一个评论中提到,你应该熟悉像jQuery框架,这使得使用Ajax的工作非常非常容易(及以上交浏览器兼容)