ajax表单问题:无法从选择的多个数据中获取数据

时间:2013-12-17 12:28:45

标签: javascript php ajax forms

我认为我的问题很简单(但我很简单),我不使用jquery或任何其他插件只是纯JavaScript和PhP。 我有一个带有选择字段的简单表单,其中可以选择多个项目,例如:

<form id="test" name="test">
<input type="hidden" id="norman" value="bates"/>
<select multiple="multiple" name="fred[]" id="fred[]">
    <option value="1">one</option>
    <option value="2">two</option>
    <option value="3">three</option>
    <option value="4">four</option>
</select>
<input type="button" id="button" value="test" onclick="callAHAH('POST','my-page.php','container','loading...','modify-user')"/>    

callAHAH是包装Ajax数据的javaScript函数,函数是:

function callAHAH(method,url, pageElement, callMessage,form_name){
  //window.location = url;

//document.getElementById(pageElement).innerHTML = callMessage;
try{
    req = new XMLHttpRequest(); /* ff */
}
catch(e){
    try{
        req = new ActiveObject("Msxml2.XMLHTTP"); /* some ie */
    }
    catch(e){
        try{
            req = new ActiveXObject("Microsoft.XMLHTTP"); /*other ie */
        }
        catch(e){
            req = false;
        }
    }
}
req.onreadystatechange = function(){responseAHAH(pageElement);};
 var build_url=window.location.origin;
  var url = "aj_scripts/"+url;
 if(build_url=="http://localhost:8080"){        
    url=build_url+"/pitd_outer/pitd/"+url;
 }
// Check request status
if(method=='POST'){
    req.open("POST",url,true);
  // adds  a header to tell the PHP script to recognize the data as is sent via POST
  req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  var the_data="";

  ///
  var the_form=document.getElementById(form_name);
 //alert(form_name);
for (var i=0;i<the_form.length;i++){
      var the_type = the_form.elements[i].type;
      var value;
      if(the_type !="button"){
          if(the_type =="checkbox"){
                value = the_form.elements[i].checked

             // alert("the array name is: "+the_array_name+" and it is checked:"+checked);
          }else{

                value=the_form.elements[i].value;
          }
        var id= the_form.elements[i].id;     
        the_data+=id;
        the_data+="="+value+"&";
      }     
  }      
  the_data=the_data.substring(0,the_data.length-1);//removing the last & symbol  

// alert(the_data);
  req.send(the_data);       // calls the send() method with datas as parameter
}else if(method=="GET"){
    req.open("GET",url,true);
    req.send(null);
}
}

JS处理程序一直工作正常,直到我尝试选择多个项目,然后代码只返回第一个或最后一个选定的项目。

我知道表单正在发送一个数组,但我似乎无法让代码正确测试我试过的数组

var array_test=Array.isArray(the_form.elements[i]);
               alert("array test:"+array_test);

但我得到的都是假的......

  1. 如何获取所有已选择的选择数据,然后
  2. 如何为帖子的文本字符串格式化它

    my_array[]=1$my_array[]=2 etc
    
  3. 提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

