繁体   English   中英

在javascript中获取所有表单值,然后将值发送回php页面

[英]Get all form values in javascript and send the values back to php page

我正在使用Ajax方式来处理我的数据,所以我有很多表格,并且我使用jquery检查了表格的值,所以我可以使用值类型和所有验证人员..现在想知道如何获取我所有的值“我正在谈论25 input,并将其发送到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种类型,输入类型为Text,Password和Radio

顺便说一句

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

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或其他一些访问方法对其进行迭代

您可以使用JQuery

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM