2013-05-10 13 views
1

我使用Ajax方式處理我的數據 所以我有很多表單,並且我使用jquery檢查了表單值,所以我「M確定與值類型和所有驗證工作人員的..現在 我想知道如何讓我的所有值「我說的是25輸入,, 並將其發送到PHP頁面在javascript中獲取所有表單值並將值發送回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="[email protected]" /></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種類型,輸入類型文本,密碼和無線電

感謝名單的方式

+0

,如果你使用jQuery你可以做'$(「#myForm會」)連載( );' – Kenneth 2013-05-10 12:48:33

+0

不,它是唯一的Javascript頁面, ,當我在同一頁面使用jquery 所有我的其他功能停止工作 所以我正在尋找JavaScript解決方案 – 2013-05-10 12:52:13

回答

0

我使用此代碼將表單值讀入到一個dic tionary:

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的列表,它們是form的子項。您也可以在他們使用則firstChild/nextChild或一些其它的訪問方法重複

+0

var elems = form.elements; 請參閱:https://developer.mozilla.org/en-US/docs/DOM/Form.elements – pawel 2013-05-10 13:19:26

+0

謝謝..我真的不理解你,但我會嘗試你的解決方案兄弟 – 2013-05-10 21:50:56

-2

您可以使用jQuery

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

和通通過AJAX中的「數據」的數據

+0

在OP的評論中說,他不能使用jQuery – 2013-05-10 12:59:36

+0

沒有用! – 2013-05-24 18:37:16

-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"; 
+0

yooo人我說過javascript不jQuery! – 2013-05-10 21:50:15

+0

@Ahmed Rhuma,jquery是javascript,但沒關係,下次把你的問題放在你的問題中,你想要一個非jQuery的解決方案,因爲它沒有在你的問題中指定。 – 2013-06-03 11:10:26

相關問題