Clicky

How would I go about creating a submit button that send a yes to a database when a checkbox has been ticked? I have inserted a check box into the ContactByEmail field. My script is as follows

Cheers guys
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:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
?php
//php injection protection script
function sanitize($var){
    if(is_array($var)){
        return array_map('sanitize',$var);
    } else {
        if(get_magic_quotes_gpc()){
            $var = stripslashes($var);
        }
        $var = mysql_real_escape_string($var);
        return $var;
    }
}


$date1 = "";
$date2 = "";

$date1 = mysql_real_escape_string($date1);
$date2 = mysql_real_escape_string($date2);

//creates the search criteria - allows user to select data from date ranges 

if (isset($_POST['Submit']))
{
       	$date1 = date('Y-m-d H:i:s', mktime(0,0,0,$_POST['startmonth'],$_POST['startday'],$_POST['startyear']));
		$date2 = date('Y-m-d H:i:s', mktime(23,59,59,$_POST['endmonth'],$_POST['endday'],$_POST['endyear']));
}



$sql = ("SELECT * FROM request
WHERE PostDate BETWEEN '$date1' AND '$date2'");
$result = mysql_query($sql,$con);


$num=mysql_num_rows($result);


//creates the titles for displaying fields within the html data grid table
?>

<table width="1300px" border="0" cellspacing="4" cellpadding="6" class="altrowstable" id="alternatecolor">
<tr valign="top">
<th style="width: auto"><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th style="width: auto"><font face="Arial, Helvetica, sans-serif">Address</font>
<th style="width: auto"><font face="Arial, Helvetica, sans-serif">Email</font></th>
<th style="width: auto"><font face="Arial, Helvetica, sans-serif">Telephone</font></th>
<th style="width: auto"><font face="Arial, Helvetica, sans-serif">Healthcarepro</font></th>
<th style="width: auto"><font face="Arial, Helvetica, sans-serif">Description</font></th>
<th style="width: auto"><font face="Arial, Helvetica, sans-serif">Email</font></th>
<th style="width: auto"><font face="Arial, Helvetica, sans-serif">Phone</font></th>
<th style="width: auto"><font face="Arial, Helvetica, sans-serif">Mail</font></th>
<th style="width: auto"><font face="Arial, Helvetica, sans-serif">Local Date</font></th>

</tr>

<?php



$i=0;
while ($i < $num) {
//pulls data from MySQL database 
$f1=mysql_result($result,$i,"Title");
$f2=mysql_result($result,$i,"FirstName");
$f3=mysql_result($result,$i,"Surname");
$f4=mysql_result($result,$i,"Address1");
$f5=mysql_result($result,$i,"Address2");
$f6=mysql_result($result,$i,"City");
$f7=mysql_result($result,$i,"Country");
$f8=mysql_result($result,$i,"PostCode");
$f9=mysql_result($result,$i,"Email");
$f10=mysql_result($result,$i,"Telephone");
$f11=mysql_result($result,$i,"HealthCarePro");

$f13=mysql_result($result,$i,"Description");
$f14=mysql_result($result,$i,"ContactByEmail");
$f15=mysql_result($result,$i,"ContactByPhone");
$f16=mysql_result($result,$i,"ContactByMail");
$f17=mysql_result($result,$i,"LocalDate");

//writes the pulled data into the html datagrid ......
?>

<tr valign="top">
<td style="width: auto"><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
<font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
<font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td style="width: auto"><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font><br />
<font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font><br />
<font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font><br />
<font face="Arial, Helvetica, sans-serif"><?php echo $f7; ?></font><br />
<font face="Arial, Helvetica, sans-serif"><?php echo $f8; ?></font></td>
<td style="width: auto"><font face="Arial, Helvetica, sans-serif"><?php echo $f9; ?></font></td>
<td style="width: auto"><font face="Arial, Helvetica, sans-serif"><?php echo $f10; ?></font></td>
<td style="width: auto"><font face="Arial, Helvetica, sans-serif"><?php echo $f11; ?></font></td>

<td style="width: auto"><font face="Arial, Helvetica, sans-serif"><?php echo $f13; ?></font></td>
<td style="width: auto"><font face="Arial, Helvetica, sans-serif"><?php if ($f14=="Yes"){echo "checked";} ?>><input type="checkbox" name="chk1" value="ContactByMail"></font></td>
<td style="width: auto"><font face="Arial, Helvetica, sans-serif"><?php echo $f15; ?></font></td>
<td style="width: auto"><font face="Arial, Helvetica, sans-serif"><?php echo $f16; ?></font></td>
<td style="width: auto"><font face="Arial, Helvetica, sans-serif"><?php echo $f17; ?></font></td>


</tr>


<?php
$i++;
}
?>
</table>
<br />

<form name="frm1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table width="800" border="0" cellspacing="1" cellpadding="3">
  <tr>
    <td colspan="2">Select date - please fill in all fields</td>
    </tr>
  <tr align="left">
    <td width="100" align="left">Start Day:<select name="startday" size="1">
        <option>Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
        <option value="16">16</option>
        <option value="17">17</option>
        <option value="18">18</option>
        <option value="19">19</option>
        <option value="20">20</option>
        <option value="21">21</option>
        <option value="22">22</option>
        <option value="23">23</option>
        <option value="24">24</option>
        <option value="25">25</option>
        <option value="26">26</option>
		<option value="27">27</option>
        <option value="28">28</option>
        <option value="29">29</option>
        <option value="30">30</option>
        <option value="31">31</option>
        
      </select></td>
    <td width="100" align="left"> Start Month:<select name="startmonth" size="1">
        <option>Select</option>
        <option value="01">01</option>
        <option value="02">02</option>
        <option value="03">03</option>
        <option value="04">04</option>
        <option value="05">05</option>
        <option value="06">06</option>
        <option value="07">07</option>
		<option value="08">08</option>
        <option value="09">09</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
      	</select>   </td>
     <td width="100" align="left"> Start Year:<select name="startyear" size="1">
        <option>Select</option>
        <option value="2011">2011</option>
		<option value="2012">2012</option>
      </select>   </td>
      <td width="200" align="left"></td>
	<td width="100" align="left"> End Day:<select name="endday" size="1">
        <option>Select</option>
           <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
        <option value="16">16</option>
        <option value="17">17</option>
        <option value="18">18</option>
        <option value="19">19</option>
        <option value="20">20</option>
        <option value="21">21</option>
        <option value="22">22</option>
        <option value="23">23</option>
        <option value="24">24</option>
        <option value="25">25</option>
        <option value="26">26</option>
		<option value="27">27</option>
        <option value="27">28</option>
        <option value="27">29</option>
        <option value="27">30</option>
        <option value="27">31</option>
      </select>   </td>
     <td width="100" align="left"> End Month:<select name="endmonth" size="1">
        <option>Select</option>
        <option value="01">01</option>
        <option value="02">02</option>
        <option value="03">03</option>
        <option value="04">04</option>
        <option value="05">05</option>
        <option value="06">06</option>
        <option value="07">07</option>
		<option value="08">08</option>
        <option value="09">09</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
      	</select>   </td>
 	<td width="100" align="left"> End Year:<select name="endyear" size="1">
        <option>Select</option>
        <option value="2011">2011</option>
		<option value="2012">2012</option>
     	</select>   </td>
  </tr>
  <tr>
    <td colspan="7" align="center"><input type="submit" name="Submit" value="go"></td>
  </tr>
  <tr>
    <td colspan="2" align="left" valign="middle">&nbsp;</td>
  </tr>
</table>
</form>
<FORM>
<INPUT TYPE="BUTTON" VALUE="EXCEL" ONCLICK="window.location.href='http://localhost/PHP_SQL_to_excel.php'">
</FORM>
</td>
<td width="20%"></td>
</tr>
</table>

asked 08/31/2011 11:12

skyscream's gravatar image

skyscream ♦♦


2 Answers:
Send the database $_REQUEST['ContactByEmail'], should work
link

answered

jnfsmile's gravatar image

jnfsmile

Assumptions:

1. the checkbox fields are defined in mysql something like this:

ContactByMail enum('Yes', 'No') DEFAULT 'No' NOT NULL

2.  the update of the data requires knowing which record to update.   Perhaps there is an ID in the request table.

I'll assume you have a unique ID in that table called ID

Line 65 insert:  $f0=mysql_result($result,$i,"ID");
Line 116 needs to go before line 62 so that the checkboxes are inside the form to be submitted.
Line 100 becomes:
      <input type="checkbox" name="chk1[]" value="<?php  echo $f0; ?>">


Style point:  Rather than number of rows being used as a loop or setting all the $f## variables you could reference each field as you use it by name with an associative array ($ass is the associative array below which is referenced by the field name)

while ($ass = mysql_fetch_assoc($result)) {
    ....
    echo '<INPUT type=checkbox name=chk1[] value=',$ass{'ID'},'> ';
    ....
}
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
if (isset($_POST['Submit'])){
	if (isset($_POST['chk1'])) {
		echo "<PRE>";
		var_dump($_POST['chk1']); // shows you which boxes are checked
		echo "</PRE>";
		foreach($_POST['chk1'] as $checkedID) {
			//$sql = "UPDATE request SET ContactByMail='Yes' WHERE ID='".$checkedID."'";
			//mysql_query($sql);
		}
	}
}
link

answered 2011-08-31 at 11:27:27

ropenner's gravatar image

ropenner

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×2

Asked: 08/31/2011 11:12

Seen: 321 times

Last updated: 09/01/2011 10:20