Jump to content

Getting a value from a checkbox


Ewergreen

Recommended Posts

Hi, I'm new here, so forgive me if I make any newbie mistakes.

 

I'm also new to PHP, but going strong. That is untill I hit this one: I'm trying to get the values from some checkboxes. I have a checkbox for each day of the week and then multiple options for each day. Now I have a name for each checkbox so I know which one = what combination. That's not a problem. The value for each checkbox is 8, so that's not a problem either.

 

But I can't seem to write either a 0 if it's unchecked or an 8 if it's checked, to my database. Any help would be appreciated. Thanks!

 

<?php session_start();
error_reporting(0);
include('connection.php');

$asq7=$_POST['asq7'];
$asq8=$_POST['asq8'];
$asq9=$_POST['asq9'];

$semail=$_SESSION['semail'];

if(isset($_POST['submit'])) 
  {
$sql=" UPDATE calculator SET asq7='$asq7',
							 asq8='$asq8',
							 asq9='$asq9' WHERE email='$semail' ";


	  
$query=mysql_query($sql) or die ("Kon de query niet uitvoeren");

header ("Location: page3.php");

   }
else
   {

?>


<?php include('../include_top.php'); ?>

<div id="container">
<div id="top">
<h1>Z-Matic ROI calculator</h1></div>
<div id="leftnav">
<div class="menu_active">Login</div>
<div class="menu_active">Productgebruik</div>
<div class="menu_active">Shop management</div>
<div class="menu">kostenreductie</div>
<div class="menu">Resultaat</div>


</div>
<div id="content">
<h2>Shop management</h2>

<!--Start of form-->
<form action="page1.php" method="post">
<input type="hidden" name="email" value="<?=$_SESSION['semail']?>" />
<table cellpadding="5" cellspacing="5">
  <tr>
    <td></td>
    <td class="MAINTABLE">Maandag</td>
    <td class="MAINTABLE">Dinsdag</td>
    <td class="MAINTABLE">Woensdag</td>
    <td class="MAINTABLE">Donderdag</td>
    <td class="MAINTABLE">Vrijdag</td>
    <td class="MAINTABLE">Zaterdag</td>
    <td class="MAINTABLE">Zondag</td>
  </tr>
  <tr>
    <td class="MAINTABLE">Dag</td>
    <td><input type="checkbox" name="asq7" value="8"></td>
    <td><input type="checkbox" name="asq8" value="8"></td>
    <td><input type="checkbox" name="asq9" value="8"></td>
    <td><input type="checkbox" name="scmq1" value="8"></td>
    <td><input type="checkbox" name="scmq2" value="8"></td>
    <td><input type="checkbox" name="scmq3" value="8"></td>
    <td><input type="checkbox" name="scmq4" value="8"></td>
  </tr>
  <tr>
    <td class="MAINTABLE">Vroege shift</td>
    <td><input type="checkbox" name="scmq5" value="8"></td>
    <td><input type="checkbox" name="scmq6" value="8"></td>
    <td><input type="checkbox" name="scmq7" value="8"></td>
    <td><input type="checkbox" name="scmq8" value="8"></td>
    <td><input type="checkbox" name="scmq9" value="8"></td>
    <td><input type="checkbox" name="scmq10" value="8"></td>
    <td><input type="checkbox" name="scmq11" value="8"></td>
  </tr>
  <tr>
    <td class="MAINTABLE">Late shift</td>
    <td><input type="checkbox" name="scmq12" value="8"></td>
    <td><input type="checkbox" name="scmq13" value="8"></td>
    <td><input type="checkbox" name="scmq14" value="8"></td>
    <td><input type="checkbox" name="scmq15" value="8"></td>
    <td><input type="checkbox" name="scmq16" value="8"></td>
    <td><input type="checkbox" name="scmq17" value="8"></td>
    <td><input type="checkbox" name="ppq1" value="8"></td>
  </tr>
  <tr>
    <td class="MAINTABLE">Nacht</td>
    <td><input type="checkbox" name="ppq2" value="8"></td>
    <td><input type="checkbox" name="ppq3" value="8"></td>
    <td><input type="checkbox" name="ppq4" value="8"></td>
    <td><input type="checkbox" name="ppq5" value="8"></td>
    <td><input type="checkbox" name="ppq6" value="8"></td>
    <td><input type="checkbox" name="mcq1" value="8"></td>
    <td><input type="checkbox" name="mcq2" value="8"></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td class="MAINTABLE">Overuren</td>
    <td><input type="text" name="mcq4" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="mcq5" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="asq7" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="asq7" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="asq7" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="asq7" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="asq7" maxlength="2" style="width:25px;"></td>
  </tr>
</table>

<table>
<tr>
	<td> </td>
	<td><input name="submit" type="submit" class="button" value="volgende"/></td>
</tr>
</table>
</form>


<!--End of form-->


<?php include('../include_bottom.php'); ?>

<?
}
?>

