Help with validation rules - php

I am trying to figure out how to validate a section of a form using php. If at least one of value 1-5 is checked, then at least one of value A-E must be checked. Value's A-E cannot be allowed without at least one of 1-5 being checked. Multiple values in each section can be selected, as long as there is at least one value in each section checked.
I have tried individual IF statements and arrays without success.
Does anyone have any suggestions or examples? I am missing something and I am certain it is pretty obvious, right in my face.
<div class="grid_12 alpha">
<strong>DISMISSAL</strong> - This section is to be used for errors which require dismissal by a Judge.
</div>
<div class="grid_5 alpha">
<input class="checkbox" type="checkbox" name="dis_checkbox_a" />A
<input class="checkbox" type="checkbox" name="dis_checkbox_b" />B
<input class="checkbox" type="checkbox" name="dis_checkbox_c" />C
<input class="checkbox" type="checkbox" name="dis_checkbox_d" />D
<input class="checkbox" type="checkbox" name="dis_checkbox_e" />E
</div>
<div class="grid_3">
<input class="checkbox" type="checkbox" name="dis_checkbox_mon" />Missing Officers Name
</div>
<div class="grid_4 omega">
<input class="checkbox" type="checkbox" name="dis_checkbox_civ_crim_error" />Criminal/Civil Designation in Error
</div>
<div class="grid_12 alpha">
<input class="checkbox" type="checkbox" name="dis_checkbox_improp_code_descr" />Improper Statute Number AND Description
</div>
<div class="grid_12 alpha">
<input class="checkbox" type="checkbox" name="dis_checkbox_other" />Other
<input id="input" type="text" name="dis_cit_other" size="115" maxlength="110" onkeypress="checkCapsLock( event )" />
</div>
<div class="grid_6 alpha">
Citing Officer:
<input id="input" type="text" name="dis_cit_officer" size="5" maxlength="5" onkeypress="checkCapsLock( event )" />
(Enter PR# only)
</div>
<div class="grid_6 omega">
Supervisor Notified:
<input id="input" type="text" name="dis_supvsr_notified" size="5" maxlength="5" onkeypress="checkCapsLock( event )" />
(Enter PR# only)
</div>

$number_boxes = array($_POST['box1'], $_POST['box2'], $_POST['box3'], $_POST['box4'], $_POST['box5']);
$letter_boxes = array($_POST['box_a'], $_POST['box_b'], $_POST['box_c'], $_POST['box_d'], $_POST['box_e']);
function validateCheckboxes($number_boxes, $letter_boxes){
$numbers_checked = FALSE;
$letters_checked = FALSE;
foreach($number_boxes as $val){
if($val) $numbers_checked = TRUE;
}
foreach($letter_boxes as $val){
if($val) $letters_checked = TRUE;
}
if ($numbers_checked && !$letters_checked){
return FALSE;
} else if(!$numbers_checked && $letters_checked){
return FALSE;
} else {
return TRUE;
}
}

Related

how to use checkbox and input type number together in php

I am creating a meeting room booking application using php. In that, when a person book a room there have an option to book tea or snacks and specify the number of items they need. For that, I use checkboxes to select items and input type number to specify the number of items.i.e, if tea selected you have to specify the no. of tea in the input field. My problem is that I can't store or display the checkbox value and associated number together.my code is
<form class="form-horizontal" method="post" id="bookroom" action="bookedreview.php">
<h4>Food & Beverages</h4>
<div class="checkbox">
<input type="checkbox" value="tea" name="food[]"><label>Tea</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="coffee" name="food[]"><label>Coffee</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="snacks" name="food[]"><label>Snacks</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="water" name="food[]"><label>Water</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="nuts" name="food[]"><label>Nuts</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="breakfast" name="food[]"><label>Breakfast</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="lunch" name="food[]"><label>Lunch</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="dinner" name="food[]"><label>Dinner</label>
<input type="number" name="foodnum[]">
</div>
<input type="submit" name="submit" value="value">
</form>
if (isset($_POST['submit']))
{
$foodnum=$_POST['foodnum'];
$food=$_POST['food'];
//$food=implode(',',$_POST['food']);
foreach($foodnum as $index =>$value){
$fud=$value;
$num=$foodnum['index'];
}
}
but when I display the variable I couldn't get the result. Can anyone help me how could store the checked items and the associated quantity together.
You can get with passing food name in textbox name array
<form class="form-horizontal" method="post" id="bookroom" action="">
<h4>Food & Beverages</h4>
<div class="checkbox">
<input type="checkbox" value="tea" name="food[]"><label>Tea</label>
<input type="number" name="foodnum[tea]">
</div>
<div class="checkbox">
<input type="checkbox" value="coffee" name="food[]"><label>Coffee</label>
<input type="number" name="foodnum[coffee]">
</div>
<div class="checkbox">
<input type="checkbox" value="snacks" name="food[]"><label>Snacks</label>
<input type="number" name="foodnum[snacks]">
</div>
<div class="checkbox">
<input type="checkbox" value="water" name="food[]"><label>Water</label>
<input type="number" name="foodnum[water]">
</div>
<div class="checkbox">
<input type="checkbox" value="nuts" name="food[]"><label>Nuts</label>
<input type="number" name="foodnum[nuts]">
</div>
<div class="checkbox">
<input type="checkbox" value="breakfast" name="food[]"><label>Breakfast</label>
<input type="number" name="foodnum[breakfast]">
</div>
<div class="checkbox">
<input type="checkbox" value="lunch" name="food[]"><label>Lunch</label>
<input type="number" name="foodnum[lunch]">
</div>
<div class="checkbox">
<input type="checkbox" value="dinner" name="food[]"><label>Dinner</label>
<input type="number" name="foodnum[dinner]">
</div>
<input type="submit" name="submit" value="value">
</form>
<?php
if (isset($_POST['submit']))
{
$foodnum=$_POST['foodnum'];
$food=$_POST['food'];
echo "<table border='1' style='width:100%'><tr><th>Food Name</th><th>Count</th></tr>";
// output data of each row
foreach($food as $foo)
{
echo "<tr><td>".$foo."</td><td>".$foodnum[$foo]."</td></tr>";
}
echo "</table>";
}
?>
For Insert
foreach($food as $foo)
{
$fieldVal1=$foo;
$fieldVal1=$foodnum[$foo];
$query ="INSERT INTO foodcounts( foodsname, cnt) VALUES ('".$fieldVal1."','".$fieldVal2."' )";
mysqli_query($conn, $query);
}

how to stop response time and count mouse clicks

The code below is a dynamic way of displaying each option as checkbox buttons for each question:
function ExpandOptionType($option) {
$options = explode('-', $option);
if(count($options) > 1) {
$start = array_shift($options);
$end = array_shift($options);
do {
$options[] = $start;
}while(++$start <= $end);
}
else{
$options = explode(' or ', $option);
}
echo '<p>';
foreach($options as $indivOption) {
echo '<div id="ck-button"><label class="fixedLabelCheckbox"><input type="checkbox" name="options[]" id="option-' . $indivOption . '" value="' . $indivOption . '" /><span>' . $indivOption . '</span></label></div>';
}
echo '</p>';
}
foreach ($arrQuestionId as $key=>$question) {
?>
<p><?php echo ExpandOptionType(htmlspecialchars($arrOptionType[$key])); ?></p>
<p><input type='text' class='questionIds' name='questionids' value='<?php echo htmlspecialchars($arrQuestionId[$key]); ?>' /></p>
<p><input type='text' class='responseTime' name='responsetime' value='00:00:00' /></p>
<p><input type='text' class='mouseClick' name='mouseclick' value='0' /></p>
}
Now below I 2 text inputs which are also in the foreach loop, one for response time and other is for counting mouse click:
<p><input type='text' class='responseTime' name='responsetime' value='00:00:00' /></p>
<p><input type='text' class='mouseClick' name='mouseclick' value='0' /></p>
Now this is my questions:
The response time text input contains a count up timer. What I want is that if the first button checkbox is clicked in a question, the question's response timer should stop. This is so we know how long it took the user to respond answering a particular question
The mouse click text starts with 0 and what I want this text input to do is for every button checkbox that is clicked in a question, the question's mouse click text input counts up the amount of clicks so we know how many clicks on the question's options the user has compiled.
How can the above be achieved?
Below is a jsfiddle showing sample code of what it looks like for one question:
http://jsfiddle.net/zAFND/630/
UPDATE:
Source code showing multiple questions example:
QUESTION 1:
<p>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-A" value="A" />
<span>A</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-B" value="B" />
<span>B</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-C" value="C" />
<span>C</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-D" value="D" />
<span>D</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-E" value="E" />
<span>E</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-F" value="F" />
<span>F</span>
</label>
</div>
</p>
<p><input type='text' class='questionIds' name='questionids' value='73' /></p>
<p><input type='text' class='responseTime' name='responsetime' value='00:00:00' /></p>
<p><input type='text' class='mouseClick' name='mouseclick' value='0' /></p>
QUESTION 2:
<p>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-A" value="A" />
<span>A</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-B" value="B" />
<span>B</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-C" value="C" />
<span>C</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-D" value="D" />
<span>D</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-E" value="E" />
<span>E</span>
</label>
</div>
</p>
<p><input type='text' class='questionIds' name='questionids' value='74' /></p>
<p><input type='text' class='responseTime' name='responsetime' value='00:00:00' /></p>
<p><input type='text' class='mouseClick' name='mouseclick' value='0' /></p>
As you created a reference for setinterval that is good. Now you can remove the set interval on click event of first checkbox and add a click event on all checkbox to increase counter.
That will be
$(document).ready(function(){
var checkBox=$('#ck-button').find('input');
var responsetimer=//your interval function
checkbox.filter(':first').bind('click',function(e){
clearInterval(responsetimer);
});
checkbox.bind('click',function(e){
$('.mouseClick').val(parseInt($('.mouseClick').val())+1);
});
});
Well i am not sure about the question you want timer to be stopped at first button click or first time button clicked. If it is first time than dont use the first binding . keep clear interval in second binding itself.
checkbox.bind('click',function(e){
clearInterval(responsetimer);
$('.mouseClick').val(parseInt($('.mouseClick').val())+1);
});
JS fiddle :http://jsfiddle.net/zAFND/631/
for second option
http://jsfiddle.net/zAFND/638/
UPDATE
if you want this for multiple question wrap each question with a div say <div class="queWrap"></div>
Make a array refrence for your interval function so that it can be clear.
Loop to each queWrap and start timer and assign events on checkbox.
Check example for multiple question :http://jsfiddle.net/zAFND/640/

if (!empty($_POST)) not working

I have a php form (code to follow) with a submit button that runs JSON-events.php as its action (method = POST). In the JSON-events code I am testing whether the form has been submitted using if (!empty($_POST)). My issue is that the JSON-events code doesnt seem to be recognising $_POST.
Heres the form side code section.
<div class="simple_overlay" id="searchform">
<form id="myform" class = "cols" action="json-events.php" method="post" >
<h3>Search</h3>
<p>
<label>address/postcode *</label>
<input type="address" id="searchaddress" />
</p>
<p>
<label for="amount">maximum distance:</label>
<input type="text" id="amount" value="5 miles" style=" border:0; color:#43a8c4; font-weight:bold;" />
</p>
<div id="slider-range-min" style="width:130px; margin-left:4px; margin-bottom:20px"></div>
<p id="terms">
<label>Category 1</label>
<input name="cat1" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 2</label>
<input name="cat2" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 3</label>
<input name="cat3" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 4</label>
<input name="cat4" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 5</label>
<input name="cat5" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 6</label>
<input name="cat6" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 7</label>
<input name="cat7" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 8</label>
<input name="cat8" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 9</label>
<input name="cat9" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 10</label>
<input name="cat10" type="checkbox" value="1" checked="checked"/>
</p>
<p>
<input type="hidden" name="searchlat" id="searchlat"/>
</p>
<p>
<input type="hidden" name="searchlong" id="searchlong" />
</p>
<input type="submit" name="search" id="search"/>
<input type="button" value="Check Address" onclick="codeAddressSearch()"/>
<button type="reset">Reset</button>
</form>
</div>
and here is the top part of the JSON...
if (!empty($_POST)) {
any help very much appreciated
empty() will work if $_POST does not have any values (Empty array), but in your case when you submit without any values still you get an array like below and it's not empty:
Array
(
[searchlat] =>
[searchlong] =>
[search] => Submit Query
)
empty() will return true only if $_POST returns
Array (
)
But it's not going to happen as every form will have one Sumbit button.
Just use
if($_POST) {
//php code
}
It will solve your problem.
Learn more about empty() by visiting http://php.net/manual/en/function.empty.php
Do not use
if (!empty($_POST)) {
to check if there is a post done use this:
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
Conditions that you can use with $_POST
if(count($_POST)>0){
//...
}
if(isset($_POST['field_name'])){
//...
}
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
//..
}
Try this
if(!empty($_POST['search']) && isset($_POST['search']))
much better to check for an actual value in the $_POST array. Eg.
if (!empty($_POST['search']))
Using empty() will fail if value 0 is posted
So it is better to use
isset($_POST['FIELD_NAME'])

html5 form not reacting to submit

I've got a simple one-page form, the kind I've written dozens of times, but this behavior is totally new to me. At first, it was submitting on page-load, thus redirecting automatically to the next page in the series. I'm still not sure how I got that to stop, but now it's not doing anything at all when you hit "submit". The page simply sits there.
I've tried stripping out the error-checking scripts, the show/hide script, even jquery itself, then taking the form down to just one input. I tried getting rid of the redirect and just having it output a simple line, then vardump, and still nothing. I hit submit, and no matter what I do, the page just sits there. I've never run into behavior like this before, and firebug et al give me no errors or leads.
If anyone has any ideas at all, no matter how crazy, I'm willing to try. I'm at a loss. Thanks in advance!
<?php
session_start();
include('functions.php');
if ($_POST['submit']) {
$company = $_POST['company'];
$taxid = $_POST['taxid'];
header("location:step2.php");
}
include('head.php'); ?>
<form method="post" name="basic" action="step1.php">
<div class="col70">
<label for="company">Organization/Business name <img src="img/req.jpg" alt="required"></label>
<input type="text" name="company" id="company" value="" />
</div>
<div class="col30">
<label for="taxid">Taxpayer ID# (IEN or SS#) <img src="img/req.jpg" alt="required"></label>
<input type="text" name="taxid" id="taxid" value="" />
</div>
<div class="newcol">
<label for="address">Mailing Address <img src="img/req.jpg" alt="required"></label>
<input type="text" name="address" id="address" value="" />
</div>
<div class="col30 newcol">
<label for="city">City <img src="img/req.jpg" alt="required"></label>
<input type="text" name="city" id="city" value="" />
</div>
<div class="col30">
<label for="state">State <img src="img/req.jpg" alt="required"></label>
<select name="state" id="state" tabindex="<?php echo $tabin; $tabin++; ?>">
<option value="0"></option>
<option value="1">Alabama</option>
</select>
</div>
<div class="col25">
<label for="zipcode">Zip Code <img src="img/req.jpg" alt="required"></label>
<input type="text" name="zipcode" id="zipcode" value="" />
</div>
<fieldset><legend>1. What kind of group/company do you have? <img src="img/req.jpg" alt="required"></legend>
<span id="nfpfp" class="InputGroup">
<label><input name="nfpfp" id="nfpfp_1" type="radio" value="1" />For-profit business.</label>
<label><input name="nfpfp" id="nfpfp_2" type="radio" value="2" />Non-profit 501(c)3 organization.</label>
</span>
</fieldset>
<fieldset><legend>2. How will you use the booth space? Select all that apply. <img src="img/req.jpg" alt="required"></legend>
<span id="type" class="InputGroup">
<label><input name="food" id="type_1" type="checkbox" value="1" />Food sales</label>
<label><input name="retail" id="type_2" type="checkbox" value="2" />Retail sales</label>
<label><input name="activity" id="type_3" type="checkbox" value="3" />Activity</label>
<label><input name="display" id="type_4" type="checkbox" value="4" />Display</label>
<label><input name="other" id="type_5" type="checkbox" value="5" />Other</label>
</span>
</fieldset>
<label for="otherdetails" class="newcol offsides">Enter a short description of your use. (Ex: "BBQ sandwiches", "kite kits", "face painting".) <img src="img/req.jpg" alt="required"></label>
<input type="text" name="otherdetails" id="otherdetails" value="" />
<fieldset><legend>3. Select any/all that apply. Additional questions may appear, if further information is required.</legend>
<span id="additional" class="InputGroup">
<label><input name="raffle" id="raffle_1" type="checkbox" class="switchcheck1" value="1" />I'll be selling raffle tickets and/or holding a raffle at the festival.</label>
<div class="newcol offstate1">
<label for="raffledetails">You'll need written permission from the Exchange Club. Please enter details about the raffle. <img src="img/req.jpg" alt="required"></label>
<textarea name="raffledetails" id="raffledetails" tabindex="<?php echo $tabin; $tabin++; ?>"></textarea>
</div>
<label><input name="trailer" type="checkbox" id="trailer_1" value="1">I'll be bringing a trailer.</label>
<label><input name="outlets" type="checkbox" id="outlets_1" class="switchcheck2" value="1" />I'll require electrical outlets.</label>
<div class="newcol offstate2">
<label for="outletsdetails">How many outlets will you require? <img src="img/req.jpg" alt="required"></label>
<input type="text" name="outletsdetails" id="outletsdetails" />
</div>
</span>
</fieldset>
<input type="button" name="submit" class="a_button" value="submit" />
</form>
The element with name="submit" is of type button and not submit, so it renders a plain button that does nothing (with the intention that you bind some JavaScript to it).
Use type="submit" instead.
Try
<input type="submit" name="submit" class="a_button" value="submit" />
for your submit button.

echo from the submission form value with different IDs

I have a submission form with three of them
two of them are okay which I use one ID for all the value that going to be submit
but one of them which I'm struggling now is the one with all different values of course and the selection form also with different ID which made me confuse how can I echo it.
<form action="" method="post">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal" >
<legend>เลือกช่อง</legend>
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="ch41" checked="checked" />
<label for="radio-choice-1">number 1</label>
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="ch80" />
<label for="radio-choice-2">number 2</label>
<input type="radio" name="radio-choice-1" id="radio-choice-3" value="ch44" />
<label for="radio-choice-3>"number 3</label>
<input type="radio" name="radio-choice-1" id="radio-choice-4" value="ch42" />
<label for="radio-choice-4">number 4</label>
<input type="radio" name="radio-choice-1" id="radio-choice-5" value="ch83" />
<label for="radio-choice-5">number 5</label>
</fieldset>
</div>
</form>
and here is what I'm trying to echo
<video width="5%" height="5%" src="http://mydomain:<?php echo $_POST["radio-choice-1"]; ?>-<?php echo $_POST["date"]; ?>-<?php echo $_POST["time"]; ?>" alt="" controls="" tabindex="0">
so the problem is on the "radio-choice-#"
since it has 5 numbers on the ID
here is the example
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="ch80" />
<label for="radio-choice-2">number 2</label>
<input type="radio" name="radio-choice-1" id="radio-choice-3" value="ch44" />
<label for="radio-choice-3>"number 3</label>
<input type="radio" name="radio-choice-1" id="radio-choice-4" value="ch42" />
<label for="radio-choice-4">number 4</label>
<input type="radio" name="radio-choice-1" id="radio-choice-5" value="ch83" />
<label for="radio-choice-5">number 5</label>
so if they choose menu number 5
I want to echo here
<video width="5%" height="5%" src="http://mydomain:<?php echo $_POST["the value of number 5"]; ?">
Your code for actually getting the value from the radio button was already correct. Here is something you could use to keep data more organized and be able to get the 'page' number you're on.
<?php
$items = array('ch41', 'ch80', 'ch44', 'ch42', 'ch83');
$page = array_search((empty($_POST['choice']) ? 'ch41' : $_POST['choice']), $items) + 1; // Change 'ch41' to whatever the default page is
// To name the labels, you can add keys for your values like so:
$items = array('home' => 'ch41', 'page' => 'ch80', 'channel' => 'ch44', 'news' => 'ch42', 'contact' => 'ch83');
$page = (empty($_POST['choice']) ? 'home' : array_search($_POST['choice'], $items)); // Replace 'home' with whatever the default page is
// Then replace the <?= $key + 1 ?> below with just <?= $key ?>
?>
You are currently on page <?= $page ?>.
<form action="" method="post">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal" >
<legend>เลือกช่อง</legend>
<?php foreach ($items as $key => $value): ?>
<input type="radio" name="choice" id="radio-choice-<?= $key ?>" value="<?= $value ?>"<?= $key === $_POST['choice'] ? ' checked="checked"' : '' ?> />
<label for="radio-choice-<?= $key ?>">number <?= $key + 1 ?></label>
<?php endforeach; ?>
</fieldset>
</div>
</form>
A rough example. You can add additional channels to the $items array. It will default to page 1 and automatically check the radio button for the page you're currently viewing. I hope this is what you wanted.

Categories