获取javascript中的所有表单值并将值发送回php页面

时间:2013-05-10 12:46:21

标签: javascript

我正在使用Ajax方式处理我的数据 所以我有很多表单,并且我使用jquery检查了表单值,所以我对值类型和所有验证人员都很好。 现在我想知道如何获得我所有的价值观“我说的是25输入, 并将其发送到php页面

示例:

<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>

这是我的表格之一.. 这是javascript函数

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);

}

我一直在使用这种方式,但使用大量表单并不是灵活的方法

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);

我想以foreach循环为例..或任何添加方式 输入字段Id +输入字段值,, 并将所有这些添加到我的链接自动,

示例: 我想我的链接就像这样

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

注意:如您所见,我有3种类型,输入类型文本,密码和广播

顺便说一下,

Thanx

3 个答案:

答案 0 :(得分: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的列表。您也可以使用firstChild / nextChild或其他一些访问方法迭代它们

答案 1 :(得分:-2)

您可以使用JQuery

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

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

答案 2 :(得分:-2)

您可以使用以下内容:

var url = "huemix_custom.php?section=emps&action=add_new_emps_to_db&";

$("#YourFormId").find("[name]").each(function (index, item) {
    var $item = $(item);
    if ($item.is("[type=radio]")) {
        url += $item.attr("name") + "=" + ($item.is(':checked') ? 1 : 0) + "&";
    }
    else url += $item.attr("name") + "=" + encodeURI($item.val()) + "&";
});

url += "nocache = '+nocache";