日期:2014-05-16  浏览次数:20526 次

急急急,ajax提交form表单问题
表单为form1,id为form1,表单所在页面为index.html,提交到PHP页面为index.php,方法为index(),跪求ajax提交form1表单的详细代码。跪求,很急,谢谢了。本人一点不会ajax,尤其是url写具体点,不懂
ajax 表单 php

------解决方案--------------------
不会PHP,没懂你那个方法为INDEX()是什么意思。
写了个函数示例,提交form1.input1的数据到index.php

var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}

function submitForm(){
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
url=encodeURI("index.php?txt="+document.forms.form1.input1.value);
xmlHttp.open("POST",url,true);
xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
alert('提交成功');
}
}

------解决方案--------------------
ajax和普通表单提交一样,如果懒可以导入jquery,有序列化表单和集成了ajax,就直接用jquery提交序列化后的表单数据就行了

$.ajax({url:'index.php',data:$('表单选择器').serialize(),type:'POST',dataType:'html',success:function(data){
alert('提交成功!');
},error:function(xhr){
 alert('动态页出错\n'+xhr.responseText)
}})

------解决方案--------------------
无上传用jquery
有上传用jquery+jquery.form

//无上传
$("#form1").submit(function(){
 $.ajax($(this).attr('action'),$(this).serialize(),function(json){
   //处理代码
 },'json');
 return false;
});
//有上传
 $("#form1").ajaxSubmit({
   dataType:'json',
   success:function(json){
    //处理代码
   }
 });

------解决方案--------------------
引用:
无上传用jquery
有上传用jquery+jquery.form

//无上传
$("#form1").submit(function(){
 $.ajax($(this).attr('action'),$(this).serialize(),function(json){
   //处理代码
 },'json');
 return false;
});
//有上传
 $("#form1").ajaxSubmit({
   dataType:'json',
   success:function(json){
    //处理代码
   }
 });

不好意思直接写的代码 有个错误 $.ajax改为$.post