multi afdeling subafdeling
hier is mijn code:
form.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
mysql_connect('localhost','root','');
mysql_select_db('test');
/* Nieuwe trarining id genereren */
$query = "SELECT id FROM training ORDER BY id DESC LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$last_id = $row["id"];
$id_num = $last_id + 1;
$new_id = $id_num;
?>
<html>
<head>
<script type = "text/javascript">
function showSelect4() {
var d = document.getElementById("mydiv4");
if (document.getElementById("chk4").checked) {
d.style.display ="block";
}
else {
d.style.display = "none";
d.selectedIndex = 0;
}
}
function showSelect6() {
var d = document.getElementById("mydiv6");
if (document.getElementById("chk6").checked) {
d.style.display ="block";
}
else {
d.style.display = "none";
d.selectedIndex = 0;
}
}
</script>
</head>
<body>
<div id="serv">
<b>Nieuwe training toevoegen</b>
<br/><br/>
<form name="servForm" action="check.php" method="post" >
<table width="300" border="0">
</div>
<div id="choseLoc">
<table border="0">
<br>Selecteer afdeling:
<div id="">
<input type="checkbox" id = "chk4" name="chk1[]" value="4" onclick = "showSelect4()">Afdeling 1<br>
</div>
<div id = "mydiv4" style="display:none">
<input type="checkbox" name="chk2[]" value="9">A<br />
<input type="checkbox" name="chk2[]" value="12">B<br />
<input type="checkbox" name="chk2[]" value="2">C<br />
</div>
<br>
<br>
<div id="">
<input type="checkbox" id = "chk6" name="chk1[]" value="6" onclick = "showSelect6()">Afdeling 2<br>
</div>
<div id = "mydiv6" style="display:none">
<input type="checkbox" name="chk2[]" value="9">A<br />
<input type="checkbox" name="chk2[]" value="12">B<br />
</div>
</table>
</div>
<br/>
<div id="buttons">
<input type="reset" value="Clear" /> <input type="submit" value="Save" name="savebtn" />
</form>
</div>
</body>
</html>
mysql_connect('localhost','root','');
mysql_select_db('test');
/* Nieuwe trarining id genereren */
$query = "SELECT id FROM training ORDER BY id DESC LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$last_id = $row["id"];
$id_num = $last_id + 1;
$new_id = $id_num;
?>
<html>
<head>
<script type = "text/javascript">
function showSelect4() {
var d = document.getElementById("mydiv4");
if (document.getElementById("chk4").checked) {
d.style.display ="block";
}
else {
d.style.display = "none";
d.selectedIndex = 0;
}
}
function showSelect6() {
var d = document.getElementById("mydiv6");
if (document.getElementById("chk6").checked) {
d.style.display ="block";
}
else {
d.style.display = "none";
d.selectedIndex = 0;
}
}
</script>
</head>
<body>
<div id="serv">
<b>Nieuwe training toevoegen</b>
<br/><br/>
<form name="servForm" action="check.php" method="post" >
<table width="300" border="0">
</div>
<div id="choseLoc">
<table border="0">
<br>Selecteer afdeling:
<div id="">
<input type="checkbox" id = "chk4" name="chk1[]" value="4" onclick = "showSelect4()">Afdeling 1<br>
</div>
<div id = "mydiv4" style="display:none">
<input type="checkbox" name="chk2[]" value="9">A<br />
<input type="checkbox" name="chk2[]" value="12">B<br />
<input type="checkbox" name="chk2[]" value="2">C<br />
</div>
<br>
<br>
<div id="">
<input type="checkbox" id = "chk6" name="chk1[]" value="6" onclick = "showSelect6()">Afdeling 2<br>
</div>
<div id = "mydiv6" style="display:none">
<input type="checkbox" name="chk2[]" value="9">A<br />
<input type="checkbox" name="chk2[]" value="12">B<br />
</div>
</table>
</div>
<br/>
<div id="buttons">
<input type="reset" value="Clear" /> <input type="submit" value="Save" name="savebtn" />
</form>
</div>
</body>
</html>
check.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<html>
<head>
</head>
<body>
<?php
mysql_connect('localhost','root','');
mysql_select_db('test');
$query = "SELECT id FROM training ORDER BY id DESC LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$last_id = $row["id"];
$id_num = $last_id + 1;
$new_id = $id_num;
$count = mysql_num_rows($result);
?>
<?php
if(isset($_POST["savebtn"]))
{
$checkbox1 = $_POST['chk1'];
$checkbox2 = $_POST['chk2'];
echo "Hoofd Cat ". print_r($_POST['chk1']);
echo "<br />";
echo "Sub Cat ".print_r($_POST['chk2']);
echo "<br />";
echo "Size ".sizeof($checkbox2);
echo "<br />";
if(sizeof($checkbox2) > 0){
for ($i=0; $i<sizeof($checkbox1);$i++) {
foreach($checkbox2 as $waarde) {
$query="INSERT INTO afdeling_subafdeling(afdeling_id,subafd_id) VALUES('".$checkbox1[$i]."','".$waarde."')";
mysql_query($query) or die(mysql_error());
}
}
}else{
foreach($checkbox1 as $waarde){
$subcat = 0;
$query="INSERT INTO afdeling_subafdeling(afdeling_id,subafd_id) VALUES('".$waarde."','".$subcat."')";
mysql_query($query) or die(mysql_error());
}
}
?>
<?php
}
?>
<a href="form.php">Nog een training toevoegen</a>;
<a href="index.php">Terug naar de hoofdmenu</a>;
</body>
</html>
<head>
</head>
<body>
<?php
mysql_connect('localhost','root','');
mysql_select_db('test');
$query = "SELECT id FROM training ORDER BY id DESC LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$last_id = $row["id"];
$id_num = $last_id + 1;
$new_id = $id_num;
$count = mysql_num_rows($result);
?>
<?php
if(isset($_POST["savebtn"]))
{
$checkbox1 = $_POST['chk1'];
$checkbox2 = $_POST['chk2'];
echo "Hoofd Cat ". print_r($_POST['chk1']);
echo "<br />";
echo "Sub Cat ".print_r($_POST['chk2']);
echo "<br />";
echo "Size ".sizeof($checkbox2);
echo "<br />";
if(sizeof($checkbox2) > 0){
for ($i=0; $i<sizeof($checkbox1);$i++) {
foreach($checkbox2 as $waarde) {
$query="INSERT INTO afdeling_subafdeling(afdeling_id,subafd_id) VALUES('".$checkbox1[$i]."','".$waarde."')";
mysql_query($query) or die(mysql_error());
}
}
}else{
foreach($checkbox1 as $waarde){
$subcat = 0;
$query="INSERT INTO afdeling_subafdeling(afdeling_id,subafd_id) VALUES('".$waarde."','".$subcat."')";
mysql_query($query) or die(mysql_error());
}
}
?>
<?php
}
?>
<a href="form.php">Nog een training toevoegen</a>;
<a href="index.php">Terug naar de hoofdmenu</a>;
</body>
</html>
als ik van de eerste afdeling A en B selecteer en van de tweede ook A en B, wordt dit zo in het database opgeslagen.
afdeling_id subafd_id
4 9
4 12
4 9
4 12
6 9
6 12
6 9
6 12
- Aar -:
Gelieve in het vervolg bij code de [code][/code]-tags gebruiken. De quote-tag is alleen bedoeld voor het citeren van tekst.
Alvast bedankt!
Alvast bedankt!
Gewijzigd op 28/04/2014 18:03:39 door - Ariën -
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
<div id="">
<input type="checkbox" id = "chk4" name="chk1[]" value="4" onclick = "showSelect4()">Afdeling 1<br>
</div>
<div id = "mydiv4" style="display:none">
<input type="checkbox" name="chk2[4][]" value="9">A<br />
<input type="checkbox" name="chk2[4][]" value="12">B<br />
<input type="checkbox" name="chk2[4][]" value="2">C<br />
</div>
<input type="checkbox" id = "chk4" name="chk1[]" value="4" onclick = "showSelect4()">Afdeling 1<br>
</div>
<div id = "mydiv4" style="display:none">
<input type="checkbox" name="chk2[4][]" value="9">A<br />
<input type="checkbox" name="chk2[4][]" value="12">B<br />
<input type="checkbox" name="chk2[4][]" value="2">C<br />
</div>
Dan in PHP zo uitlezen:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$values = array();
foreach ($_POST['chk1'] as $key) {
$values = array();
if (isset($_POST['chk2'][$key]) {
foreach($_POST['chk2'][$key] as $sub) {
$values[] = '(' . (int)$key . ',' . (int) $value . ')';
}
}
}
if (count($values) > 0) {
$query = "INSERT INTO afdeling_subafdeling(afdeling_id,subafd_id)
VALUES " . implode(',', $values);
mysql_query($query);
}
?>
$values = array();
foreach ($_POST['chk1'] as $key) {
$values = array();
if (isset($_POST['chk2'][$key]) {
foreach($_POST['chk2'][$key] as $sub) {
$values[] = '(' . (int)$key . ',' . (int) $value . ')';
}
}
}
if (count($values) > 0) {
$query = "INSERT INTO afdeling_subafdeling(afdeling_id,subafd_id)
VALUES " . implode(',', $values);
mysql_query($query);
}
?>
Gewijzigd op 28/04/2014 17:39:13 door Ger van Steenderen
Ger van Steenderen op 28/04/2014 17:38:38:
Je zal wat meer anderscheid moeten maken bij de checkboxen per afdeling, nu wordt dat een array met het aantal items wat is aangevinkt, bv:
Dan in PHP zo uitlezen:
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
<div id="">
<input type="checkbox" id = "chk4" name="chk1[]" value="4" onclick = "showSelect4()">Afdeling 1<br>
</div>
<div id = "mydiv4" style="display:none">
<input type="checkbox" name="chk2[4][]" value="9">A<br />
<input type="checkbox" name="chk2[4][]" value="12">B<br />
<input type="checkbox" name="chk2[4][]" value="2">C<br />
</div>
<input type="checkbox" id = "chk4" name="chk1[]" value="4" onclick = "showSelect4()">Afdeling 1<br>
</div>
<div id = "mydiv4" style="display:none">
<input type="checkbox" name="chk2[4][]" value="9">A<br />
<input type="checkbox" name="chk2[4][]" value="12">B<br />
<input type="checkbox" name="chk2[4][]" value="2">C<br />
</div>
Dan in PHP zo uitlezen:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$values = array();
foreach ($_POST['chk1'] as $key) {
$values = array();
if (isset($_POST['chk2'][$key]) {
foreach($_POST['chk2'][$key] as $sub) {
$values[] = '(' . (int)$key . ',' . (int) $value . ')';
}
}
}
if (count($values) > 0) {
$query = "INSERT INTO afdeling_subafdeling(afdeling_id,subafd_id)
VALUES " . implode(',', $values);
mysql_query($query);
}
?>
$values = array();
foreach ($_POST['chk1'] as $key) {
$values = array();
if (isset($_POST['chk2'][$key]) {
foreach($_POST['chk2'][$key] as $sub) {
$values[] = '(' . (int)$key . ',' . (int) $value . ')';
}
}
}
if (count($values) > 0) {
$query = "INSERT INTO afdeling_subafdeling(afdeling_id,subafd_id)
VALUES " . implode(',', $values);
mysql_query($query);
}
?>
Ik krijg een foutmelding wanneer ik deze code toepas.
hij geeft een fout bij deze zin: foreach($_POST['chk2'][$key] as $sub) {.
en het gaat om de '{' teken!
Ger van Steenderen op 30/04/2014 16:19:02: