I have a form with multiple checkboxes that can be selected. If a user wanted they could go in and change the value in the checkbox. I want to know if there is a way I can not accept the form or display an error if the value submitted was not one of the original choices displayed. Here is my code:
HTML Code:
<form action="" method="post">
<table>
<tr>
<td>
<input type="checkbox" name="checkbox[]" value="1">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="2">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="3">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="4">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="5">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="6">
</td>
</tr>
</table>
<input type="submit" name="submit" value="submit">
</form>
PHP Code:
<?php
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
echo $check;
}
}
If you want, since html is static (which I dont know why), you could initialize some original values, then compare it. Consider this example:
<?php
$original_values = array(1, 2, 3, 4, 5, 6);
if(isset($_POST['submit'], $_POST['checkbox'])) {
$submitted_values = $_POST['checkbox'];
foreach($submitted_values as $value) {
if(!in_array($value, $original_values)) {
// this particular submitted value does not belong to the original values that is set
echo 'not good';
exit;
}
}
// all good if it goes down here
echo 'all good';
}
?>
<form method="POST" action="">
<table border="0" cellpadding="10">
<tr>
<?php foreach($original_values as $value): ?>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $value; ?>" /></td>
<?php endforeach; ?>
</tr>
<tr><td colspan="6"><input type="submit" name="submit" value="submit" /></td></tr>
</table>
</form>
Note: To test, try to change the values thru the browser [most likely F12], change the dom values. Example: Try to change a checkbox value attribute to 7 or 100, check it the submit.
Also another way:
$original_values = array(1, 2, 3, 4, 5, 6);
if(isset($_POST['submit'], $_POST['checkbox'])) {
$submitted_values = $_POST['checkbox'];
$differences = array_diff($submitted_values, $original_values);
if(count($differences) > 0) {
echo 'not good';
exit;
}
// all good if it goes down here
echo 'all good';
}
Print all the values inside a table:
if(isset($_POST['submit'], $_POST['checkbox'])) {
$submitted_values = $_POST['checkbox'];
$count = count($submitted_values);
echo "<table border='1' cellpadding='10'>";
echo "<tr><td colspan='$count'>Values</td></tr>";
echo "<tr>";
foreach($submitted_values as $value) {
echo "<td>$value</td>";
}
echo "</tr>";
echo "</table>";
}
Store values in a separate PHP file, checkbox_values.php:
<?php
$checkbox_values = array(1, 2, 3, 4, 5, 6);
?>
And your form, another PHP file, main.php:
<?php
require 'checkbox_values.php';
?>
<form method="POST" action="checkbox_process.php">
<table border="0" cellpadding="10">
<tr>
<?php foreach ( $checkbox_values as $check ) { ?>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $check; ?>" /></td>
<?php } ?>
</tr>
<tr><td colspan="6"><input type="submit" name="submit" value="submit" /></td></tr>
</table>
</form>
And checkbox_process.php:
<?php
require 'checkbox_values.php';
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
if ( in_array($check, $checkbox_values) ) { echo $check; }
}
}
?>
Related
Through checkbox I want to pass & values. how can I do that. I can only pass one value. I am trying but its not working
while($row_tbl = mysqli_fetch_array($query))
{
<tr class="success" style="font-size:12px;">
<td > <input type="checkbox" name="check[]" class="chk_val" value=" <?php echo $row_tbl['Course_ID'] ?> " id="in" onclick="test()" /></td>
<td > <?php echo $row_tbl['Course_ID'] ?> </td>
<td> <?php echo $row_tbl['Course_Title'] ?> </td>
<td> <?php echo $row_tbl['Section'] ?> </td>
<td> <?php echo $row_tbl['Time'] ?> </td>
<td> <?php echo $row_tbl['Day'] ?> </td>
<td> <?php echo $row_tbl['Dept'] ?> </td>
<td> <?php echo $row_tbl['Capacity'] ?> /  0 </td>
<?php
}
To pass multiple values, you need to create multiple checkbox with the same name (as an array)
<input type="checkbox" name="check[]" class="chk_val" value="value1"/>
<input type="checkbox" name="check[]" class="chk_val" value="value2"/>
<input type="checkbox" name="check[]" class="chk_val" value="value3"/>
<input type="checkbox" name="check[]" class="chk_val" value="value4"/>
In your controller,
$values = $request->check; //the array of checked inputs.
Then you can loop through it
foreach($values as $value) {
...
}
Update
Accodring to the chat discussion you want to send 2 different fields per checkbox. In your checkboxes, put both fields like this
#foreach($courses as $course)
<input type="checkbox"
name="check[]"
class="chk_val"
value="{{ $course->id }}-{{ $course->section }}"/>
#endforeach
Pay attention to the value here. I added a delimiter - that will be useful in the controller. In controller now
public function selectedCourses(Request $request)
{
... //whatever you do for validation
//loop through selected courses
foreach( $request->check as $values ) {
$values = explode("-", $values); //split where we added the dash
$id = $values[0]; //the course ID
$section = $values[1]; // the course section
... do what you want here with that information
}
}
And voila!
this is my html code:
<input name="dependencies[]"
and in php i do this:
$dependencies = $_POST['dependencies'];
and when I do this:
print_r($dependencies);
I can see the values like this:
Array ( [0] => [1] => )
My question
I want to add each value from that array to another array:
I didn't know how to do that
I tried:
foreach ($dependencies as $number){
echo $number;
}
but nothing has been printed
Update
this is the html
<input name="dependencies[]" value="<?php $question->id; ?>" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
and I can see the check boxes checked or not when I run the page
Update2
the whole code
<form action="../DB/addDependencies.php" method="post">
<input type="hidden" name="questionID" />
<table>
<tr>
<th>Porgugese Name</th>
<th>Englisn Name</th>
<th>Dependent</th>
</tr>
<?php
foreach ($questions as $question) {
?>
<tr>
<td>
<?php echo $question->ptName; ?>
</td>
<td>
<?php echo $question->enName; ?>
</td>
<td>
<input name="dependencies[]" value="<?php $question->id; ?>" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
</td>
</tr>
<?php
}
?>
</table>
<input type="submit" value="Save" name="submit" />
</form>
You need to change your HTML to this:
<input name="dependencies[<?php $question->id; ?>]" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
Then to test if a checkbox if checked you just need to do something like
function isQuestionChecked($question_id) {
return isset($_POST['dependencies']) && isset($_POST['dependencies'][$question_id]);
}
try to use
foreach ($dependencies as $key=>$val){
var_dump($key);
var_dump ($val);
}
instead of
foreach ($dependencies as $number){
echo $number;
}
;-) hope that will bring some ideas to your mind in this case
I have a form with check box values i want to arrange the checkbox in two columns instead of
one single long column.
How can i split it to two columns ?
Here is the code :
<form id="form" name="form" method="post" action="">
<table width="502" border="0">
<tr>
<td>
Name :
<input type="textbox" name="rcv_group_name" value="">
<input type="hidden" name="add" value="add" />
</td>
</tr>
<tr>
<td align="left">
<strong>HEADING</strong>
<?php
$q = "select * from Config_RCV";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) > 0)
{
$k=0;
while ($row = mysqli_fetch_array($r,MYSQLI_NUM))
{
?>
<br> <input type="checkbox" name ="rcv_val[]" value ="<? echo $row[0];?>" /> <? echo $row[1];?>
<?
$k++;
}
}
?>
</td>
</tr>
<tr>
<td align="right"><input type="submit" name="Submit" id="Submit" value="Submit" style="background-color:#999" />
< /td>
<td height="26" align="left"> </td>
<td> </td>
</tr>
</table>
<form>
add another table and make 2 columns by splitting 2'nd result:
if (mysqli_num_rows($r) > 0) {
$k=0;
echo '<table><tr>';
while ($row = mysqli_fetch_array($r,MYSQLI_NUM)) {
?>
<td><input type="checkbox" name ="rcv_val[]" value ="<?php echo $row[0];?>" /> <?php echo $row[1];?> </td>
<?php
$k++;
if($k%2 == 0){
echo '</tr><tr>';
}
}
echo '</table>';
}
use your $k
if ($k%2==0){
echo "<br />";
}
instead of the lone now
Using tables like above will probably be better for looks
You could just add a second column to your table:
echo "<tr>";
$i = 0;
while ($row = mysqli_fetch_array($r,MYSQLI_NUM)) {
if ($i++%2==0) {
echo "</tr><tr>";
}
echo "<td>".$CELL_CONTENT_HERE."</td>";
}
echo "</tr>";
I need to pass back a large string of results to a form, so that the form can read those results from the URL and then populate the form with them. Problem is, the link ends up being:
&key=value&key=value ... until it can't process anymore (I assume a URL has a length limit?) resulting in my form not being able to fully populate. I need another way to pass values back to my form file.
VIEW.php file (basically just a table of values right as they are from the database, with the first column "id" being a link. When I click on "id", it goes back to my add.php(form page) and populates the form with the data matching that id)
<table border="0" cellpadding="0" cellspacing="0" id="table">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>MANUFACTURER</th>
<th>MODEL</th>
<th>DESCRIPTION</th>
<th>ON HAND</th>
<th>REORDER</th>
<th>COST</th>
<th>PRICE</th>
<th>SALE</th>
<th>DISCOUNT</th>
<th>DELETED</th>
<th></th>
</tr>
</thead>
<tbody>
<?php } ?>
<?php
// loop to fetch data
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>
<a href='molszewski1_a2_add.php'>$row[id]</a></td>";
echo "<td>$row[name]</td>";
echo "<td>$row[manufac]</td>";
echo "<td>$row[model]</td>";
echo "<td>$row[descrip]</td>";
echo "<td>$row[onhand]</td>";
echo "<td>$row[reorder]</td>";
echo "<td>$row[cost]</td>";
echo "<td>$row[price]</td>";
echo "<td>$row[sale]</td>";
echo "<td>$row[discont]</td>";
echo "<td>$row[deleted]</td>";
$status = "$row[deleted]";
echo "<td><a href='molszewski1_a2_delete.php?id=$row[id]&flag=$status&sort=$sort'>";
$status = "$row[deleted]";
if ($status == 'n') {
$flag = "restore";
echo "delete";
} else if ( $status == 'y') {
$flag = "delete";
echo "restore";
}
echo "</a></td>";
echo "</tr>";
} ?>
<?php { ?>
</tbody>
</table>
ADD.php (form page where the form is supposed to fetch the data and populate it)
<?php
// If no form has been submitted, present form
if (empty($_GET))
{
add_form();
}
// if a form has been submitted
else
{
// if form_validity() == 1, proceed to connect
if (form_validity() == 1)
{
// connect to mysql + database
connect();
$saleItem = "n";
$discountItem = "n";
if( array_key_exists( 'saleItem', $_GET ) && $_GET['saleItem'] == 'y' )
{ $saleItem = "y"; }
if( array_key_exists( 'discountItem', $_GET ) && $_GET['discountItem'] == 'y' )
{ $discountItem = "y"; }
// get values from form, insert into database
$sql=("INSERT INTO inventory (name,
manufac,
model,
descrip,
onhand,
reorder,
cost,
price,
sale,
discont,
deleted)
VALUES ('$_GET[itemName]',
'$_GET[manufacturer]',
'$_GET[model]',
'$_GET[description]',
'$_GET[numberOnHand]',
'$_GET[reorderLevel]',
'$_GET[cost]',
'$_GET[sellingPrice]',
'$saleItem',
'$discountItem', 'n')");
// if the query doesn't work, display error message
if (!(mysql_query($sql))) { die ("could not query: " . mysql_error()); }
add_form();
// redirect to view.php after form submission
// use php instead
echo "<meta http-equiv='REFRESH' content='0;url=molszewski1_a2_view.php'>";
}
else
{
// if form is not valid (form_validity returns 0), display error messages
add_form();
}
}
?>
FUNCTIONS.php (all my functions for stuff like the form)
<?php function page_navigation(){ ?>
<div class="center">
<input type="button" value="ADD" />
<input type="button" value="VIEW" />
<input type="button" value="VIEW DELETED" />
<input type="button" value="VIEW ACTIVE" />
<br />
<br />
</div>
<?php } ?>
<?php function add_form() { ?>
<form action="molszewski1_a2_add.php" method="get" id="form">
<table width="529px">
<tr>
<td>ITEM NAME</td>
<td><input name="itemName" size="30" type="text" value="<?php echo $_GET["itemName"] ?>"/></td>
</tr>
<tr>
<td>MANUFACTURER</td>
<td><input name="manufacturer" size="30" type="text" value="<?php echo $_GET["manufacturer"] ?>"/></td>
</tr>
<tr>
<td>MODEL</td>
<td><input name="model" size="30" type="text" value="<?php echo $_GET["model"] ?>"/></td>
</tr>
<tr>
<td>DESCRIPTION</td>
<td><textarea name="description" rows="3" cols="20"><?php echo $_GET["description"] ?></textarea></td>
</tr>
<tr>
<td>ON HAND</td>
<td><input name="numberOnHand" size="30" type="text" value="<?php echo $_GET["numberOnHand"] ?>"/></td>
</tr>
<tr>
<td>REORDER LEVEL</td>
<td><input name="reorderLevel" size="30" type="text" value="<?php echo $_GET["reorderLevel"] ?>"/></td>
</tr>
<tr>
<td>COST</td>
<td><input name="cost" size="30" type="text" value="<?php echo $_GET["cost"] ?>"/></td>
</tr>
<tr>
<td>SELLING PRICE</td>
<td><input name="sellingPrice" size="30" type="text" value="<?php echo $_GET["sellingPrice"] ?>"/></td>
</tr>
<tr>
<td>SALE ITEM</td>
<td>
<input type="checkbox" name="saleItem" value="y" <?php if( isset( $_GET['saleItem'] ) ){ ?> checked="checked" <?php } ?> />
</td>
</tr>
<tr>
<td>DISCOUNTED ITEM</td>
<td>
<input type="checkbox" name="discountItem" value="y" <?php if( isset( $_GET['discountItem'] ) ){ ?> checked="checked" <?php } ?> />
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="save" name="submit" id="submit" /></td>
</tr>
</table>
</form>
<?php } ?>
Use method="post" and $_POST (instead of $_GET).
POST requests can be much larger than GET requests as GET requests are limited by the maximum length of a URL. POST requests are limited by the size of the max_post_size ini-value which is usually a few megabytes.
I've a doubt. I've 3 textboxes and each is having checkboxes next to it. I want to display
the values of only those textboxes whose respective checkboxes are clicked. Following is the attached HTML and PHP codes:
<html>
<head>
</head>
<body>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="a1" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Hostel"></td>
</tr>
<tr>
<td><input type="text" name="b1" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Transport"></td>
</tr>
<tr>
<td><input type="text" name="c1" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Food"></td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
and below is the PHP part.
<?php
$a=$_POST['a1'];
$b=$_POST['b1'];
$c=$_POST['c1'];
$facilityArray = $_POST['facility'];
$facility = "";
if(count($facilityArray) > 0)
{
foreach($facilityArray as $fac)
{
$facility .= " " . $fac;
}
}
echo $facility; echo "<br>";
echo $a; echo "<br>";
echo $b; echo "<br>";
echo $c;
?>
With the help of following codes I am able to display all the values of checked checkboxes. I am also able to display the values of all the textboxes. But I actually want to display the values of only those textboxes whose respective checkboxes are clicked. I know it may be a very basic question but please help me grow in PHP. Thanks in advance... :(
Your textboxes should also be in an array post to achieve this.
To achieve this change the input lines as:
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
From php you'll be getting the posted textboxes in an array as:
$textbox=$_POST['textboxes'];
You should then loop through the checkboxes array and if the corresponding checkbox is "on" (clicked), then display the textboxes value. To do this you would also need a counter to make sure you are on the same array index for both checkboxes and textboxes:
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
I've also added a name to your submit button so you only check the form when it is submitted.
Your page should now look something like this:
<?php
if(isset($_POST['submit']))
{
$textbox=$_POST['textboxes'];
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
?>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td colspan="3"><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
UPDATE:
To make sure that the $_POST variable exists before assigning it to a variable we use the isset(). In your case just update the php segment as:
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['textboxes']))
{
$textbox=$_POST['textboxes'];
if(isset($_POST['facility']))
{
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
}
}
?>
Where the only changes are the addition of another two if statements that take a boolean flag from the isset() function according to whether the $_POST variable has been posted successfully
if(isset($_POST['textboxes']))
AND
if(isset($_POST['facility']))