How To Select User Selected Checkbox If Several With Same Name php - php

I want to make it so the select boxes user selects stays selected encase validation fails later in my form the rest of the form works perfect but I cant seem to figure how to do this for select boxes without changing all the names can someone please help I have created this which contains just my form checkboxes and the action I will be doing later on so you can see what I require
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="desert_l">
<label for="hnumber"><div class="bookerror"></div>Desert Choice </label>
</div>
<div id="desert_f">
<table width="275">
<tr>
<td width="113">
<label>
<input type="checkbox" name="desgroup[]" value="Vanila Cheesecake"<?php if (isset($dessgroup) && ($dessgroup == "Vanila Cheesecake"))
{
echo ' checked="checked""';
}
?> />
Vanilla Cheesecake</label></td>
<td width="94"><input type="checkbox" name="desgroup[]" value="Carrot Cake"<?php if (isset($dessgroup) && ($dessgroup == "Carrot Cake"))
{
echo ' checked="checked""';
}
?> />
Carrot Cake</td>
<td width="52"><input type="checkbox" name="desgroup[]" value="Jelly"<?php if (isset($dessgroup) && ($dessgroup == "Jelly"))
{
echo ' checked="checked""';
}
?> />
Jelly</td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="desgroup[]" value="Fruit Cheesecake"<?php if (isset($dessgroup) && ($dessgroup == "Fruit Cheesecake"))
{
echo ' checked="checked""';
}
?> >
Fruit Cheesecake</label></td>
<td><input type="checkbox" name="desgroup[]" value="Fruit Flan"<?php if (isset($dessgroup) && ($dessgroup == "Fruit Flan"))
{
echo ' checked="checked""';
}
?> />
Fruit Flan</td>
<td> </td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="desgroup[]" value="Fruit Platter"<?php if (isset($dessgroup) && ($dessgroup == "Fruit Platter"))
{
echo ' checked="checked""';
}
?> />
Fruit Platter</label></td>
<td><input type="checkbox" name="desgroup[]" value="Chocolate Cake"<?php if (isset($dessgroup) && ($dessgroup == "Chocolate Cake"))
{
echo ' checked="checked""';
}
?>/>
Chocolate Cake</td>
<td> </td>
</tr>
</table>
<p>
<input type="submit" name="submit" id="submit" value="Submit" />
</p>
<p><span class="bookerror">
<?php
$dessgroup = $_POST['desgroup'] ;
if (isset ($_POST['submit']))
{
if (isset ($dessgroup))
{
echo "<h2>Desert Choice</h2>";
// Show Deserts Selected
for($i=0; $i < count($dessgroup); $i++)
{
echo $dessgroup[$i]."</br>";
}}
else
{
echo "You Must Select At Least 1 Desert" ;
}
}
?>
</span></p>
<p>Please Choose The Number Specified In Your Menu Choice<br />
</p>
</div>
</form>

Instead of
<input type="checkbox" name="desgroup[]" value="Vanila Cheesecake"<?php if (isset($dessgroup) && ($dessgroup == "Vanila Cheesecake"))
{
echo ' checked="checked""';
}
?> />
try
<input type="checkbox" name="desgroup[]" value="Vanila Cheesecake"<?php if (isset($_POST['desgroup']) && is_array($_POST['desgroup']) && in_array("Vanila Cheesecake", $_POST['desgroup']))
{
echo ' checked="checked""';
}
?> />
It would save you a lot of typing though if you would use arrays consequently.
$arr = array('possible value 1', 'possible value 2');
foreach ($arr as $val) {
?>
<input type="checkbox" name="desgroup[]" value="<?php echo $val; ?>"<?php if (isset($_POST['desgroup']) && is_array($_POST['desgroup']) && in_array($val, $_POST['desgroup']))
{
echo ' checked="checked""';
}
?> />
<?php
}

Try this
<input type="checkbox" name="desgroup[]" value="Vanila Cheesecake" <?php echo ((isset($dessgroup) && ($dessgroup == "Vanila Cheesecake")))?'checked="checked"':''; ?> />

