-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.php
189 lines (154 loc) · 6.27 KB
/
form.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
<?php include 'inc/header.php';?>
<!-- form entry and validation -->
<?php
// set empty variables
$customernameErr = $customeremailErr = $friendsnameErr = $friendsemailErr = "";
$customername = $customeremail = $friendsname = $friendsemail = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["customername"])) {
$customernameErr = "inserisci il tuo nome";
} else {
$customername = form_input($_POST["customername"]);
// only letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$customername)) {
$customernameErr = "inserisci un nome valido";
}
}
if (empty($_POST["customeremail"])) {
$customeremailErr = "inserisci il tuo indirizzo e-mail";
} else {
$customeremail = form_input($_POST["customeremail"]);
// check if e-mail address is well-formed
// if (!filter_var($customeremail, FILTER_VALIDATE_EMAIL)) {
// $customeremailErr = "inserisci un indirizzo e-mail valido";
// }
}
if (empty($_POST["friendsname"])) {
$friendsnameErr = "inserisci un nome";
} else {
$friendsname = form_input($_POST["friendsname"]);
// only letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$friendsname)) {
$friendsnameErr = "inserisci un nome valido";
}
}
if (empty($_POST["friendsemail"])) {
$friendsemailErr = "inserisci un indirizzo e-mail";
} else {
$friendsemail = form_input($_POST["friendsemail"]);
// check if e-mail address is well-formed
// if (!filter_var($friendsemail, FILTER_VALIDATE_EMAIL)) {
// $friendsemailErr = "inserisci un indirizzo e-mail valido";
// }
}
}
// this is needed to return the form data below the form
function form_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<script>
function clearForm()
{
document.getElementById("recommendation").reset();
}
</script>
<h2 class="secondary_heading">Invia ad un'amica o un amico</h2>
<p class="subtitle_gray">Condividi questo sito fantastico con le amiche o gli amici!</p>
<p class="subtitle_gray">Facendo clic su INVIA accetti che questi dettagli vengano aggiunti alla nostra banca dati.</p>
<form id="recommendation" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<ul>
<li class="main_form">
<label>Il tuo nome * </label>
<input type="text" name="customername" value="<?php echo $_POST['customername']; ?>">
<span class="error"> <?php echo $customernameErr;?></span>
</li>
<li class="main_form">
<label>Il tuo indirizzo email * </label>
<input type="text" name="customeremail" value="<?php echo $_POST['customeremail']; ?>">
<span class="error"> <?php echo $customeremailErr;?></span>
</li>
<li class="main_form">
<label>il nome del tuo amico / della tua amica * </label>
<input type="text" name="friendsname" value="<?php echo $_POST['friendsname']; ?>">
<span class="error"> <?php echo $friendsnameErr;?></span>
</li>
<li class="main_form">
<label>il indirizzo e-mail del tuo amico / della tua amica * </label>
<input type="text" name="friendsemail" value="<?php echo $_POST['friendsemail']; ?>">
<span class="error"> <?php echo $friendsemailErr;?></span>
</li>
</ul>
<p class="main_form">
<label></label>
<input type="button" onclick="clearForm();" value="repristinare">
</p>
<p class="main_form">
<label></label>
<input type="image" src="images/submit.gif" name="submit" alt="submit button" /> INVIA
</p>
</form>
<!-- database connection with debugging message -->
<?php
// replace with name of database you are using:
$mydatabase = "aziendacampione";
$conn = mysqli_connect("localhost:8889", "root", "root", $mydatabase);
// in production, comment out from here...
if (!$conn) {
echo "<br>";
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "<br>";
echo "A connection was made to the $mydatabase database on MySQL!" . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($conn) . PHP_EOL;
// ...to here
?>
<?php
if(
(isset($_POST['customername'])&& $_POST['customername'] !='') &&
(isset($_POST['customeremail'])&& $_POST['customeremail'] !='') &&
(isset($_POST['friendsname'])&& $_POST['friendsname'] !='') &&
(isset($_POST['friendsemail'])&& $_POST['friendsemail'] !='')
)
{
$customername = $conn->real_escape_string($_POST['customername']);
$customeremail = $conn->real_escape_string($_POST['customeremail']);
$friendsname = $conn->real_escape_string($_POST['friendsname']);
$friendsemail = $conn->real_escape_string($_POST['friendsemail']);
// replace with table you are writing to:
$mytable = "recommendations";
$sql = "INSERT INTO $mytable (customername, customeremail, friendsname, friendsemail)
VALUES ('".$customername."', '".$customeremail."', '".$friendsname."', '".$friendsemail."')";
if(!$result = $conn->query($sql)){
die('There was an error running the query [' . $conn->error . ']');
}
else
{
echo "<br>";
echo "<br>Grazie mille, $customername. Il tuo amico / la tua amica $friendsname è stato contattato/a.";
echo "<p>Questi sono i dati che hai aggiunto alla nostra banca dati:</p>";
echo "<br>";
echo "<span style='font-weight: bold;'>il tuo nome: </span>";
echo $customername;
echo "<br>";
echo "<span style='font-weight: bold;'>il tuo e-mail: </span>";
echo $customeremail;
echo "<br>";
echo "<span style='font-weight: bold;'>il nome del tuo amico / della tua amica: </span>";
echo $friendsname;
echo "<br>";
echo "<span style='font-weight: bold;'>il e-mail del tuo amico / della tua amica: </span>";
echo $friendsemail;
echo "<br>";
echo "<br>";
print 'Puoi <a href="'. $_SERVER['PHP_SELF'] .'">torna indietro e inviare un altro amico / una altra amica</a>, se lo desideri.';
}
}
?>
<?php include 'inc/footer.php';?>