好的,我有答案!

  function callAHAH(method,url, pageElement, callMessage,form_name){
  //window.location = url;

//document.getElementById(pageElement).innerHTML = callMessage;
try{
    req = new XMLHttpRequest(); /* ff */
}
catch(e){
    try{
        req = new ActiveObject("Msxml2.XMLHTTP"); /* some ie */
    }
    catch(e){
        try{
            req = new ActiveXObject("Microsoft.XMLHTTP"); /*other ie */
        }
        catch(e){
            req = false;
        }
    }
}
req.onreadystatechange = function(){responseAHAH(pageElement);};
 var build_url=window.location.origin;
  var url = "aj_scripts/"+url;
 if(build_url=="http://localhost:8080"){        
    url=build_url+"/pitd_outer/pitd/"+url;
 }
// Check request status
if(method=='POST'){
    req.open("POST",url,true);
  // adds  a header to tell the PHP script to recognize the data as is sent via POST
  req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  var the_data="";

  ///
  var the_form=document.getElementById(form_name);
 //alert(form_name);
for (var i=0;i<the_form.length;i++){
      var the_type = the_form.elements[i].type;
      var value;
      if(the_type !="button"){
          if(the_type =="checkbox"){
                value = the_form.elements[i].checked
                var id= the_form.elements[i].id;     
                the_data+=id;
                the_data+="="+value+"&";
          }else if(the_form.elements[i].hasAttribute('multiple') == true){
              var test_data = "";
              var the_multiples = the_form.elements[i].options;
              if((the_form.elements[i].hasAttribute('getall')==true)&&(the_form.elements[i].getAttribute('getall')=="get_all")){
                  //a multiple select that we need to get all values from and not just those that are selected
                  alert("hi");
                  console.log("inside the_form_elements has attributes");                   
                for(var j=0;j<the_multiples.length;j++){
                  test_data +=  the_form.elements[i].id+"="+the_multiples[j].value+"&";  
                }//end of for var j
              }else{
              //a select list with potentially multiple selections and only want the selected ones
                  for(var j=0;j<the_multiples.length;j++){
                      if(the_multiples[j].selected == true){
                      test_data +=  the_form.elements[i].id+"="+the_multiples[j].value+"&";
                      }//end of if the_multiples                          
                  }//end of for var j
              }//end of the if the_form inner
              test_data=test_data.substring(0,test_data.length-1);//removing the last & symbol 
              the_data +=test_data;
              alert(test_data);

          }else{

                value=the_form.elements[i].value;
                var id= the_form.elements[i].id;     
                the_data+=id;
                the_data+="="+value+"&";
          }//end of if the_form outer


      }     
  }      
  the_data=the_data.substring(0,the_data.length-1);//removing the last & symbol  

// alert(the_data);
  req.send(the_data);       // calls the send() method with datas as parameter
}else if(method=="GET"){
    req.open("GET",url,true);
    req.send(null);
}
}

  function responseAHAH(pageElement){   
var output='';
if(req.readyState == 4){
    if(req.status == 200){          
        output = req.responseText;
        document.getElementById(pageElement).innerHTML = output;            
    }
}
}

代码的重要部分是:

  if(the_type !="button"){
          if(the_type =="checkbox"){
                value = the_form.elements[i].checked
                var id= the_form.elements[i].id;     
                the_data+=id;
                the_data+="="+value+"&";
          }else if(the_form.elements[i].hasAttribute('multiple') == true){
              var test_data = "";
              var the_multiples = the_form.elements[i].options;
              if((the_form.elements[i].hasAttribute('getall')==true)&&(the_form.elements[i].getAttribute('getall')=="get_all")){
                  //a multiple select that we need to get all values from and not just those that are selected
                  alert("hi");
                  console.log("inside the_form_elements has attributes");                   
                for(var j=0;j<the_multiples.length;j++){
                  test_data +=  the_form.elements[i].id+"="+the_multiples[j].value+"&";  
                }//end of for var j
              }else{
              //a select list with potentially multiple selections and only want the selected ones
                  for(var j=0;j<the_multiples.length;j++){
                      if(the_multiples[j].selected == true){
                      test_data +=  the_form.elements[i].id+"="+the_multiples[j].value+"&";
                      }//end of if the_multiples                          
                  }//end of for var j
              }//end of the if the_form inner
              test_data=test_data.substring(0,test_data.length-1);//removing the last & symbol 
              the_data +=test_data;
              alert(test_data);

          }else{

                value=the_form.elements[i].value;
                var id= the_form.elements[i].id;     
                the_data+=id;
                the_data+="="+value+"&";
          }//end of if the_form outer

基本上我需要起诉JavaScript hasAttribute函数,我知道显然每个浏览器都不支持它,但它适用于IE10,以及FF O和C的最新版本。

从那里我不得不使用.options子选择来循环选择每个选项以查找所选的选项。

那些检查代码会注意到我有一个奇怪的条件,hasAttribute('getall'),因为我有一个非常奇怪的情况,我需要从多个选择中获取所有值,无论是否他们再次被选中我知道非标准属性不是由所有浏览器提供,而是与我使用的那些一起移植。

问候