2

I can't get acces to input checkbox_material. It's something wrong with my code. Please help me. My textarea workes fint, but checkbox_material never begin set. Please help me to figure out what the problem is.

My Form:

<div class="form_style">
<div id="checkbox_content"></div>
   <input type="checkbox" value="1" alt="Checkbox" name="checkbox_material[]">One
   <input type="checkbox" value="2" alt="Checkbox" name="checkbox_material[]">Two<br>
   <textarea id="materialMessage" rows="5" cols="45" name="material_message"></textarea<br>
   <button id="materialSubmit">Add record</button>
</div>

Javascript:

<script type="text/javascript">
$(document).ready(function() {
    $("#materialSubmit").click(function (e) { 
        e.preventDefault();
        if($("#materialMessage").val() === "") {
            alert("Please enter some text!");
            return false;
        }


    var query_string = '';
    $("input[@type='checkbox'][@name='checkbox_material']").each(function() {
        if (this.checked) {
            query_string += "&checkbox_material[]=" + this.value;
        }
    }); 

    if(query_string.length == 0) {
        alert("Please enter some HELLO!");
        return false;
    }

    alert("Hej");
    $.ajax({
        type: "POST",
        url: "material.php",
        data: "id=1" + query_string,
        success:function(t) {
            $("div#checkbox_content").empty().append(t);
        },
        error:function() {
            $("div#checkbox_content").append("An error occured during processing");
        }
    });




    var myData = "material_message="+ $("#materialMessage").val(); 
    jQuery.ajax({
      type: "POST",         
      url: "material.php",  
      dataType:"text",      
      data:myData,          
      success:function(response) {
        $("#materialResponds").append(response);
        $("#materialMessage").val(''); 
      },
      error:function (xhr, ajaxOptions, thrownError) {
        alert(thrownError); //throw any errors
      }
    });
  });

PHP:

if (isset($_POST['checkbox_material'])) 
{
   print_r($_POST['checkbox_material']); 
}
2
  • 1
    What version of jQuery are you using? @attr selectors were deprecated in 1.2 and removed in 1.3. Commented May 12, 2013 at 19:22
  • 1
    have you tried put numbers in the name of the checkboxes? like name="checkbox_material[0]" name="checkbox_material[1]" and so on? Commented May 12, 2013 at 19:23

1 Answer 1

1

The brackets are part of the name:

var query_string = '';
$("input[name='checkbox_material[]']").each(function() {
    if (this.checked) {
        query_string += "&checkbox_material[]=" + this.value;
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.