如何循环复选框ID并将它们传递给SQL DB?

时间:2013-02-02 21:20:24

标签: javascript arrays checkbox

我正在努力弄清楚如何找到所有Checkbox输入ID,并通过一个字符串或一些可以处理请求的东西单独传递给我的SQL DB。这就是我到目前为止所做的,它是重复和多余的,并且是PAAAIN,因为我还有70个复选框可以通过,这简直太荒谬了。当然有一种方法可以在Javascript中做到这一点?这一切都在工作,我有一个单独的PHP上传脚本,将所有这些信息发送到数据库,正确,我只是想知道一个快捷方式或数组或任何它是什么,以帮助我管理我的70多个复选框人们将是选择,需要发送到数据库。

下面的代码是我正在使用的,它的工作原理,但你可以看到它处理所有这些ID是荒谬的.....任何想法?我会永远感激。

Separate snippet form HTML file:
<tr>
                    <td>
                        <input id="fire_extinguisher" value="fire_extinguisher" type="checkbox">Fire Extinguisher
                    </td>
                    <td>
                        <input id="eye_wash_shower" type="checkbox">Eye Wash/Shower
                    </td>
                    <td>
                        <input id="ladder_scaffold" type="checkbox">Ladder/Scaffold
                    </td>
                    <td>
                        <input id="warning_signs" type="checkbox">Warning Signs
                    </td>
                    <td>
                        <input id="communications" type="checkbox">Communications
                    </td>
                </tr>

JS FILE

//UPLOAD DATA
//Global variables
var jsaform = {
    'location_detail' : "",
    'emergency_number' : "",
    'today' : "",
    'person_in_charge' : "",
    'other_employees' : "",
    'job_description' : "",
    'job_hazards' : "",
    'energy_source_controls' : "",
    'fire_extinguisher' : "No",
    'eye_wash_shower' : "No",
    'ladder_scaffold' : "No",
    'warning_signs' : "No",
    'communications' : "No",
    'machine_guarding' : "No",
    'forklift' : "No",
    'transportation' : "No",
    'welding_cutting' : "No",
    'test_equipment' : "No",
    'barricades' : "No",
    'gfci_cord' : "No",
    'protective_covering' : "No",
    'hand_tools' : "No",
    'ventilation' : "No",
    'msds' : "No",
    'power_tools' : "No",
    'soldering' : "No",
    'equipment_other' : "",
    'natural_fiber' : "No",
    'flame_resistant' : "No",
    'long_sleeves' : "No",
    'kevlar_sleeves' : "No",
    'tyvek' : "No",
    'hard_hat' : "No",
    'hearing_protection' : "No",
    'sun_screen' : "No",
    'safety_glasses' : "No",
    'goggles' : "No",
    'face_shield' : "No",
    'harness' : "No",
    'attachment_point' : "No",
    'personal_flotation_device' : "No",
    'full_face' : "No",
    'half_face' : "No",
    'dust_mask' : "No",
    'safety_shoes' : "No",
    'rubber_boots' : "No",
    'dielectric' : "No",
    'cotton' : "No",
    'leather' : "No",
    'hot_gloves' : "No",
    'protection_other' : ""
}

function uploaddata() { 

//Read all of the data from the page
jsaform.location_detail = document.getElementById("location_detail").value;
jsaform.emergency_number = document.getElementById("emergency_number").value;
jsaform.today = document.getElementById("today").value;
jsaform.person_in_charge = document.getElementById("person_in_charge").value;
jsaform.other_employees = document.getElementById("other_employees").value;
jsaform.job_description = document.getElementById("job_description").value;
jsaform.job_hazards = document.getElementById("job_hazards").value;
jsaform.energy_source_controls = document.getElementById("energy_source_controls").value;
jsaform.equipment_other = document.getElementById("equipment_other").value;
jsaform.protection_other = document.getElementById("protection_other").value;   
//Read Checked items
if (document.getElementById('fire_extinguisher').checked) {
                    jsaform.fire_extinguisher = 'Yes';
            }
if (document.getElementById('eye_wash_shower').checked) {
                    jsaform.eye_wash_shower = 'Yes';
            }
if (document.getElementById('ladder_scaffold').checked) {
                    jsaform.ladder_scaffold = 'Yes';
            }
if (document.getElementById('warning_signs').checked) {
                    jsaform.warning_signs = 'Yes';
            }

//Send to database upload function
upload_jsaform();   
}
    function upload_jsaform() {
        $.ajax({
      type: 'POST',
      url: './php/upload_jsaform.php',
      data: jsaform,
      async: false,
      dataType: 'text',
      success: function() {
        alert("Thank you. Your Job Safety Analysis form has been submitted.");
        },

      error: function(jqXHR, textStatus, errorThrown) {
        alert("Error... " + textStatus + "\n" + errorThrown);
            }
    });
    return false;
};

1 个答案:

答案 0 :(得分:0)

你可以像这样循环:

for (eID in jsaform) {
    if(document.getElementById(eID) && document.getElementById(eID).checked) {
        jsaform[eID] = 'Yes';
    }
}