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.
-
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.
-
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.
If you use the
[]at the end of the name, you are going to receive an array in the$_POSTentry. So if you usename="subscribe[]"you will get$_POST['subscribe']as an array