Link to comment
https://forums.phpfreaks.com/topic/152806-getting-a-value-from-a-checkbox/
Share on other sites

that method works well (what premiso posted). Here's my method. It allows for easier growth (just an opinion)

<?php session_start();
error_reporting(0);
include('connection.php');
$semail=$_SESSION['semail'];

if(isset($_POST['submit']))
{
function check_data($data){
	foreach ($data as $key=>$val){
		$output[$key] = ($val == ?8:0;
	}
	return $output;
}
$post_data = check_data($_POST);
$asq7=$post_data['asq7'];
$asq8=$post_data['asq8'];
$asq9=$post_data['asq9'];

$sql=" UPDATE calculator SET asq7='$asq7',
                         asq8='$asq8',
                         asq9='$asq9' WHERE email='$semail' ";



$query=mysql_query($sql) or die ("Kon de query niet uitvoeren");

header ("Location: page3.php");

}
else
{
include('../include_top.php');
$semail = $_SESSION['semail'];
echo <<< THEEND
<div id="container">
<div id="top">
<h1>Z-Matic ROI calculator</h1></div>
<div id="leftnav">
<div class="menu_active">Login</div>
<div class="menu_active">Productgebruik</div>
<div class="menu_active">Shop management</div>
<div class="menu">kostenreductie</div>
<div class="menu">Resultaat</div>


</div>
<div id="content">
<h2>Shop management</h2>

<!--Start of form-->
<form action="page1.php" method="post">
<input type="hidden" name="email" value="$semail" />
<table cellpadding="5" cellspacing="5">
  <tr>
    <td></td>
    <td class="MAINTABLE">Maandag</td>
    <td class="MAINTABLE">Dinsdag</td>
    <td class="MAINTABLE">Woensdag</td>
    <td class="MAINTABLE">Donderdag</td>
    <td class="MAINTABLE">Vrijdag</td>
    <td class="MAINTABLE">Zaterdag</td>
    <td class="MAINTABLE">Zondag</td>
  </tr>
  <tr>
    <td class="MAINTABLE">Dag</td>
    <td><input type="checkbox" name="asq7" value="8"></td>
    <td><input type="checkbox" name="asq8" value="8"></td>
    <td><input type="checkbox" name="asq9" value="8"></td>
    <td><input type="checkbox" name="scmq1" value="8"></td>
    <td><input type="checkbox" name="scmq2" value="8"></td>
    <td><input type="checkbox" name="scmq3" value="8"></td>
    <td><input type="checkbox" name="scmq4" value="8"></td>
  </tr>
  <tr>
    <td class="MAINTABLE">Vroege shift</td>
    <td><input type="checkbox" name="scmq5" value="8"></td>
    <td><input type="checkbox" name="scmq6" value="8"></td>
    <td><input type="checkbox" name="scmq7" value="8"></td>
    <td><input type="checkbox" name="scmq8" value="8"></td>
    <td><input type="checkbox" name="scmq9" value="8"></td>
    <td><input type="checkbox" name="scmq10" value="8"></td>
    <td><input type="checkbox" name="scmq11" value="8"></td>
  </tr>
  <tr>
    <td class="MAINTABLE">Late shift</td>
    <td><input type="checkbox" name="scmq12" value="8"></td>
    <td><input type="checkbox" name="scmq13" value="8"></td>
    <td><input type="checkbox" name="scmq14" value="8"></td>
    <td><input type="checkbox" name="scmq15" value="8"></td>
    <td><input type="checkbox" name="scmq16" value="8"></td>
    <td><input type="checkbox" name="scmq17" value="8"></td>
    <td><input type="checkbox" name="ppq1" value="8"></td>
  </tr>
  <tr>
    <td class="MAINTABLE">Nacht</td>
    <td><input type="checkbox" name="ppq2" value="8"></td>
    <td><input type="checkbox" name="ppq3" value="8"></td>
    <td><input type="checkbox" name="ppq4" value="8"></td>
    <td><input type="checkbox" name="ppq5" value="8"></td>
    <td><input type="checkbox" name="ppq6" value="8"></td>
    <td><input type="checkbox" name="mcq1" value="8"></td>
    <td><input type="checkbox" name="mcq2" value="8"></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td class="MAINTABLE">Overuren</td>
    <td><input type="text" name="mcq4" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="mcq5" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="asq7" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="asq7" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="asq7" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="asq7" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="asq7" maxlength="2" style="width:25px;"></td>
  </tr>
</table>

<table>
<tr>
      <td> </td>
      <td><input name="submit" type="submit" class="button" value="volgende"/></td>
</tr>
</table>
</form>


<!--End of form-->
THEEND;

include('../include_bottom.php');
}
?>

jonsjava, thanks a lot for the help, but your way just seems to be too complicated for me :)

 

