Formulier werkt niet bij gebruik van AJAX
Per record van de database tabel heb ik een formulier gedefinieerd, waarbij met een submit-knop het aantal kan worden doorgegeven.
In het hoofdscherm werkt dit prima, maar als het AJAX-filter zoek.php gebruikt wordt werkt het niet meer.
Wat doe ik fout?
hoofd.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
83
84
85
86
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
83
84
85
86
<script type="text/javascript">
function showHint(actie,zoek)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","zoek.php?actie="+actie+"&zoek="+zoek,true);
xmlhttp.send();
}
</script>
<center>
<h1>Voorraad</h1>
<?php
$Knop = $_POST["Knop"];
$Actie = $_POST["Actie"];
$db = mysqli_connect('localhost','voorraad','voorraad','voorraad');
if ($Actie == 'Uit') {
$artikel = $_POST["artikel"];
$totaal = $_POST["totaal"];
$aantal = $_POST["aantal"];
if ($totaal >= $aantal and $aantal > 0) {
$sql = "insert into voorraad
(artikel,aantal)
values
('$artikel',$aantal*(-1))";
$result = mysqli_query($db, $sql);
} else {
echo "<script>alert(\"Aantal $aantal is niet goed.\");</script>";
}
}
?>
<br>
<form action='$_SERVER[SCRIPT_NAME]' method='POST' STYLE='margin: 0px; padding: 0px;'>
<table border=1 cellspacing=0 cellpadding=5>
<tr>
<td>Zoek:<td><input type='text' size=20 id='zoek' name='zoek' onkeyup='showHint("uit",this.value)'>
</tr>
</table>
</form>
<span id='txtHint'>
<table border=1 cellspacing=0 cellpadding=5>
<tr><td>Artikel<td>Totaal<td>Aantal</tr>
<?php
$sql = "select count(0) as aantalgevonden from voorraad";
$result = mysqli_query($db, $sql);
$myrow = mysqli_fetch_assoc($result);
$aantalgevonden = $myrow["aantalgevonden"];
if ($aantalgevonden<>0) {
$sql = "select artikel,sum(aantal) as totaal
from voorraad
group by artikel
having sum(aantal)>0
order by artikel";
$result = mysqli_query($db, $sql);
while ($myrow = mysqli_fetch_assoc($result)) {
$artikel = $myrow["artikel"];
$totaal = $myrow["totaal"];
echo "<form action='$_SERVER[SCRIPT_NAME]' method='POST' STYLE='margin: 0px; padding: 0px;'>
<tr>
<td>$artikel<input type='hidden' name='artikel' value='$artikel'>
<td>$totaal<input type='hidden' name='totaal' value='$totaal'>
<td><input type='text' name='aantal' id='aantal' size=5> <input type='submit' value='ok'>
<input type='hidden' name='Knop' value='Uit'>
<input type='hidden' name='Actie' value='Uit'>
</tr>
</form>
";
}
}
?>
</table>
</span>
function showHint(actie,zoek)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","zoek.php?actie="+actie+"&zoek="+zoek,true);
xmlhttp.send();
}
</script>
<center>
<h1>Voorraad</h1>
<?php
$Knop = $_POST["Knop"];
$Actie = $_POST["Actie"];
$db = mysqli_connect('localhost','voorraad','voorraad','voorraad');
if ($Actie == 'Uit') {
$artikel = $_POST["artikel"];
$totaal = $_POST["totaal"];
$aantal = $_POST["aantal"];
if ($totaal >= $aantal and $aantal > 0) {
$sql = "insert into voorraad
(artikel,aantal)
values
('$artikel',$aantal*(-1))";
$result = mysqli_query($db, $sql);
} else {
echo "<script>alert(\"Aantal $aantal is niet goed.\");</script>";
}
}
?>
<br>
<form action='$_SERVER[SCRIPT_NAME]' method='POST' STYLE='margin: 0px; padding: 0px;'>
<table border=1 cellspacing=0 cellpadding=5>
<tr>
<td>Zoek:<td><input type='text' size=20 id='zoek' name='zoek' onkeyup='showHint("uit",this.value)'>
</tr>
</table>
</form>
<span id='txtHint'>
<table border=1 cellspacing=0 cellpadding=5>
<tr><td>Artikel<td>Totaal<td>Aantal</tr>
<?php
$sql = "select count(0) as aantalgevonden from voorraad";
$result = mysqli_query($db, $sql);
$myrow = mysqli_fetch_assoc($result);
$aantalgevonden = $myrow["aantalgevonden"];
if ($aantalgevonden<>0) {
$sql = "select artikel,sum(aantal) as totaal
from voorraad
group by artikel
having sum(aantal)>0
order by artikel";
$result = mysqli_query($db, $sql);
while ($myrow = mysqli_fetch_assoc($result)) {
$artikel = $myrow["artikel"];
$totaal = $myrow["totaal"];
echo "<form action='$_SERVER[SCRIPT_NAME]' method='POST' STYLE='margin: 0px; padding: 0px;'>
<tr>
<td>$artikel<input type='hidden' name='artikel' value='$artikel'>
<td>$totaal<input type='hidden' name='totaal' value='$totaal'>
<td><input type='text' name='aantal' id='aantal' size=5> <input type='submit' value='ok'>
<input type='hidden' name='Knop' value='Uit'>
<input type='hidden' name='Actie' value='Uit'>
</tr>
</form>
";
}
}
?>
</table>
</span>
zoek.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
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
<?php
$actie = $_GET ["actie"];
$zoek = $_GET ["zoek"];
if ($zoek == " ") {
$zoek = "";
}
if ($actie == 'uit') {
echo "<table border=1 cellspacing=0 cellpadding=5>
<tr><td>Artikel<td>Totaal<td>Aantal</tr>";
$db = mysqli_connect('localhost','voorraad','voorraad','voorraad');
$sql = "select artikel,sum(aantal) as totaal
from voorraad
where artikel like '%".$zoek."%'
group by artikel
having sum(aantal)>0
order by artikel";
$result = mysqli_query($db, $sql);
while ($myrow = mysqli_fetch_assoc($result)) {
$artikel = $myrow["artikel"];
$totaal = $myrow["totaal"];
echo "<form name='uit' action='$_SERVER[SCRIPT_NAME]' method='POST' STYLE='margin: 0px; padding: 0px;'>
<tr>
<td>$artikel<input type='hidden' name='artikel' value='$artikel'>
<td>$totaal<input type='hidden' name='totaal' value='$totaal'>
<td><input type='text' name='aantal' id='aantal' size=5> <input type='submit' name='knop' value='Ok'>
</tr>
</form>
";
}
echo "</table>";
}
?>
$actie = $_GET ["actie"];
$zoek = $_GET ["zoek"];
if ($zoek == " ") {
$zoek = "";
}
if ($actie == 'uit') {
echo "<table border=1 cellspacing=0 cellpadding=5>
<tr><td>Artikel<td>Totaal<td>Aantal</tr>";
$db = mysqli_connect('localhost','voorraad','voorraad','voorraad');
$sql = "select artikel,sum(aantal) as totaal
from voorraad
where artikel like '%".$zoek."%'
group by artikel
having sum(aantal)>0
order by artikel";
$result = mysqli_query($db, $sql);
while ($myrow = mysqli_fetch_assoc($result)) {
$artikel = $myrow["artikel"];
$totaal = $myrow["totaal"];
echo "<form name='uit' action='$_SERVER[SCRIPT_NAME]' method='POST' STYLE='margin: 0px; padding: 0px;'>
<tr>
<td>$artikel<input type='hidden' name='artikel' value='$artikel'>
<td>$totaal<input type='hidden' name='totaal' value='$totaal'>
<td><input type='text' name='aantal' id='aantal' size=5> <input type='submit' name='knop' value='Ok'>
</tr>
</form>
";
}
echo "</table>";
}
?>
Er zijn nog geen reacties op dit bericht.