[oop] Simpel Contact Formulier
Ik ben al jaren actief als "amateur programmeur" met voornamelijk PHP, gewoon omdat ik het leuk vind. Nu ben ik begonnen met het aanleren van OOP, en heb al iets gemaakt, puur om te leren. Nu zou ik graag jullie mening willen weten over onderstaande. Ik denk namelijk dat het niet geheel juist is wat ik gedaan heb. Zie eerst onderstaande code:
Code (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
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
<?php
require 'contact.class.php';
?>
<form action="" method="post">
<?php
if (isset($_POST["submit"])) {
$sendMail = new gw2Mail();
$sendMail->senderName = $_POST['senderName'];
$sendMail->senderEmail = $_POST['senderEmail'];
$sendMail->recipient = $_POST['recipient'];
$sendMail->copy = $_POST['copy'];
$sendMail->subject = $_POST['subject'];
$sendMail->message = $_POST['message'];
$sendMail->sendMail();
}
?>
<table class='ipb_table ipsMemberList'>
<tr class='header' colspan='4'>
<th scope='col' colspan='5'>Send E-mail</th>
</tr>
<tr class='row1'>
<td style='width: 50%'><strong>From:</strong><br />Enter the sender's name in this field.</td>
<td><input type="text" class="input_text" name="senderName" id="senderName" value="<?php if (isset($_POST['senderName'])) { echo $_POST['senderName']; } ?>" size="50" maxlength="125" /></td>
</tr>
<tr class='row2'>
<td style='width: 50%'><strong>From E-mail:</strong><br />Enter the sender's e-mail address in this field.</td>
<td><input type="text" class="input_text" name="senderEmail" id="senderEmail" value="<?php if (isset($_POST['senderEmail'])) { echo $_POST['senderEmail']; } ?>" size="50" maxlength="125" /></td>
</tr>
<tr class='row1'>
<td style='width: 50%'><strong>Recipient:</strong><br />Enter the recipient's e-mail address in this field.</td>
<td><input type="text" class="input_text" name="recipient" id="recipient" value="<?php if (isset($_POST['recipient'])) { echo $_POST['recipient']; } ?>" size="50" maxlength="125" /></td>
</tr>
<tr class='row2'>
<td style='width: 50%'><strong>Carbon Copy:</strong><br />Send a copy to someone else? Enter another e-mail address here. Leave blank for no copy.</td>
<td><input type="text" class="input_text" name="copy" id="copy" value="<?php if (isset($_POST['copy'])) { echo $_POST['copy']; } ?>" size="50" maxlength="125" /></td>
</tr>
<tr class='row1'>
<td style='width: 50%'><strong>Subject:</strong><br />Enter a subject in this field.</td>
<td><input type="text" class="input_text" name="subject" id="subject" value="<?php if (isset($_POST['subject'])) { echo $_POST['subject']; } ?>" size="50" maxlength="50" /></td>
</tr>
<tr class='row2'>
<td colspan='2' style='width: 100%'><textarea style="height: 250px; width: 99%;" name="message" id="message" cols="30" rows="14" virtual wrap="on"><?php if (isset($_POST['message'])) { echo $_POST['message']; } ?></textarea></td>
</tr>
</table>
<input type="submit" name="submit" value="Submit" tabindex="50" class="input_submit" accesskey="s" />
</form>
require 'contact.class.php';
?>
<form action="" method="post">
<?php
if (isset($_POST["submit"])) {
$sendMail = new gw2Mail();
$sendMail->senderName = $_POST['senderName'];
$sendMail->senderEmail = $_POST['senderEmail'];
$sendMail->recipient = $_POST['recipient'];
$sendMail->copy = $_POST['copy'];
$sendMail->subject = $_POST['subject'];
$sendMail->message = $_POST['message'];
$sendMail->sendMail();
}
?>
<table class='ipb_table ipsMemberList'>
<tr class='header' colspan='4'>
<th scope='col' colspan='5'>Send E-mail</th>
</tr>
<tr class='row1'>
<td style='width: 50%'><strong>From:</strong><br />Enter the sender's name in this field.</td>
<td><input type="text" class="input_text" name="senderName" id="senderName" value="<?php if (isset($_POST['senderName'])) { echo $_POST['senderName']; } ?>" size="50" maxlength="125" /></td>
</tr>
<tr class='row2'>
<td style='width: 50%'><strong>From E-mail:</strong><br />Enter the sender's e-mail address in this field.</td>
<td><input type="text" class="input_text" name="senderEmail" id="senderEmail" value="<?php if (isset($_POST['senderEmail'])) { echo $_POST['senderEmail']; } ?>" size="50" maxlength="125" /></td>
</tr>
<tr class='row1'>
<td style='width: 50%'><strong>Recipient:</strong><br />Enter the recipient's e-mail address in this field.</td>
<td><input type="text" class="input_text" name="recipient" id="recipient" value="<?php if (isset($_POST['recipient'])) { echo $_POST['recipient']; } ?>" size="50" maxlength="125" /></td>
</tr>
<tr class='row2'>
<td style='width: 50%'><strong>Carbon Copy:</strong><br />Send a copy to someone else? Enter another e-mail address here. Leave blank for no copy.</td>
<td><input type="text" class="input_text" name="copy" id="copy" value="<?php if (isset($_POST['copy'])) { echo $_POST['copy']; } ?>" size="50" maxlength="125" /></td>
</tr>
<tr class='row1'>
<td style='width: 50%'><strong>Subject:</strong><br />Enter a subject in this field.</td>
<td><input type="text" class="input_text" name="subject" id="subject" value="<?php if (isset($_POST['subject'])) { echo $_POST['subject']; } ?>" size="50" maxlength="50" /></td>
</tr>
<tr class='row2'>
<td colspan='2' style='width: 100%'><textarea style="height: 250px; width: 99%;" name="message" id="message" cols="30" rows="14" virtual wrap="on"><?php if (isset($_POST['message'])) { echo $_POST['message']; } ?></textarea></td>
</tr>
</table>
<input type="submit" name="submit" value="Submit" tabindex="50" class="input_submit" accesskey="s" />
</form>
contact.class.php:
Code (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
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
<?php
class gw2Mail {
var $senderName;
var $senderEmail;
var $recipient;
var $copy;
var $subject;
var $message;
var $bcc;
public function sendMail()
{
if ($this->senderName != "") {
$this->senderName = filter_var($this->senderName, FILTER_SANITIZE_STRING);
if ($this->senderName == "") {
$errors .= '- Please enter a valid name!';
}
} else {
$errors .= '- You forgot to enter a name!<br />';
}
if ($this->senderEmail != "") {
$this->senderEmail = filter_var($this->senderEmail, FILTER_SANITIZE_STRING);
if ($this->senderEmail == "") {
$errors .= '- Please enter a valid Email!';
}
} else {
$errors .= '- You forgot to enter an email!<br />';
}
if ($this->recipient != "") {
$this->recipient = filter_var($this->recipient, FILTER_SANITIZE_STRING);
if ($this->recipient == "") {
$errors .= '- Please enter a valid recipient email!';
}
} else {
$errors .= '- You forgot to enter a recipient email!<br />';
}
if ($this->subject != "") {
$this->subject = filter_var($this->subject, FILTER_SANITIZE_STRING);
if ($this->subject == "") {
$errors .= '- Please enter a valid subject!';
}
} else {
$errors .= '- You forgot to enter a subject!<br />';
}
if ($this->message != "") {
$this->message = filter_var($this->message, FILTER_SANITIZE_STRING);
if ($this->message == "") {
$errors .= '- Please enter a valid message!';
}
} else {
$errors .= '- You forgot to enter a message!<br />';
}
if (!$errors) {
$this->bcc="";
$headers = "From: $this->senderName <$this->senderEmail>";
$headers .= "\r\nCc: $this->copy";
$headers .= "\r\nBcc: $this->bcc\r\n\r\n";
$send_contact=mail("$this->recipient","$this->subject","$this->message","$headers");
} else {
echo '<p class=\'message error\'>';
echo '<font color="#FFFFFF">' . $errors . '</font>';
echo '</p><br />';
}
}
}
?>
class gw2Mail {
var $senderName;
var $senderEmail;
var $recipient;
var $copy;
var $subject;
var $message;
var $bcc;
public function sendMail()
{
if ($this->senderName != "") {
$this->senderName = filter_var($this->senderName, FILTER_SANITIZE_STRING);
if ($this->senderName == "") {
$errors .= '- Please enter a valid name!';
}
} else {
$errors .= '- You forgot to enter a name!<br />';
}
if ($this->senderEmail != "") {
$this->senderEmail = filter_var($this->senderEmail, FILTER_SANITIZE_STRING);
if ($this->senderEmail == "") {
$errors .= '- Please enter a valid Email!';
}
} else {
$errors .= '- You forgot to enter an email!<br />';
}
if ($this->recipient != "") {
$this->recipient = filter_var($this->recipient, FILTER_SANITIZE_STRING);
if ($this->recipient == "") {
$errors .= '- Please enter a valid recipient email!';
}
} else {
$errors .= '- You forgot to enter a recipient email!<br />';
}
if ($this->subject != "") {
$this->subject = filter_var($this->subject, FILTER_SANITIZE_STRING);
if ($this->subject == "") {
$errors .= '- Please enter a valid subject!';
}
} else {
$errors .= '- You forgot to enter a subject!<br />';
}
if ($this->message != "") {
$this->message = filter_var($this->message, FILTER_SANITIZE_STRING);
if ($this->message == "") {
$errors .= '- Please enter a valid message!';
}
} else {
$errors .= '- You forgot to enter a message!<br />';
}
if (!$errors) {
$this->bcc="";
$headers = "From: $this->senderName <$this->senderEmail>";
$headers .= "\r\nCc: $this->copy";
$headers .= "\r\nBcc: $this->bcc\r\n\r\n";
$send_contact=mail("$this->recipient","$this->subject","$this->message","$headers");
} else {
echo '<p class=\'message error\'>';
echo '<font color="#FFFFFF">' . $errors . '</font>';
echo '</p><br />';
}
}
}
?>
Het gaat me even over twee dingen. De form validation, kun je dat het beste in de class zelf zetten of juist daarbuiten, en waarom? Of is dat gewoon "smaak"?
En ten tweede, zaken zoals onderstaande:
Code (php)
1
2
3
4
5
2
3
4
5
} else {
echo '<p class=\'message error\'>';
echo '<font color="#FFFFFF">' . $errors . '</font>';
echo '</p><br />';
}
echo '<p class=\'message error\'>';
echo '<font color="#FFFFFF">' . $errors . '</font>';
echo '</p><br />';
}
Zet je dat ook in je class? Dat is namelijk een stukje styling voor de website. Ik denk niet dat ik dit zo goed doe.
Tips over bovenstaande zijn welkom! Alvast bedankt!
Ten eerste mix je PHP code in je HTML code, wat daar totaal niet thuis hoort.
Ten tweede mix je HTML code in je PHP code, wat daar ook absoluut niet thuis hoort.
Je mail class gebruikt het var keyword. Iets wat al jaren niet meer wordt gebruikt. Dat is iets uit PHP4. Raar dat je daar geen warning over hebt gekregen. Waarschijnljk staan je display_errors uit. Ook niet echt handig.
Verder zie ik niet welke form validation je waar wilt hebben. In welke class moet dan volgens jou komen?
Sowieso moet je een class niet meerdere dingen laten doen. Als je een class hebt die een form kan opbouwen, dan stop je je validatie in een andere class. Dat heeft niets met smaak te maken, moor dat is nu eenmaal hoe OO werkt (SOLID principle).
Waarom gebruik je niet een MVC framework zoals Symfony, Laravel oid? Dan word je al meer in een OO richting geduwd dan wanneer je het zelf probeer te doen. Dat gaat je nooit lukken als je er net mee bezig bent.
En waarom gebruik je ook geen mail class zoals DwiftMailer? Die kunnen precies doen wat jij wilt en nog veel meer.
Bedankt voor je reacties, ik kan er in ieder geval wat mee! :)