So I went with the easier way, but it doesn't seem to do the trick. I've only done four now, because I don't want to do everything over again.

 

What happens when I execute the code: it writes a blank line with all 0 values to sql and doesn't go to page3.php as it should.

 

Any ideas?

 

<?php session_start();
error_reporting(0);
include('connection.php');

$asq7=isset($_POST['asq7'])?8:0;
$asq8=isset($_POST['asq8'])?8:0;
$asq9=isset($_POST['asq9'])?8:0;
$q35 = $_POST['q35'];


if(isset($_POST['submit'])) 
  {
$sql=" UPDATE calculator SET q7='$q7',
							 q8='$q8',
							 q9='$q9',
							 q35='$q35' WHERE email='$semail' ";


	  
$query=mysql_query($sql) or die ("Kon de query niet uitvoeren");

header ("Location: page3.php");

   }
else
   {

   }

?>


<?php include('../include_top.php'); ?>

<div id="container">
<div id="top">
<h1>Z-Matic ROI calculator</h1></div>
<div id="leftnav">
<div class="menu_active">Login</div>
<div class="menu_active">Productgebruik</div>
<div class="menu_active">Shop management</div>
<div class="menu">kostenreductie</div>
<div class="menu">Resultaat</div>


</div>
<div id="content">
<h2>Shop management</h2>

<!--Start of form-->
<form action="page1.php" method="post">
<input type="hidden" name="email" value="<?=$_SESSION['semail']?>" />
<table cellpadding="5" cellspacing="5">
  <tr>
    <td></td>
    <td class="MAINTABLE">Maandag</td>
    <td class="MAINTABLE">Dinsdag</td>
    <td class="MAINTABLE">Woensdag</td>
    <td class="MAINTABLE">Donderdag</td>
    <td class="MAINTABLE">Vrijdag</td>
    <td class="MAINTABLE">Zaterdag</td>
    <td class="MAINTABLE">Zondag</td>
  </tr>
  <tr>
    <td class="MAINTABLE">Dag</td>
    <td><input type="checkbox" name="q7" value="8"></td>
    <td><input type="checkbox" name="q8" value="8"></td>
    <td><input type="checkbox" name="q9" value="8"></td>
    <td><input type="checkbox" name="q10" value="8"></td>
    <td><input type="checkbox" name="q11" value="8"></td>
    <td><input type="checkbox" name="q12" value="8"></td>
    <td><input type="checkbox" name="q13" value="8"></td>
  </tr>
  <tr>
    <td class="MAINTABLE">Vroege shift</td>
    <td><input type="checkbox" name="q14" value="8"></td>
    <td><input type="checkbox" name="q15" value="8"></td>
    <td><input type="checkbox" name="q16" value="8"></td>
    <td><input type="checkbox" name="q17" value="8"></td>
    <td><input type="checkbox" name="q18" value="8"></td>
    <td><input type="checkbox" name="q19" value="8"></td>
    <td><input type="checkbox" name="q20" value="8"></td>
  </tr>
  <tr>
    <td class="MAINTABLE">Late shift</td>
    <td><input type="checkbox" name="q21" value="8"></td>
    <td><input type="checkbox" name="q22" value="8"></td>
    <td><input type="checkbox" name="q23" value="8"></td>
    <td><input type="checkbox" name="q24" value="8"></td>
    <td><input type="checkbox" name="q25" value="8"></td>
    <td><input type="checkbox" name="q26" value="8"></td>
    <td><input type="checkbox" name="q27" value="8"></td>
  </tr>
  <tr>
    <td class="MAINTABLE">Nacht</td>
    <td><input type="checkbox" name="q28" value="8"></td>
    <td><input type="checkbox" name="q29" value="8"></td>
    <td><input type="checkbox" name="q30" value="8"></td>
    <td><input type="checkbox" name="q31" value="8"></td>
    <td><input type="checkbox" name="q32" value="8"></td>
    <td><input type="checkbox" name="q33" value="8"></td>
    <td><input type="checkbox" name="q34" value="8"></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td class="MAINTABLE">Overuren</td>
    <td><input type="text" name="q35" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="q36" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="q37" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="q38" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="q39" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="q40" maxlength="2" style="width:25px;"></td>
    <td><input type="text" name="q41" maxlength="2" style="width:25px;"></td>
  </tr>
</table>

<table>
<tr>
	<td> </td>
	<td><input name="submit" type="submit" class="button" value="volgende"/></td>
</tr>
</table>
</form>


<!--End of form-->


<?php include('../include_bottom.php'); ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.