Вот кстати готовое решение,
абсолютно рабочее, в котором используются почти все элементы программирования, нет фреймворков - они пока вам не нужны, и нет работы с базой, но оно тут не нужно.
1. Можно выбирать поля для заполнения
2. всплывает подсказка о не заполненном поле
3. пока все не заполнят - кнопка отправить не активна
4. сами увидите, но вариант не на сдачу контрольной, сильно круто для студента с месячным сроком обучения
Вот коды - разбирайтесь, вроде все описано, на вопросы ответим.
Файл с названием "contact_form.php"
Кликните здесь для просмотра всего текста
| 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
| <?php
$website_logo = 'http://'.$_SERVER['SERVER_NAME'].'/favicon.ico'; // если не иконки, тогда указать вручную полный путь на /favicon.ico. Пример: "http://ваш_сайт/favicon.ico"
// Настройки - какие поля отображать //
// 0 - disable; 1 - enable;
$FirstName = 1;
$Surname = 1;
$Gender = 1;
$Age = 1;
$MobilePhoneNumber = 1;
$AreaCode = 1;
$PhoneNumber = 1;
$Address = 1;
$State = 1;
$Country = 1;
$PostCode = 1;
?>
<!-- // Стили можно вынести в отдельный файл "style.css" и подключить <link href="/style.css" rel="stylesheet" type="text/css" /> //-->
<style>
.website_logo {
left: 15px;
top: 13px;
width: 32px;
position: absolute
}
.cont_form_head {
width:400px;
text-align:left;
border:1px solid #CCCCCC;
color:#003300;
font:14px tahoma, verdana;
background:#F5F5F5;
margin: 0 auto
}
.cont_form_head_mid {
position: relative;
text-align:center;
background:#708090;
border:1px solid #F5F5F5;
color:#FFFFB5;
padding:0
}
.cont_form1_1 {
padding:8px 20px 0px 20px;
border-top:solid 1px #CCCCCC
}
.cont_form1 {
padding:8px 20px 0px 20px
}
.cont_form2 {
padding:1px 20px 0px 20px
}
.cont_form3 {
width:358px;
height:20px;
background:#F5F5F5;
border:1px solid #CCCCCC;
color:#999
}
.cont_form_sub {
padding:5px;
text-align:center
}
.novalid {
display:none;
text-align: center;
background:#FFCFCF;
margin: 0 20px;
padding: 3px
}
input:enabled.submit {
font-weight: bold;
padding: 3px 20px
}
</style>
<!-- // javascript можно вынести в отдельный файл "script.js" и подключить <script src="/script.js" type="text/javascript"></script> //-->
<script type="text/javascript">
function validate_form(form) {
<? if ($FirstName == true): ?>
if (form.name_field.value == 'Enter Your First Name Here' || form.name_field.value == '') {
document.getElementById('FirstName').style.display="block";
document.getElementById('FirstName2').style.display="none";
form.name_field.focus();
return false;
} else {
document.getElementById('FirstName').style.display="none";
document.getElementById('FirstName2').style.display="block";
}
<? endif; ?>
<? if ($Surname == true): ?>
if (form.surname_field.value == 'Enter Your Surname Here' || form.surname_field.value == '') {
document.getElementById('Surname').style.display="block";
document.getElementById('Surname2').style.display="none";
form.surname_field.focus();
return false;
} else {
document.getElementById('Surname').style.display="none";
document.getElementById('Surname2').style.display="block";
}
<? endif; ?>
<? if ($Gender == true): ?>
if (form.sex[0].checked == '' && form.sex[1].checked == '') {
document.getElementById('Gender').style.display="block";
document.getElementById('Gender2').style.display="none";
return false;
} else {
document.getElementById('Gender').style.display="none";
document.getElementById('Gender2').style.display="block";
}
<? endif; ?>
<? if ($Age == true): ?>
if (form.age_field.value == 'Enter Your Age' || form.age_field.value == '') {
document.getElementById('Age').style.display="block";
document.getElementById('Age2').style.display="none";
form.age_field.focus();
return false;
} else {
document.getElementById('Age').style.display="none";
document.getElementById('Age2').style.display="block";
}
<? endif; ?>
<? if ($MobilePhoneNumber == true): ?>
if (form.mobile_field.value == 'Enter Your Mobile Phone Number Here' || form.mobile_field.value == '') {
document.getElementById('MobilePhoneNumber').style.display="block";
document.getElementById('MobilePhoneNumber2').style.display="none";
form.mobile_field.focus();
return false;
} else {
document.getElementById('MobilePhoneNumber').style.display="none";
document.getElementById('MobilePhoneNumber2').style.display="block";
}
<? endif; ?>
<? if ($AreaCode == true): ?>
if (form.area_field.value == 'Enter Your Area Code Here' || form.area_field.value == '') {
document.getElementById('AreaCode').style.display="block";
document.getElementById('AreaCode2').style.display="none";
form.area_field.focus();
return false;
} else {
document.getElementById('AreaCode').style.display="none";
document.getElementById('AreaCode2').style.display="block";
}
<? endif; ?>
<? if ($PhoneNumber == true): ?>
if (form.phone_field.value == 'Enter Your Phone Number Here' || form.phone_field.value == '') {
document.getElementById('PhoneNumber').style.display="block";
document.getElementById('PhoneNumber2').style.display="none";
form.phone_field.focus();
return false;
} else {
document.getElementById('PhoneNumber').style.display="none";
document.getElementById('PhoneNumber2').style.display="block";
}
<? endif; ?>
<? if ($Address == true): ?>
if (form.address_field.value == 'Enter Your Address Here' || form.address_field.value == '') {
document.getElementById('Address').style.display="block";
document.getElementById('Address2').style.display="none";
form.address_field.focus();
return false;
} else {
document.getElementById('Address').style.display="none";
document.getElementById('Address2').style.display="block";
}
<? endif; ?>
<? if ($State == true): ?>
if (form.state_field.value == 'Enter Your State Here' || form.state_field.value == '') {
document.getElementById('State').style.display="block";
document.getElementById('State2').style.display="none";
form.state_field.focus();
return false;
} else {
document.getElementById('State').style.display="none";
document.getElementById('State2').style.display="block";
}
<? endif; ?>
<? if ($Country == true): ?>
if(form.country_field.options[form.country_field.selectedIndex].value == 0) {
document.getElementById('Country').style.display="block";
document.getElementById('Country2').style.display="none";
form.country_field.focus();
return false;
} else {
document.getElementById('Country').style.display="none";
document.getElementById('Country2').style.display="block";
}
<? endif; ?>
<? if ($PostCode == true): ?>
if (form.post_field.value == 'Enter Your Post Code' || form.post_field.value == '') {
document.getElementById('PostCode').style.display="block";
document.getElementById('PostCode2').style.display="none";
form.post_field.focus();
return false;
} else {
document.getElementById('PostCode').style.display="none";
document.getElementById('PostCode2').style.display="block";
}
<? endif; ?>
document.getElementById('agree').disabled = 0;
return true;
}
// Проверка ввода в поле только цифр
function check(input){
input.value=input.value.replace(/[^\d,]/g,'');
}
// Предупреждение о правильно заполненных данных, если заполнены не все поля - кнопка отправить не активна!
function agreeForm(f) {
if (f.agree.checked && validate_form(contact_form)) {
alert('Проверьте ваши данные перед отправкой,\nчтобы мы могли с вами связаться!');
f.submit.disabled = 0;
} else {
f.submit.disabled = 1;
}
}
</script>
<!-- // Сама форма на странице // -->
<div class="cont_form_head">
<form name="contact_form" action="sendmail.php" method="post" onsubmit="return validate_form(this);">
<div class="cont_form_head_mid"><img class="website_logo" src="<?=$website_logo?>" alt="Logo website"><h2>CONTACT FORM</h2></div>
<? if ($FirstName == true): ?>
<div class="cont_form1_1" id="FirstName2" style="display:block;">First Name:</div> <!-- // Название поля в форме -->
<div id="FirstName" class="novalid">Пожалуйста, укажите First Name</div> <!-- // Всплывающее предупреждение о незаполненом поле -->
<div class="cont_form2">
<input name="name_field" type="text" value="Enter Your First Name Here" class="cont_form3" maxlength="15"
onfocus="if(this.value=='Enter Your First Name Here') this.value=''; this.style.color='#000';"
onblur="if(this.value=='') { this.value='Enter Your First Name Here'; this.style.color='#999'; } else { this.style.color='#000'; } return validate_form(contact_form);" />
</div>
<? endif; ?>
<? if ($Surname == true): ?>
<div class="cont_form1" id="Surname2" style="display:block;">Surname:</div>
<div id="Surname" class="novalid">Пожалуйста, укажите Surname</div>
<div class="cont_form2">
<input name="surname_field" type="text" value="Enter Your Surname Here" class="cont_form3" maxlength="15"
onfocus="if(this.value=='Enter Your Surname Here') this.value=''; this.style.color='#000';"
onblur="if(this.value=='') { this.value='Enter Your Surname Here'; this.style.color='#999'; } else { this.style.color='#000'; } return validate_form(contact_form);" />
</div>
<? endif; ?>
<? if ($Gender == true): ?>
<div class="cont_form1" id="Gender2" style="display:block;">Gender:</div>
<div id="Gender" class="novalid">Пожалуйста, укажите Gender</div>
<div class="cont_form2">
<label><input type="radio" name="sex" value="male" onchange="return validate_form(contact_form);"/>Male</label>
<label><input type="radio" name="sex" value="female" onchange="return validate_form(contact_form);"/>Female</label>
</div>
<? endif; ?>
<? if ($Age == true): ?>
<div class="cont_form1" id="Age2" style="display:block;">Age:</div>
<div id="Age" class="novalid">Пожалуйста, укажите Age</div>
<div class="cont_form2">
<input name="age_field" type="text" value="Enter Your Age" onkeyup="return check(this);" class="cont_form3" maxlength="2"
onfocus="if(this.value=='Enter Your Age') this.value=''; this.style.color='#000';"
onblur="if(this.value=='') { this.value='Enter Your Age'; this.style.color='#999'; } else { this.style.color='#000'; } return validate_form(contact_form);" />
</div>
<? endif; ?>
<? if ($MobilePhoneNumber == true): ?>
<div class="cont_form1" id="MobilePhoneNumber2" style="display:block;">Mobile Phone Number:</div>
<div id="MobilePhoneNumber" class="novalid">Пожалуйста, укажите Mobile Phone Number</div>
<div class="cont_form2">
<input name="mobile_field" type="text" value="Enter Your Mobile Phone Number Here" onkeyup="return check(this);" class="cont_form3" maxlength="15"
onfocus="if(this.value=='Enter Your Mobile Phone Number Here') this.value=''; this.style.color='#000';"
onblur="if(this.value=='') { this.value='Enter Your Mobile Phone Number Here'; this.style.color='#999'; } else { this.style.color='#000'; } return validate_form(contact_form);" />
</div>
<? endif; ?>
<? if ($AreaCode == true): ?>
<div class="cont_form1" id="AreaCode2" style="display:block;">Area Code:</div>
<div id="AreaCode" class="novalid">Пожалуйста, укажите Area Code</div>
<div class="cont_form2">
<input name="area_field" type="text" value="Enter Your Area Code Here" onkeyup="return check(this);" class="cont_form3" maxlength="3"
onfocus="if(this.value=='Enter Your Area Code Here') this.value=''; this.style.color='#000';"
onblur="if(this.value=='') { this.value='Enter Your Area Code Here'; this.style.color='#999'; } else { this.style.color='#000'; } return validate_form(contact_form);" />
</div>
<? endif; ?>
<? if ($PhoneNumber == true): ?>
<div class="cont_form1" id="PhoneNumber2" style="display:block;">Phone Number:</div>
<div id="PhoneNumber" class="novalid">Пожалуйста, укажите Phone Number</div>
<div class="cont_form2">
<input name="phone_field" type="text" value="Enter Your Phone Number Here" onkeyup="return check(this);" class="cont_form3" maxlength="15"
onfocus="if(this.value=='Enter Your Phone Number Here') this.value=''; this.style.color='#000';"
onblur="if(this.value=='') { this.value='Enter Your Phone Number Here'; this.style.color='#999'; } else { this.style.color='#000'; } return validate_form(contact_form);" />
</div>
<? endif; ?>
<? if ($Address == true): ?>
<div class="cont_form1" id="Address2" style="display:block;">Address:</div>
<div id="Address" class="novalid">Пожалуйста, укажите Address</div>
<div class="cont_form2">
<input name="address_field" type="text" value="Enter Your Address Here" class="cont_form3" maxlength="100"
onfocus="if(this.value=='Enter Your Address Here') this.value=''; this.style.color='#000';"
onblur="if(this.value=='') { this.value='Enter Your Address Here'; this.style.color='#999'; } else { this.style.color='#000'; } return validate_form(contact_form);" />
</div>
<? endif; ?>
<? if ($State == true): ?>
<div class="cont_form1" id="State2" style="display:block;">State:</div>
<div id="State" class="novalid">Пожалуйста, укажите State</div>
<div class="cont_form2">
<input name="state_field" type="text" value="Enter Your State Here" class="cont_form3" maxlength="15"
onfocus="if(this.value=='Enter Your State Here') this.value=''; this.style.color='#000';"
onblur="if(this.value=='') { this.value='Enter Your State Here'; this.style.color='#999'; } else { this.style.color='#000'; } return validate_form(contact_form);" />
</div>
<? endif; ?>
<? if ($Country == true): ?>
<div class="cont_form1" id="Country2" style="display:block;">Country:</div>
<div id="Country" class="novalid">Пожалуйста, укажите Country</div>
<div class="cont_form2">
<select name="country_field" onchange="return validate_form(contact_form);">
<option value="">Select Country</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Australia">Australia</option>
<option value="Ukraine">Ukraine</option>
</select>
</div>
<? endif; ?>
<? if ($PostCode == true): ?>
<div class="cont_form1" id="PostCode2" style="display:block;">Post Code:</div>
<div id="PostCode" class="novalid">Пожалуйста, укажите Post Code</div>
<div class="cont_form2">
<input name="post_field" type="text" value="Enter Your Post Code" onkeyup="return check(this);" class="cont_form3" maxlength="6"
onfocus="if(this.value=='Enter Your Post Code') this.value=''; this.style.color='#000';"
onblur="if(this.value=='') { this.value='Enter Your Post Code'; this.style.color='#999'; } else { this.style.color='#000'; } return validate_form(contact_form);" />
</div>
<? endif; ?>
<div class="cont_form2">
<label><input type="checkbox" id="agree" name="agree" onclick="validate_form(contact_form);agreeForm(this.form);" disabled />Я согласен отправить данные</label>
</div>
<div class="cont_form_sub">
<input type="submit" name="submit" class="submit" value="Send" disabled />
</div>
</form>
</div> |
|
Файл с названием "form.php"
Кликните здесь для просмотра всего текста
| PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Send Form</title>
</head>
<body>
<?php
include('contact_form.php');
?>
</body>
</html> |
|
и последний с названием "sendmail.php"
Кликните здесь для просмотра всего текста
| 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
| <?php
header("Content-Type: text/html; charset=UTF-8");
$to = 'programmist@alexgames.x10.mx'; //куда отправлять почту?
$from = "testscript@alexgames.x10.mx"; //адрес, от которого придёт уведомление - обязательно существующий почтовый ящик, желательно на сервере
$title='message from www.alexgames.x10.mx'; // подпись сообщения в почтовом ящике
$website_logo = 'http://'.$_SERVER['SERVER_NAME'].'/favicon.ico'; // если нет иконки, тогда указать вручную полный путь на /favicon.ico. Пример: "http://ваш_сайт/favicon.ico"
$ip = $_SERVER['REMOTE_ADDR']; // IP-адрес с которого отправили письмо
$name=$_POST['name_field'];
$fio=$_POST['surname_field'];
$sex=$_POST['sex'];
$email=$_POST['age_field'];
$tel=$_POST['mobile_field'];
$text=$_POST['area_field'];
$phone=$_POST['phone_field'];
$address=$_POST['address_field'];
$state=$_POST['state_field'];
$country=$_POST['country_field'];
$postcode=$_POST['post_field'];
$logo = '<img src="'.$website_logo.'" alt="Logo website">';
$headers = "Content-type: text/html; charset=UTF-8 \r\n";
$headers .= "From: alexgames <".$from.">\r\n";
$mess = '<html><body>';
$mess .= '<table rules="all" style="border-color: #666; margin:0 auto; width: 90%; border-collapse: separate;" cellpadding="8">';
$mess .= "<tr style='background: #FCFFDB;'><td colspan='2'>".$logo."</td></tr>";
if (isset($name)) {
$mess .= "<tr style='background: #eee;'><td><strong>name:</strong></td><td><strong>".strip_tags($name)."</strong></td></tr>";
}
if (isset($fio)) {
$mess .= "<tr><td style='width: 190px;'><strong>fio:</strong></td><td><strong>".strip_tags($fio)."</strong></td></tr>";
}
if (isset($sex)) {
$mess .= "<tr><td style='width: 190px;'><strong>sex:</strong></td><td><strong>".strip_tags($sex)."</strong></td></tr>";
}
if (isset($email)) {
$mess .= "<tr style='background: #eee;'><td><strong>email:</strong></td><td><strong>".strip_tags($email)."</strong></td></tr>";
}
if (isset($tel)) {
$mess .= "<tr><td style='width: 190px;'><strong>tel:</strong></td><td><strong>".strip_tags($tel)."</strong></td></tr>";
}
if (isset($text)) {
$mess .= "<tr style='background: #eee;'><td><strong>text:</strong></td><td><strong>".strip_tags($text)."</strong></td></tr>";
}
if (isset($phone)) {
$mess .= "<tr><td style='width: 190px;'><strong>phone:</strong></td><td><strong>".strip_tags($phone)."</strong></td></tr>";
}
if (isset($address)) {
$mess .= "<tr style='background: #eee;'><td><strong>address:</strong></td><td><strong>".strip_tags($address)."</strong></td></tr>";
}
if (isset($state)) {
$mess .= "<tr><td style='width: 190px;'><strong>state:</strong></td><td><strong>".strip_tags($state)."</strong></td></tr>";
}
if (isset($country)) {
$mess .= "<tr style='background: #eee;'><td><strong>country:</strong></td><td><strong>".strip_tags($country)."</strong></td></tr>";
}
if (isset($postcode)) {
$mess .= "<tr><td style='width: 190px;'><strong>postcode:</strong></td><td><strong>".strip_tags($postcode)."</strong></td></tr>";
}
$mess .= "<tr style='background: #FCFFDB; text-align: center;'><td colspan='2'><i>Отправлено с IP-адреса: ".$ip."</i></td></tr>";
$mess .= "</table>";
$mess .= "</body></html>";
$mail_send = @mail($to, $title, $mess, $headers);
if($mail_send) {
//header('Refresh: 5; URL="http://alexgames.x10.mx"');
header('Refresh: 5; URL='.$_SERVER['HTTP_REFERER'].'');
echo '<h3>Mail sent OK!</h3>';
} else {
echo '<h3>Error Mail sent</h3>';
}
?> |
|
Чтоб запустить, положите всё это в корень сайта по FTP, и в адресной строке укажите: http: //вашсайт.au/form.php