· 7 years ago · Sep 26, 2018, 09:06 PM
1### PHP test code for generating values using a checkbox - Works as intended ###
2
3<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
4 <label for="sam">Sam: <input type="checkbox" name="Sam" id="sam" value="1"/></label><br />
5 <label for="john">John: <input type="checkbox" name="John" id="john" value="1"/></label><br />
6 <input name="submit" type="submit" value="submit" />
7 </form>
8
9
10<?php
11function test_input($data)
12{
13 $data = trim($data);
14 $data = stripslashes($data);
15 $data = htmlspecialchars($data);
16 return $data;
17}
18
19if ($_SERVER["REQUEST_METHOD"] == "POST")
20{
21 if (isset($_POST["submit"]))
22 {
23 $name1 = test_input($_POST["Sam"]);
24 if ($name1 != "1")
25 {
26 $name1 = "0";
27 }
28
29 $name2 = test_input($_POST["John"]);
30 if ($name2 != "1")
31 {
32 $name2 = "0";
33 }
34 }
35
36 echo "name1 = ". $name1 . "<br/>";
37 echo "name2 = ". $name2 . "<br/>";
38}
39?>
40
41############# end of PHP test file ###################################
42
43######## beginning of PHP file test code to populate a list of mysql job records and whether active with a checkbox #####
44
45
46
47<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
48<html xmlns="http://www.w3.org/1999/xhtml">
49<head>
50<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
51<title>Untitled Document</title>
52<link href="oneColLiqCtr.css" rel="stylesheet" type="text/css" />
53</head>
54
55<body>
56
57<div class="container">
58 <div class="content">
59 <h1>Add a Job Name</h1>
60<p><a href="index.php">Administration Menu</a></p>
61
62 <?php
63// Enter New Employee script
64function test_input($data) {
65 $data = trim($data);
66 $data = stripslashes($data);
67 $data = htmlspecialchars($data);
68 return $data;
69}
70
71
72if ($_SERVER["REQUEST_METHOD"] == "POST") {
73
74
75
76
77
78
79if (isset($_POST['createjob'])) {
80
81$newJobName = test_input($_POST["newJobName"]);
82$activeJob = test_input($_POST["activejob"]);
83
84if ($activeJob != 1)
85{
86 $activeJob = 0;
87}
88
89$servername = "localhost";
90$username = "terry";
91$password = "kashmir";
92$dbname = "westcal-edm-test";
93
94// Create connection
95$conn = new mysqli($servername, $username, $password, $dbname);
96// Check connection
97 if ($conn->connect_error) {
98 die("Connection failed: " . $conn->connect_error);
99}
100
101
102if (!empty($newJobName)){
103$sql = "INSERT INTO Jobs (Jobscol, activejob) VALUES ('$newJobName', '$activeJob')";
104
105} else{
106 die("Fields are empty!! Please populate them.<br/>");
107}
108
109if ($conn->query($sql) === TRUE) {
110 echo "New record created successfully<br/>";
111} else {
112 echo "Error: " . $sql . "<br>" . $conn->error;
113}
114
115echo "<p> The value of newJobName = " . $newJobName . "</p>";
116echo "<p> The value of activejob = " . $activeJob . "</p>";
117
118$servername = "localhost";
119$username = "terry";
120$password = "kashmir";
121$dbname = "westcal-edm-test";
122
123// Create connection
124$conn1 = new mysqli($servername, $username, $password, $dbname);
125
126// Check connection
127 if ($conn1->connect_error) {
128 die("Connection failed: " . $conn1->connect_error);
129}
130echo "See if your job exists in the drop down. If not, add it. <br/>";
131
132$sql1 = "SELECT * FROM Jobs";
133$result1 = $conn1->query($sql1);
134echo '<form method="post">';
135echo "<table width ='100%'>";
136echo "<tr>";
137echo "<td><strong>Job Name</strong></td>";
138echo "<td><strong>Active</strong></td>";
139echo "</tr>";
140
141if ($result1->num_rows > 0)
142{
143 echo "Job Names:<br/>";
144
145
146
147
148 while ($row1 = $result1->fetch_assoc())
149 {
150
151 // echo "<option value='".$row1[Jobscol]."'>".$row1[Jobscol]."</option>";
152 echo "<tr>";
153 echo "<td>".$row1[Jobscol]."</td>";
154 echo "<td>";
155 echo $row1[activejob];
156 if ($row1[activejob] == 1)
157 {
158
159 echo '<input type="checkbox" name="activejob" value="1" checked />';
160 } else
161 {
162
163 echo '<input type="checkbox" name="activejob" value="1" />';
164 }
165 echo "</td>";
166 echo "<td> <input type='submit' name='submit' value='Update'/> </td>";
167 echo "</tr>";
168
169
170
171 }
172} else
173{
174 echo "0 results";
175}
176
177echo "</table>";
178
179} else{ // end isset
180echo "Shit fucked up!!!<br/>";
181}
182} else{ // Else after post checked
183
184$servername = "localhost";
185$username = "terry";
186$password = "kashmir";
187$dbname = "westcal-edm-test";
188
189// Create connection1
190$conn2 = new mysqli($servername, $username, $password, $dbname);
191
192// Check conn1tion
193 if ($conn2->connect_error) {
194 die("Connection failed: " . $conn1->connect_error);
195}
196echo "See if your job exists in the drop down. If not, add it. <br/>";
197
198$sql1 = "SELECT * FROM Jobs";
199$result1 = $conn2->query($sql1);
200echo '<form method="post">';
201echo "<table width='100%'>";
202echo "<tr>";
203echo "<td><stron>Job Name</strong></td>";
204echo "<td><stron>Active</strong></td>";
205echo "</tr>";
206
207if ($result1->num_rows > 0)
208{
209 echo "Job Names:<br/>";
210
211
212
213
214 while ($row1 = $result1->fetch_assoc())
215 {
216
217 // echo "<option value='".$row1[Jobscol]."'>".$row1[Jobscol]."</option>";
218 echo "<tr>";
219 echo "<td>".$row1[Jobscol]."</td>";
220 echo "<td>";
221 echo $row1[activejob];
222 if ($row1[activejob] == 1)
223 {
224 echo '<input type="checkbox" name="activejob" value="1" checked/>';
225
226 } else
227 {
228 echo '<input type="checkbox" name="activejob" value="1" />';
229
230 }
231 echo "</td>";
232 echo "</tr>";
233
234
235
236 }
237} else
238{
239 echo "0 results";
240}
241
242echo "</table>";
243?>
244
245
246
247
248
249 <table width="100%">
250
251
252 <tr>
253 <td> </td>
254 <td>Job Name to Add:</td>
255 <td><input width="100%" name="newJobName" type="text" value=""></td>
256 <td>Active: <input type="checkbox" name="activejob" value="1"/></td>
257 </tr>
258
259 <tr>
260 <td> </td>
261 <td><input name="createjob" type="submit" value="Submit"></td>
262 <td> </td>
263 <td> </td>
264 </tr>
265 </table>
266 </form>
267
268 <p> </p>
269<?php
270}
271
272?>
273
274 <p> </p>
275 <!-- end .content --></div>
276 <!-- end .container --></div>
277</body>
278</html>
279
280
281########## end oh PHP mysql test file ################################