FORM

closed account (GLNARXSz)
Write your question here.

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
 

 <?php 
	$errors = array();
	$defaults = array('jmeno' => '', 'prijmeni' => '', 'pohlavi' => 'muž', 'stat' => 'RUS', 'vaha' => 70, 'odvetvi' => array(1=>'cyklistika',3=>'plavání'));
	$data = isset($_POST['submit']) ? $_POST : $defaults;


	if (isset($_POST['submit'])) {
		//var_dump($data);
		if (!preg_match("/^[A-Z].*/",$data['jmeno'])) $errors['jmeno'] = "Neplatné jméno";
		if (strlen($data['prijmeni'])<5) $errors['prijmeni'] = "Neplatné příjmení";
		if (empty($data['stat'])) $errors['stat'] = "Nezadán stát";
		if ($data['vaha'] < 30) $errors['vaha'] = "Příliš nízká váha";
		if ($data['vaha'] > 200) $errors['vaha'] = "Příliš vysoká váha";

		if (!$errors) {
			$sql = "INSERT INTO prihlaska (jmeno,prijmeni,pohlavi,stat,vaha,odvetvi) values (?,?,?,?,?,?)";
			unset($data['submit']);
			$data['odvetvi'] = implode(',', $data['odvetvi']);
			$parameters = array_values($data);
			if (Database::query($sql,$parameters)) {
				echo "Formulář byl úspěšně odeslán";
			} else {
				echo "Došlo k chybě";
			}
		}
	}



	if (!isset($_POST['submit']) || $errors):
 ?>


<form action="" method="POST" class="form-horizontal" role="form">
	<fieldset>
		<div class="form-group">
			<legend>Přihláška na olympiádu</legend>
		</div>
		<div class="form-group">
			<label for="jmeno" class="col-sm-4 control-label">Jméno</label>
			<div class="col-sm-4">
				<input type="text" name="jmeno" id="jmeno" class="form-control" required="required" title="Jméno" placeholder="Zadej jméno" value="<?php echo $data['jmeno']; ?>" >
			</div>
			<div class="col-sm-4 text-danger">
				<?php  if (isset($errors['jmeno'])) echo $errors['jmeno']; ?>
			</div>
		</div>
		<div class="form-group">
			<label for="prijmeni" class="col-sm-4 control-label">Příjmení</label>
			<div class="col-sm-4">
				<input type="text" name="prijmeni" id="prijmeni" class="form-control" required="required" title="Příjmení" placeholder="Zadej příjmení" value="<?php echo $data['prijmeni']; ?>" >
			</div>
			<div class="col-sm-4 text-danger">
				<?php  if (isset($errors['prijmeni'])) echo $errors['prijmeni']; ?>
			</div>
		</div>

		<div class="form-group">
			<label for="" class="col-sm-4 control-label">Pohlaví</label>
			<div class="col-sm-4 radio">
				<label class="radio-inline">
					<input type="radio" name="pohlavi" id="muz" value="muž" <?php if($data['pohlavi'] == 'muž') echo "checked"; ?>>
					Muž
				</label>
				<label class="radio-inline">
					<input type="radio" name="pohlavi" id="zena" value="žena" <?php if($data['pohlavi'] == 'žena') echo "checked"; ?>>
					Žena
				</label>
			</div>
			<div class="col-sm-4 text-danger">
				<?php  if (isset($errors['pohlavi'])) echo $errors['pohlavi']; ?>
			</div>
		</div>

		<div class="form-group">
			<label for="" class="col-sm-4 control-label">Stát</label>
			<div class="col-sm-4">
			<select name="stat" id="stat" class="form-control">
				<option value="CZE" <?php if($data['stat'] == 'CZE') echo "selected"; ?>>Česká republika</option>
				<option value="GER" <?php if($data['stat'] == 'GER') echo "selected"; ?>>Německo</option>
				<option value="RUS" <?php if($data['stat'] == 'RUS') echo "selected"; ?>>Rusko</option>
				<option value="USA" <?php if($data['stat'] == 'USA') echo "selected"; ?>>Spojené státy</option>
			</select>
			</div>
			<div class="col-sm-4 text-danger">
				<?php  if (isset($errors['stat'])) echo $errors['stat']; ?>
			</div>
		</div>

		<div class="form-group">
			<label for="vaha" class="col-sm-4 control-label">Váha</label>
			<div class="col-sm-2">
				<input type="number" name="vaha" id="vaha" class="form-control" title="Váha" placeholder="70" min="30" max="300" value="<?php echo $data['vaha']; ?>" >
			</div>
			<div class="col-sm-4  col-sm-offset-2 text-danger">
				<?php  if (isset($errors['vaha'])) echo $errors['vaha']; ?>
			</div>
		</div>

		<div class="form-group">
			<label for="" class="col-sm-4 control-label">Disciplína</label>
			<div class="col-sm-4 radio">
				<label class="radio-inline">
					<input type="checkbox" name="odvetvi[0]" id="atletika" value="atletika" <?php if (!empty($data['odvetvi'][0])) echo "checked"; ?>>
					Atletika
				</label>
				<label class="radio-inline">
					<input type="checkbox" name="odvetvi[1]" id="cyklistika" value="cyklistika" <?php if (!empty($data['odvetvi'][1])) echo "checked"; ?>>
					Cyklistika
				</label>
				<label class="radio-inline">
					<input type="checkbox" name="odvetvi[2]" id="gymnastika" value="gymnastika" <?php if (!empty($data['odvetvi'][2])) echo "checked"; ?>>
					Gymnastika
				</label>
				<label class="radio-inline">
					<input type="checkbox" name="odvetvi[3]" id="plavání" value="plavání" <?php if (!empty($data['odvetvi'][3])) echo "checked"; ?>>
					Plavání
				</label>
			</div>
		</div>

		<div class="form-group">
			<div class="col-sm-10 col-sm-offset-2">
				<button type="submit" class="btn btn-primary" name="submit">Odeslat</button>
			</div>
		</div>
	</fieldset>	
</form>
<?php 
	endif;

 ?>
Last edited on
Topic archived. No new replies allowed.