Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8537815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:59:22+00:00 2026-06-11T10:59:22+00:00

I have a contact form with some fields like Name, Email, Subscribe. The Subscribe

  • 0

I have a contact form with some fields like Name, Email, Subscribe. The Subscribe is a set of checkboxes (array) that will allow user to select multiple options at the same time.

IMPORTANT:

Please do note that I intend to make the form work in the absence of javascript as well & hence I need the input name as “subscribe[]”. Just take a look at the HTML code of the checkboxes & you will see what I mean.

FULL SOURCE CODE (WITH MY DEBUGGING):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX Posting of checkbox array</title>
</head>

<body>

<form id="frm_contact" name="frm_contact" method="post" action="ajax_contact_us.php">

<label for="name">Name</label>

<input type="text" name="name" id="name" class="text"  /><br />


<label for="email">Email</label>

<input type="text" name="email" id="email" class="text"  /><br />

<label for="subscribe">Subscribe</label>

<input type="checkbox" class="subscribe" name="subscribe" value="email" id="subscribe_0" />Email
<input type="checkbox" class="subscribe" name="subscribe" value="sms" id="subscribe_1" />SMS

<br />
<input type="submit" name="sbt_send" id="sbt_send" value="Send" class="btn_submit" />

</form>

<div id="test"></div>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){


    $('#sbt_send').click(function(e) {      

        $('#test').html();  

        var subscribe = new Array();
        $(".subscribe:checked").each(function() {
           subscribe.push($(this).val());
        });

        //Prepare data to be sent
        var dataString = $('#frm_contact').serialize();

        $.ajax({

          type: "POST",
          url: "ajax_contact_us11.php",
          data: dataString,
          dataType: 'json',
          cache: false,

          success: (function(response) 
            {
                if(response.error == 0)             
                {               
                    alert('yes');
                }
                else
                {
                    //alert('no');
                    $('#test').html('');                        

                    $('#test').append(  response.message['name'] + '<br />'+
                                        response.message['email'] + '<br />'+
                                        response.check );                                   
                }
            })                                                
        });                 


        e.preventDefault();

    });

});


</script>     
</body>
</html>

//ajax_contact_us11.php

<?php session_start();


$errors = array();

$str = '';

if( !isset( $_POST['name'] ) || trim( $_POST['name'] ) == '' )
{
    $errors['name'] = 'Enter name';
}

if( !isset( $_POST['email'] ) || trim( $_POST['email'] ) == '' )
{
    $errors['email'] = 'Enter email';
}

if( !isset( $_POST['subscribe'] ) || trim( $_POST['subscribe'] ) == '' )
{
    $errors['subscribe'] = 'Check at least one checkbox';
    $str = $errors['subscribe'];
}
else
{
    $str = $_POST['subscribe'];
}


if(count($errors))
{
    $error = 1;
    $message =  $errors;
}
else
{
    $error = 0;
    $message =  "success";
}   

print json_encode(array('error' => $error, 'message' => $message, 'check' => $str));
die(); 

?>

ISSUES:

There are 2 issues here.

  1. If I am using subscribe[] as the checkbox input name, then I am unable to get any of the values of the checkbox, even if they are both checked or just one of them is checked.

  2. If I remove [] i.e. use merely “subscribe” instead of “subscribe[]” for the name of the checkboxes, then I am able to retrieve only one value for the checkboxes, even if two are checked.

I assuming that it would be easy to solve this if “subscribe” is used instead of “subscribe[]” but I really need to use “subscribe[]”. I can make the form work when JS is disabled, but in order to post the both values correctly (in an event both the checkboxes are checked), I need the name to be “subscribe[]”.

So how I can retrieve both the checkboxes values and at the same time use “subscribe[]” as the name?

Please note that I stripped down a lengthy form to its most basic form, for example sakes. I would be adding more checkboxes to the current options & also new set of checkboxes with other names. So I would be applying the solution for this question to the new set of inputs that I would be adding to the form.

I have been working on this since one & half days, tried many solutions from SO & Google, but unfortunately, none helped.

I appreciate all help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:59:23+00:00Added an answer on June 11, 2026 at 10:59 am

    If you use the [] at the end of the name, you are going to receive an array in the $_POST entry. So if you use name="subscribe[]" you will get $_POST['subscribe'] as an array

    if( !isset( $_POST['subscribe'] ) || !is_array( $_POST['subscribe'] ))
    {
        $errors['subscribe'] = 'Check at least one checkbox';
        $str = $errors['subscribe'];
    }
    else
    {
        $str = $_POST['subscribe'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a contact form and I have some fields that are validated by
I have a very simple contact form with fields like this: <label> First Name<em>*</em>
I have a contact form which asks users for their name, email address and
Have a form that is emailed with checkboxes. Some checkboxes have an optional field
I have a form field where a user enters contact information, including name, email,
I have a contact form on a website (a general form: name , email
I have a simple contact form on a website that has 2 text fields,
i have a contact form sending itself to me by email using ASP classic,
I have a contact form where the email is actually accessible in the source,
I have a contact form that can be hidden using .slideToggle() but I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.