Related

How to check more than one checkbox if value for each checkbox exists in database else unchecked

I have three Table their structure is added down there. i want to show all role on one screen. and when click on one any role it will take you to next screen where (all the permission shown in check boxes) permission that is given to that role checked and other unchecked. and i want to that dynamically, as it can be given and taken by using check boxes. there check boxes should contain role_id and perm_id so it can b use for crud operations.table has many to many relation.
i have the role_id bu using the isset($_GET['role_id']) metthod and perm_id by calling a function that will give me the perm_id
<?php if (isset($_GET['role_id'])) {
$role_id = $_GET['role_id'];
} ?>
user_permission.php page
<?php $row = $role->allPermission($role_id); if(!empty($row)){
?>
<table class="table table-bordered table-striped" id="datatable-editable">
<thead>
<tr>
<th><center>Add</center></th>
<th><center>Update</center></th>
<th><center>Delete</center></th>
<th><center>View</center></th>
</tr>
</thead>
<tbody>
<tr>
<?php $all_permissions = $permission->allPermissions();
while ($row=mysqli_fetch_assoc($all_permissions)) {
$perm_id = $row['perm_id'];
$check_permission = $permission->checkPermission($role_id,$perm_id);
while ($row=mysqli_fetch_assoc($check_permission)){
?>
<td><center><input type="checkbox" perm_id="<?php echo $row['perm_id']?>" value="<?php echo $row['permission']?>"> <?php echo $row['permission']?>
</center>
</td>
<?php
}} ?>
</tr>
<tr>
<?php foreach ($row as $val) {?>
<td>
<?php if ($val['permission'] == 'add' || $val['permission'] == 'update' || $val['permission'] == 'delete' || $val['permission'] == 'view'){ ?>
<input type="checkbox" checked="checked" value="<?php echo $val['id']?>">
<?php } else { ?> <input type="checkbox" value="<?php echo $val['id']?>" > <?php }?>
</td>
<?php } ?>
<!-- foreach ($value as $key => $val) {
// echo $key . ' => ' . $val." / ";
echo $key['prem'];
} -->
<!-- <td>
<?php if (in_array('add', $row)){ ?>
<input type="checkbox" checked="checked" value="<?php echo $id?>">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td>
<td>
<?php if (in_array('update', $row)){ ?>
<input type="checkbox" checked="checked">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td>
<td>
<?php if (in_array('delete', $row)){ ?>
<input type="checkbox" checked="checked">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td>
<td>
<?php if (in_array('view', $row)){ ?>
<input type="checkbox" checked="checked">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td> -->
</tr>
</tbody>
</table>
allPermissions()
public function allPermissions()
{
$query="SELECT * from permission where status = 1";
$conn=$this->Connection();
$result = mysqli_query($conn,$query);
$num_rows = mysqli_num_rows($result);
if ($num_rows>0) {
return $result;
}
}
chechkPermission()
public function checkPermission($role_id,$perm_id)
{
$query = "select * from role_perm rp where role_id = '$role_id' and perm_id = '$perm_id' ";
$conn=$this->Connection();
$result = mysqli_query($conn,$query);
$num_rows = mysqli_num_rows($result);
if ($num_rows>0) {
return $result;
}
}
its been three(now) days i'm stuck here, please help. and sorry for my bad english
Table Structure
This is how i want it
you mean something like this? <input type="checkbox" <?= ($permission >= 3 ? "checked" : "") ?>

How do I install an app on MAMP with MYSQL?

So I have this order form template I'm trying to install locally via MAMP. I've got MySQL server running just fine on MAMP, but when I go to install the app, I get "MySQL extension needs to be loaded for our site to work!"
I think I have to somehow go into the code for this form and change some info, but I'm not even sure if that is what I need to do. Can anyone point me in the right direction?
I pasted the code of what I think is the correct php file I need to edit, but wow I'm lost...
<?php ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>OrderNow - PHP Order form Installation Script</title>
<link href="style.css" rel="stylesheet" type="text/css">
<link href='http://fonts.googleapis.com/css?family=Arvo:400,400italic,700,700italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="logo-main"><img src="../img/logo-main.png" alt=""></div>
<?php
include("../common/config/config.php");
$flag=0;
if(defined('MYSQL_DB_HOST') || defined('MYSQL_DB_NAME') || defined('MYSQL_DB_USERNAME') || defined('MYSQL_DB_PASSWORD'))
{
// if database is connected go to site link
if($_GET['step'] == '')
{
step_4();
}
}
?>
<?php
$step = (isset($_GET['step']) && $_GET['step'] != '') ? $_GET['step'] : '';
switch($step){
case '1':
step_1();//Agree license
break;
case '2':
step_2();//Version check
break;
case '3':
step_3();//Create Database
break;
case '4':
step_4();//Goto site
break;
default:
step_1();
}
function step_1(){
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['agree'])){
header('Location:index.php?step=2');
exit;
}
else if($_SERVER['REQUEST_METHOD'] == 'POST' && !isset($_POST['agree'])){
echo '<div class="error-msg"><span>You must agree to the license.</span></div>';
}
?>
<div class="licence-agree"><h1>license agreement</h1>
<p>By clicking the check box, you agree to the Terms & Conditions.</p>
<form action="index.php?step=1" method="post">
<p>
<input type="checkbox" name="agree" />
I agree to the license
</p>
<input type="submit" value="Continue" />
</form>
</div>
<?php
}
function step_2(){
$pre_error ="";
if($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['pre_error'] ==''){
header('Location:index.php?step=3');
exit;
}
if($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['pre_error'] != '')
echo $_POST['pre_error'];
if (phpversion() < '5.0') {
$pre_error = 'You need to use PHP5 or above for our site!<br />';
}
if (ini_get('session.auto_start')) {
$pre_error .= 'Our site will not work with session.auto_start enabled!<br />';
}
if (!extension_loaded('mysql')) {
$pre_error .= 'MySQL extension needs to be loaded for our site to work!<br />';
}
if (!extension_loaded('gd')) {
$pre_error .= 'GD extension needs to be loaded for our site to work!<br />';
}
if (!is_writable('../common/config/config.php')) {
$pre_error .= 'config.php needs to be writable for our site to be installed!';
}
?>
<div class="version-details">
<table width="100%">
<tr>
<td>PHP Version:</td>
<td><?php echo phpversion(); ?></td>
<td><?php echo (phpversion() >= '5.0') ? 'Ok' : 'Not Ok'; ?></td>
</tr>
<tr>
<td>Session Auto Start:</td>
<td><?php echo (ini_get('session_auto_start')) ? 'On' : 'Off'; ?></td>
<td><?php echo (!ini_get('session_auto_start')) ? 'Ok' : 'Not Ok'; ?></td>
</tr>
<tr>
<td>MySQL:</td>
<td><?php echo extension_loaded('mysql') ? 'On' : 'Off'; ?></td>
<td><?php echo extension_loaded('mysql') ? 'Ok' : 'Not Ok'; ?></td>
</tr>
<tr>
<td>GD:</td>
<td><?php echo extension_loaded('gd') ? 'On' : 'Off'; ?></td>
<td><?php echo extension_loaded('gd') ? 'Ok' : 'Not Ok'; ?></td>
</tr>
<tr>
<td>config.php</td>
<td><?php echo is_writable('../common/config/config.php') ? 'Writable' : 'Unwritable'; ?></td>
<td><?php echo is_writable('../common/config/config.php') ? 'Ok' : 'Not Ok'; ?></td>
</tr>
</table>
<form action="index.php?step=2" method="post">
<input type="hidden" name="pre_error" id="pre_error" value="<?php echo $pre_error;?>" />
<input type="submit" name="continue" value="Continue" />
</form>
</div>
<?php
}
function step_3(){
$sitename="";$database_host="";$database_name="";$table_prefix="";$database_username="";$database_password="";$username="";$admin_password="";$email="";
if (isset($_POST['submit']) && $_POST['submit']=="Install!") {
$sitename=isset($_POST['sitename'])?$_POST['sitename']:"";
$email=isset($_POST['email'])?$_POST['email']:"";
$database_host=isset($_POST['database_host'])?$_POST['database_host']:"";
$database_name=isset($_POST['database_name'])?$_POST['database_name']:"";
$table_prefix=isset($_POST['table_prefix'])?$_POST['table_prefix']:"";
$database_username=isset($_POST['database_username'])?$_POST['database_username']:"";
$database_password=isset($_POST['database_password'])?$_POST['database_password']:"";
$username=isset($_POST['admin_name'])?$_POST['admin_name']:"";
$admin_password=isset($_POST['admin_password'])?$_POST['admin_password']:"";
define('PREFIX', $table_prefix);
if (empty($username) || empty($admin_password) || empty($database_host) || empty($database_username) || empty($database_name)) {
echo "<div class='error-msg'><span>All fields are required! Please re-enter.</span></div>";
}
elseif(filter_var($email, FILTER_VALIDATE_EMAIL)==false)
{
echo "<div class='error-msg'><span>Enter a valid email ID.</span></div>";
}
else
{
$connection = mysql_connect($database_host, $database_username, $database_password);
if($connection==false)
{
echo "<div class='error-msg'>Could not connect to database. Check your username and password then try again.\n</div>";
}
else
{
if(!mysql_select_db($database_name, $connection))
{
echo "<div class='error-msg'>Could not select database.\n</div>";
}
$f=fopen("../common/config/config.php","w");
$database_inf=
"<?php
define('MYSQL_DB_HOST', '".$database_host."');
define('MYSQL_DB_NAME', '".$database_name."');
define('MYSQL_DB_USERNAME', '".$database_username."');
define('MYSQL_DB_PASSWORD', '".$database_password."');
define('TABLE_PREFIX', '".$table_prefix."');
define('SITE_LINK', '');
?>";
if (fwrite($f,$database_inf)>0)
{
fclose($f);
}
// include("../settings.php");
include("tables.php");
if(!empty($tableArray))
{
for ($t = 0; $t < count($tableArray); $t++)
{
if(!empty($tableArray[$t]['table_create']))
{
mysql_query($tableArray[$t]['table_create'], $connection);
}
if(!empty($tableArray[$t]['table_insert']))
{
mysql_query($tableArray[$t]['table_insert'], $connection);
}
}
mysql_query($query1, $connection);
mysql_query($query2, $connection);
mysql_query($query3, $connection);
mysql_query("UPDATE `".PREFIX."administrator` SET Name='".$sitename."',email='".$email."',username='".$username."',password='".$admin_password."',order_mail_to='".$email."' ", $connection);
mysql_close($connection);
}
header("Location:index.php?step=4");
}
}
}
?>
<div class="db-details">
<h1>Database Connection</h1>
<form method="post" action="index.php?step=3">
<p>
<label for="database_name">Site Name</label>
<input type="text" name="sitename" size="30" value="<?php echo $sitename; ?>">
</p>
<p>
<label for="database_name">Database Name</label>
<input type="text" name="database_name" size="30" value="<?php echo $database_name; ?>">
</p>
<p>
<label for="database_name">Table Prefix</label>
<input type="text" name="table_prefix" size="30" value="<?php echo $table_prefix; ?>_">
</p>
<p>
<label for="database_host">Database Host</label>
<input type="text" name="database_host" value='localhost' size="30">
</p>
<p>
<label for="database_username">Database Username</label>
<input type="text" name="database_username" size="30" value="<?php echo $database_username; ?>">
</p>
<p>
<label for="database_password">Database Password</label>
<input type="text" name="database_password" size="30" value="<?php echo $database_password; ?>">
</p>
<h3>Admin Details</h3>
<p>
<label for="username">Admin Username</label>
<input type="text" name="admin_name" size="30" value="<?php echo $username; ?>">
</p>
<p>
<label for="password">Admin Password</label>
<input name="admin_password" type="password" size="30" maxlength="15" value="<?php echo $admin_password; ?>">
</p>
<p>
<label for="database_name">Email</label>
<input type="text" name="email" size="30" value="<?php echo $email; ?>">
</p>
<p>
<label> </label>
<input type="submit" name="submit" value="Install!">
</p>
</form>
</div>
<?php
}
function step_4(){
?>
<p align="center">Site home page</p>
<p align="center">Admin page</p>
<?php
}
?>
<div class="copy-right">© 2016 OrderNow Responsive Order Form.</div>
</body>
</html>
<?php
ob_end_flush();?>
I believe the web app 'OrderNow v1.4' uses PHP 5+ and as the MySQL extension is removed in PHP 7+ (instead, either the MySQLi or PDO_MySQL extension is to be used.) and that is why you are not able to run the web app .
I suggest you to download the older version of MAMP from here or you can download the Additional PHP versions version which are PHP 4 or below as the MySQL extension is deprecated as of PHP 5.5.0

validate radio buttons php

I have a quiz which shows 1 question per page. If the user clicks next question without selecting a multiple choice answer, I'm trying to get validation to appear so that they can't advance unless they select an answer. When the user currently presses next question the error: 'Notice: Undefined index: answer' appears
any help?
quiz.php:
if(isset($_POST['checkQuiz'])) {
$a=$_POST['a'];
$quiz_id=$_SESSION['quiz_id'];
$index=$_SESSION['index'];
$resultQuery = mysqli_query($con,"SELECT `correctValue` FROM quiz_questions WHERE quiz_id = '$quiz_id' LIMIT 1 OFFSET $index");
$cor=0;
$incorrect=0;
while ($correct = mysqli_fetch_array($resultQuery)){
if ($_POST['answer'] == $correct[0]) {
$_SESSION['rightAnswers']+=1;
}
if ($_POST['answer'] != $correct[0]) {
$_SESSION['wrongAnswers']+=1;
}
}
}
<form method="post" action="" class="form complete">
<table>
<td>
<td width = "50" id="question"><?php echo $result['question'] . "<br>"; ?></td>
</td>
<tr height = "10"></tr>
<td id= "number" width = "20" class="number"><?php echo $questionNumber ?>)</td>
<td id = "possible_answers" height = "100"width = "700">
<input type="radio" name="answer" onClick="changeColour('a')" value="<?php echo $result['answerA'] ?>"> <?php echo $result['answerA']; ?> <br>
<input type="radio" name="answer" onClick="changeColour('b')" value="<?php echo $result['answerB'] ?>"> <?php echo $result['answerB']; ?> <br>
<input type="radio" name="answer" onClick="changeColour('c')" value="<?php echo $result['answerC'] ?>"> <?php echo $result['answerC']; ?> <br>
<input type="radio" name="answer" onClick="changeColour('d')" value="<?php echo $result['answerD'] ?>"> <?php echo $result['answerD']; ?> <br><br>
</table>
<?php
$_SESSION['questionNumber']=$questionNumber;
}
$a=$a+1;
?>
<input type="submit" name="exitQuiz" value="Exit Quiz" id="button1">
<?php
if ($questionNumber<$_SESSION['numberOfQuestions']) {
?>
<input type="submit" name="checkQuiz" value="Next Question" id="button1">
<input type="hidden" value="<?php echo $a ?>" name="a">
<?php
}
?>
<?php
if ($questionNumber==$_SESSION['numberOfQuestions']) {
?>
<input type="submit" name="checkResult" value="Quiz Result" id="button1">
<input type="hidden" value="<?php echo $a ?>" name="a">
<?php
} ?>
You need to check that $_POST['answer'] is set, like this:
while ($correct = mysqli_fetch_array($resultQuery)){
if (!isset($_POST['answer']) || $_POST['answer'] != $correct[0]) {
$_SESSION['wrongAnswers']+=1;
} elseif ($_POST['answer'] == $correct[0]) {
$_SESSION['rightAnswers']+=1;
}
}

php loop over array that has been sent from html form with the same variable name

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

preventing altered input value checkbox

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; }
}
}
?>

Categories