1

i'm using Ajax way to process my Data so i have a lot of forms , and i did a check of the forms values using jquery , so i'm ok with the values types and all of validation staff .. now i'm wondering how to get all my values " i'm talking about 25 input ,, and send it to php page

Example :

<input type="text" name="emp_user_name" id="emp_user_name" PLACEHOLDER="User Name" /></li>
    <li><input type="password" name="pass" id="pass" /></li>
    <li><input type="password" name="pass2" id="pass2" /></li>
    <li><input type="text" name="emp_group" id="emp_gorup" PLACEHOLDER="Group" /></li>
    <li><input type="text" name="emp_ar_name" id="emp_ar_name" PLACEHOLDER="Employee Name" /></li>
    <li><input type="text" id="emp_en_name" style="direction:ltr!important;" id="emp_en_name" PLACEHOLDER="Full name" style="direction:ltr ;" /></li>
    <li><input type="text" id="emp_address" name="emp_address" PLACEHOLDER="Address" /></li>
    <li><input type="text" name="emp_num1" style="direction:ltr!important;" id="emp_num1" PLACEHOLDER="091" /></li>
    <li><input type="text" name="emp_num2" style="direction:ltr!important;" id="emp_num2" PLACEHOLDER="092" /></li>
    <li><input type="text" name="emp_email" style="direction:ltr!important;" id="emp_email" PLACEHOLDER="support@huemix.ly" /></li>
    <li style="padding: 39px;">Male : <input type="radio" name="emp_sex" id="emp_sex" PLACEHOLDER="male" />
        Female : <input type="radio" name="emp_sex" id="emp_sex2" PLACEHOLDER="female" />     
    </li>
    <li><input type="text" name="emp_bday" id="emp_bday" PLACEHOLDER="17/02/2011" /></li>
    <li><input type="submit" name="save" onclick="add_emps_to_db()" id="save" value="" /></li>

this is one of my forms .. and this is the javascript function

function add_emps_to_db(){
nocashe = Math.random();
http.open('get','huemix_custom.php?section=emps&action=add_new_emps_to_db&" I Wanna Data Here"&nocache = '+nocache);
http.onreadystatechange = HuemixinsertReplay;
http.send(null);

}

I was using this way , but it's not flexible way to work with a lot of forms

var first_name= encodeURI(document.getElementById('first_name').value);
var middle_name= encodeURI(document.getElementById('middle_name').value);
var last_name= encodeURI(document.getElementById('last_name').value);
var phone_num1= encodeURI(document.getElementById('phone_num1').value);
var phone_num2= encodeURI(document.getElementById('phone_num2').value);
var work_type= encodeURI(document.getElementById('work_type').value);
var work_place= encodeURI(document.getElementById('work_place').value);
var from= encodeURI(document.getElementById('from').value);
var worktype1 = $("#worktype1").is(':checked')?1:0;
var worktype2 = $("#worktype2").is(':checked')?1:0;
var worktype3 = $("#worktype3").is(':checked')?1:0;
var site_name= encodeURI(document.getElementById('site_name').value);
var start_date= encodeURI(document.getElementById('start_date').value);
var end_date= encodeURI(document.getElementById('end_date').value);
var site_url= encodeURI(document.getElementById('site_url').value);
var script_type= encodeURI(document.getElementById('script_type').value);
var last_mod= encodeURI(document.getElementById('last_mod').value);
var cpanel_url= encodeURI(document.getElementById('cpanel_url').value);
var cpanel_user= encodeURI(document.getElementById('cpanel_user').value);
var cpanel_pass= encodeURI(document.getElementById('cpanel_pass').value);
var adminpanel_url= encodeURI(document.getElementById('adminpanel_url').value);
var adminpanel_user= encodeURI(document.getElementById('adminpanel_user').value);
var adminpanel_pass= encodeURI(document.getElementById('adminpanel_pass').value);
var other_url= encodeURI(document.getElementById('other_url').value);
var other_user= encodeURI(document.getElementById('other_user').value);
var other_pass= encodeURI(document.getElementById('other_pass').value);
var ftp_url= encodeURI(document.getElementById('ftp_url').value);
var ftp_user= encodeURI(document.getElementById('ftp_user').value);
var ftp_pass= encodeURI(document.getElementById('ftp_pass').value);
var support= $("#support").is(':checked')?1:0;
var support_end_date= encodeURI(document.getElementById('support_end_date').value);
var id= $(this).closest("td").find("#huemix_id").value;
var notes = encodeURI(document.getElementById('notes').value);

i wanna foreach loop for example .. or any way to add the input field Id + input field value ,, and add all of this to my link automatic ,,

Example : i wanna my link be like this

huemix_custom.php?section=emps&action=add_new_emps_to_db&id_for_virst_input=value_for_first_input&id_for_second_input=value_for_second_input... and goes on this way&nocache = '+nocache

Note : as you see i have 3 types , Input type Text , Password And Radio

Thanx by the way

4

2 回答 2

0

我使用此代码将表单值读入字典:

function getFormValues(form) {
  var res = {};
  var elems = getElementsByTagAndClassName(null, null, form);

  for (var i = elems.length - 1; i >= 0; i--) {
    var elm = elems[i];

    var nodeName = elm.nodeName.toLowerCase();
    var type = elm.type;
    if (!type) type = "";
    type = type.toLowerCase();

    if ((nodeName == "input" || nodeName == "select") && elm.name) {
      var checked = true;
      if (type == "checkbox" || type == "radio") {
        checked = elm.checked;
      }
      if (checked) {
        res[elm.name] = elm.value;
      }
    }
  }

  return res;
}

函数 getElementsByTagAndClassName 来自 mochikit,但还有其他实现。该函数简单地返回一个所有 DOMNode 的列表,这些 DOMNode 是表单的子节点。您还可以使用 firstChild/nextChild 或其他一些访问方法对它们进行迭代

于 2013-05-10T12:57:11.740 回答
-3

您可以使用 JQuery

var values = $("form").serialize();

并通过 AJAX 中的“数据”传递数据

于 2013-05-10T12:54:31.540 回答