Input velden verdwijnen na PHP echo

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Pagina: 1 2 3 volgende »

Sebas V

Sebas V

28/08/2012 11:12:40
Quote Anchor link
Goedemorgen,

Alle input velden van mijn formulier verdwijnen die na de eerste ECHO foutmelding staan. Hoe kan ik dit verhelpen zodat alle velden weer zichtbaar worden?

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<tr class="rij_aanhef">
<td class="left_td">Aanhef<font size="-2" style="vertical-align:top;">*</font></td>
<td><label class="label_aanhef" for="aanhef_1"><input name="aanhef" id="aanhef_1" value="Dhr." type="radio" /> Dhr.</label><label class="label_aanhef" for="aanhef_2"><input name="aanhef" id="aanhef_2" value="Mevr." type="radio" /> Mevr.</label>
<span class="error" style="display:none; margin-left:40px;">
<?php echo($contact->error_value('aanhef')) ?>
</span>
</td>
</tr>
<tr class="rij_contactpersoon">
<td class="left_td">Contactpersoon</td>
<td>
<input id="contactpersoon" name="contactpersoon" class="inplaceError" maxlength="120" type="text" class="error" autocomplete="off" onFocus="window.scrollTo(0, 0);"/>
<span class="error" style="display:none; margin-left:40px;">
<?php echo($contact->error_value('contactpersoon')) ?>
</span>
</td>
</tr>
<tr class="rij_bedrijf">
<td class="left_td">Bedrijfsnaam</td>
<td>
<input id="bedrijfsnaam" name="bedrijfsnaam" class="inplaceError" maxlength="120" type="text" autocomplete="off" onFocus="window.scrollTo(0, 0);"/>
<span class="error" style="display:none; margin-left:40px;">
<?php echo($contact->error_value('bedrijfsnaam')) ?>
</span>
</td>
</tr>
 
PHP hulp

PHP hulp

25/12/2024 18:41:33
 
Eddy E

Eddy E

28/08/2012 11:17:53
Quote Anchor link
Je mist een puntkomma ná je echo-aanroep. Consequent.
En een echo hoeft niet met haakjes.
 
Sebas V

Sebas V

28/08/2012 11:21:50
Quote Anchor link
Ik heb onderstaand voorbeeld geprobeerd, maar ik kom nog steeds hetzelfde probleem tegen. Mis ik hier iets?


Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?php echo $contact->error_value('aanhef'); ?>
 
Chris PHP

Chris PHP

28/08/2012 11:21:50
Quote Anchor link
Eddy Erkelens op 28/08/2012 11:17:53:
Je mist een puntkomma ná je echo-aanroep. Consequent.
En een echo hoeft niet met haakjes.


Dat niet alleen, waarom staan daar () omheen? Tevens moet een array volgens mij met [] en niet met ().

Het zou dan zo moeten komen lijkt me.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?php echo $contact->error_value['aanhef']; ?>


Toevoeging op 28/08/2012 11:22:41:

Sebastian V op 28/08/2012 11:21:50:
Ik heb onderstaand voorbeeld geprobeerd, maar ik kom nog steeds hetzelfde probleem tegen. Mis ik hier iets?


Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?php echo $contact->error_value('aanhef'); ?>


Ja bij een array gebruik je [] en niet ().
 
Sebas V

Sebas V

28/08/2012 11:23:49
Quote Anchor link
Bedankt Chris, de velden zijn nu weer zichtbaar!
 
John Berg

John Berg

28/08/2012 11:25:53
Quote Anchor link
@Eddy: bij 1 regel php code hoeft er geen puntkomma achter.

@Sebastian: verander regel 5 eens in:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?php echo '<pre>'; print_r($contact); echo '</pre>'; ?>


Wat zie je?
 
Chris PHP

Chris PHP

28/08/2012 11:28:15
Quote Anchor link
Sebastian V op 28/08/2012 11:23:49:
Bedankt Chris, de velden zijn nu weer zichtbaar!


Mag ik ook vragen waarom je hem de class="error" geeft, maar vervolgens inline css gebruikt?

<span class="error" style="display:none; margin-left:40px;">

Dik is je error class, en cursief is je inline css. Je kunt die ook gewoon toevoegen aan je error class in je css. Dan pakt hij alles in 1x mee en wordt het:

<span class="error">


Toevoeging op 28/08/2012 11:29:17:

John Berg op 28/08/2012 11:25:53:
@Eddy: bij 1 regel php code hoeft er geen puntkomma achter.

@Sebastian: verander regel 5 eens in:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?php echo '<pre>'; print_r($contact); echo '</pre>'; ?>


Wat zie je?

Het probleem is opgelost, bij de array gebruikte hij () ipv [].
 
Sebas V

Sebas V

28/08/2012 11:35:01
Quote Anchor link
@Chris: Dat was om de class te overrulen in het begin, ik heb het nu toegevoegd aan de class.

Ik merk alleen dat mijn foutmeldingen niet zichtbaar worden naast de input velden zodra ik sommige velden niet invul.. Doe ik iets fout in mijn PHP script?

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<div id="formulierruimte">
                                        <?php
                                            require_once("php/db.php");                 /* Database Class */
                                            require_once('php/utils/is_email.php');     /* Email Validation Script */
                                            
                                            /* Handle Ajax Request */

                                            if(isset($_POST['newcontact'])){
                                                $contact = new Contact();
                                                //We'll refer to that later on, to display errors.
                                                //unset($contact);

                                            }
                                            else{
                                                //header('Location: result.php');
                                            }
                                            
                                            /* Class Contact */
                                            class Contact{
                                            
                                                private $db;                        /* the database obj */
                                            
                                                //private $errors         = array();  /* holds error messages */
                                                //we have to init $errors array, as otherwise form will produce errors on missing array entry

                                                private $errors = array(            /* holds error messages */
                                                            'aanhef' => '',
                                                            'bedrijfsnaam' => '',
                                                            'email' => '',
                                                            'telefoon' => '',
                                                            'vraag1_antwoorden' => '',
                                                            'vraag2_antwoorden' => ''
                                                        );
                                            
                                            
                                                private $num_errors;                /* number of errors in submitted form */
                                            
                                                public function __construct(){
                                                    $this->db = new DB();
                                                    if(isset($_POST['newcontact']))
                                                        $this->processNewMessage();
                                                    /* We don't need this anymore
                                                     *
                                                    else
                                                        header('Location: result.php');*/

                                                }
                                            
                                                public function processNewMessage(){
                                            
                                                    $aanhef                = $_POST['aanhef'];            
                                                    $contactpersoon        = $_POST['contactpersoon'];        
                                                    $bedrijfsnaam        = $_POST['bedrijfsnaam'];
                                                    $telefoon            = $_POST['telefoon'];
                                                    $email                = $_POST['email'];
                                                    $vraag1_antwoorden    = $_POST['vraag1_antwoorden'];
                                                    $vraag2_antwoorden    = $_POST['vraag2_antwoorden'];
                                            
                                            
                                                    /* Server Side Data Validation */
                                            
                                                    /* Aanhef Validation */

                                                    if(!$aanhef || mb_strlen($aanhef = trim($aanhef)) == 0)
                                                        $this->setError('aanhef', 'Selecteer uw aanhef');
                                            
                                                    /* Contactpersoon Validation */
                                                    if(!$contactpersoon || mb_strlen($contactpersoon = trim($contactpersoon)) == 0)
                                                        $this->setError('contactpersoon', 'Vul uw contactpersoon in');
                                                    else if(mb_strlen(trim($contactpersoon)) > 120)
                                                        $this->setError('contactpersoon', 'Te lang! 120 karakters max.');
                                            
                                                    /* Bedrijfsnaam Validation */
                                                        if(!$bedrijfsnaam || mb_strlen($bedrijfsnaam = trim($bedrijfsnaam)) == 0)
                                                        $this->setError('bedrijfsnaam', 'Vul uw bedrijfsnaam in');
                                                    else if(mb_strlen(trim($bedrijfsnaam)) > 120)
                                                        $this->setError('bedrijfsnaam', 'Te lang! 120 karakters max.');
                                            
                                                        /* Telefoon Validation */
                                                        if(!$telefoon || mb_strlen($telefoon = trim($telefoon)) == 0)
                                                        $this->setError('telefoon', 'Vul uw telefoonnummer in');
                                                    else if(mb_strlen(trim($telefoon)) > 120)
                                                        $this->setError('telefoon', 'Te lang! 120 karakters max.');
                                            
                                                        /* Vraag 1 Validation */
                                                        if(!$vraag1_antwoorden || mb_strlen($vraag1_antwoorden = trim($vraag1_antwoorden)) == 0)
                                                        $this->setError('vraag1_antwoorden', 'Selecteer een antwoord a.u.b.');
                                            
                                                        /* Vraag 2 Validation */
                                                        if(!$vraag2_antwoorden || mb_strlen($vraag2_antwoorden = trim($vraag2_antwoorden)) == 0)
                                                        $this->setError('vraag2_antwoorden', 'Selecteer een antwoord a.u.b.');
                                            
                                                        /* Email Validation */
                                                    if(!$email || mb_strlen($email = trim($email)) == 0)
                                                        $this->setError('email','Vul uw e-mail in');
                                                    else{
                                                        if(!is_email($email))
                                                            $this->setError('email', 'Vul een correct email adres in');
                                                        else if(mb_strlen($email) > 120)
                                                            $this->setError('email', 'Te lang! 120 karakters max.');
                                                    }

                                            
                                                    /* No errors, insert in db
                                                    else*/

                                                    if($this->countErrors() == 0) {
                                                        if(($ret = $this->db->dbNewMessage($aanhef, $contactpersoon, $bedrijfsnaam, $email, $telefoon, $vraag1_antwoorden, $vraag2_antwoorden)) > 0){
                                                            //$json = array('result'         => 1);
                                                            if(SEND_EMAIL)
                                                                $this->sendEmail($aanhef,$contactpersoon,$bedrijfsnaam,$email,$telefoon,$vraag1_antwoorden,$vraag2_antwoorden);
                                            
                                                            //This is for relocating to successful result page
                                                            header('Location: result.php');
                                                        }
else {  
                                                            // else
                                                            //    $json = array('result'      => -2); /* something went wrong in database insertion  */
                                            
                                                            //This will need special treatment. You have to prepare an errorpage
                                                            //for database-related issues.

                                                            header("Location: database-error.html");
                                                        }

                                            
                                                        /*$encoded = json_encode($json);
                                                        echo $encoded;
                                                        unset($encoded);*/

                                                    }
                                                }

                                            
                                                public function sendEmail($aanhef,$contactpersoon,$bedrijfsnaam,$email,$telefoon,$vraag1_antwoorden,$vraag2_antwoorden){
                                                /* Just format the email text the way you want ... */
                                                $message_body   = "<div style=\"font-size:12px; font-weight:normal;\">Hallo,<br><br>"
                                                                            ."Het volgende bedrijf heeft zich zojuist aangemeld:</div><br>"
                                                                            ."<table cellpadding=\"1\" cellspacing=\"1\" width=\"550px\"><tr><td style=\"font-size:12px; color:#000000\">Bedrijfsnaam:</td><td style=\"font-size:12px; color:#000000\">".$bedrijfsnaam."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Aanhef:</td><td style=\"font-size:12px; color:#000000\">".$aanhef."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Contactpersoon:</td><td style=\"font-size:12px; color:#000000\">".$contactpersoon."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Telefoonnummer:</td><td style=\"font-size:12px; color:#000000\">".$telefoon."</td></tr><tr><td style=\"font-size:12px; color:#000000\">E-mail:</td><td style=\"font-size:12px; color:#000000\">".$email."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Antwoord vraag 1:</td><td style=\"font-size:12px; color:#000000\">".$vraag1_antwoorden."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Antwoord vraag 2:</td><td style=\"font-size:12px; color:#000000\">".$vraag2_antwoorden."</td></tr></table><br>";
                                                
                                                    // Geef GELDIGE adressen op
                                                    // Een korte benaming voor jouw website

                                                    $website_naam = 'Aanmelding';
                                                    // Jouw eigen geldige emailadres
                                                    $eigen_emailadres = 'MAIL';
                                                    // Een geldig emailadres voor errors
                                                    $error_emailadres = 'MAIL';
                                                    // De naam van de verzender
                                                    $naam_verzender = ''.$bedrijfsnaam.'';
                                                    // Het geldige emailadres van de afzender
                                                    $email_verzender = ''.$email.'';
                                                    // Een geldig emailadres of helemaal leeg laten
                                                    $bcc_emailadres = '';
                                                    // HTML mail? True/False
                                                    $html = true;
                                                    
                                                    // De headers samenstellen
                                                    $headers     = 'From: ' . $website_naam . ' <' . $eigen_emailadres . '>' . PHP_EOL;
                                                    $headers    .= 'Reply-To: ' . $naam_verzender . ' <' . $email_verzender . '>' . PHP_EOL;
                                                    $headers    .= 'Return-Path: Mail-Error <' . $error_emailadres . '>' . PHP_EOL;
                                                    $headers    .= ($bcc_emailadres != '') ? 'Bcc: ' . $bcc_emailadres . PHP_EOL : '';
                                                    $headers    .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
                                                    $headers    .= 'X-Priority: Normal' . PHP_EOL;
                                                    $headers    .= ($html) ? 'MIME-Version: 1.0' . PHP_EOL : '';
                                                    $headers    .= ($html) ? 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL : '';
                                        
                                                
                                                    mail(EMAIL_TO,MESSAGE_SUBJECT,$message_body,$headers);
                                            
                                                }

                                            
                                                public function setError($field, $errmsg){
                                                    $this->errors[$field]   = $errmsg;
                                                    $this->num_errors       = count($this->errors);
                                                }

                                            
                                                public function error_value($field){
                                                    if(array_key_exists($field,$this->errors))
                                                        return $this->errors[$field];
                                                    else
                                                        return '';
                                                }

                                            
                                                public function countErrors(){
                                                    return $this->num_errors;
                                                }
                                            };

                                            ?>

                                            <table width="675px" cellpadding="0" cellspacing="0">
                                                <form id="ContactForm" action="quiz.php" method="post">
                                                <tr>
                                                    <td class="left_td">&nbsp;</td><td><font size="-2">* Gelieve alle velden in te vullen.</font></td>
                                                </tr>
                                                <tr class="rij_aanhef">
                                                    <td class="left_td">Aanhef<font size="-2" style="vertical-align:top;">*</font></td>
                                                    <td>
                                                        <label class="label_aanhef" for="aanhef_1"><input name="aanhef" id="aanhef_1" value="Dhr." type="radio" /> Dhr.</label><label class="label_aanhef" for="aanhef_2"><input name="aanhef" id="aanhef_2" value="Mevr." type="radio" /> Mevr.</label>
                                                        <span class="error">
                                                            <?php echo $aanhef->error_value['aanhef']; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                                <tr class="rij_contactpersoon">
                                                    <td class="left_td">Contactpersoon</td>
                                                    <td>
                                                        <input id="contactpersoon" name="contactpersoon" class="inplaceError" maxlength="120" type="text" class="error" autocomplete="off" onFocus="window.scrollTo(0, 0);"/>
                                                        <span class="error">
                                                            <?php echo $contactpersoon->error_value['contactpersoon']; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                                <tr class="rij_bedrijf">
                                                    <td class="left_td">Bedrijfsnaam</td>
                                                    <td>
                                                        <input id="bedrijfsnaam" name="bedrijfsnaam" class="inplaceError" maxlength="120" type="text" autocomplete="off" onFocus="window.scrollTo(0, 0);"/>
                                                        <span class="error">
                                                            <?php echo $bedrijfsnaam->error_value['bedrijfsnaam']; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="left_td">E-mail</td>
                                                    <td>
                                                        <input id="email" name="email" class="inplaceError" maxlength="120" type="text" autocomplete="off" onFocus="window.scrollTo(0, 0);"/>
                                                        <span class="error">
                                                            <?php echo $email->error_value['email']; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="left_td">Telefoonnummer</td>
                                                    <td><input id="telefoon" name="telefoon" class="inplaceError" maxlength="120" type="text" autocomplete="off" onFocus="window.scrollTo(0, 0);"/>
                                                        <span class="error" style="display:none; margin-left:40px;">
                                                            <?php echo $telefoon->error_value['telefoon']; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>


Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
.error{
    color:#e4670f;
    display:none;
    margin-left:40px;
}
 
- SanThe -

- SanThe -

28/08/2012 11:40:41
Quote Anchor link
Sebastian V op 28/08/2012 11:35:01:
Ik merk alleen dat mijn foutmeldingen niet zichtbaar worden

display:none;


Lijkt mij logisch.
 
Sebas V

Sebas V

28/08/2012 11:43:10
Quote Anchor link
Hee Santhe, ik heb overal "display:none" verwijderd, maar er verschijnt nog steeds geen foutmelding..
 
Chris PHP

Chris PHP

28/08/2012 11:44:02
Quote Anchor link
Je vult bij elke statement dit in.

trim($bedrijfsnaam)) == 0)

Dit houdt in dat hij een integer verwacht.

Echter zijn alle values die jij invult strings, vervang de 0 een door ''. Dus 2x een enkele quote ' of 2x een dubbelen quote " allebei mag.
Dan wordt het zo

trim($bedrijfsnaam)) == '')


Dit houdt in dat als het een lege string is, hij iets gaat doen ipv wanneer hij 0 is wat in dit geval nooit zal voorkomen.
Gewijzigd op 28/08/2012 11:46:09 door Chris PHP
 
John Berg

John Berg

28/08/2012 11:48:13
Quote Anchor link
Niet het antwoord op je vraag: je zou kunnen overwegen om de jquery validate plugin in je form te hangen. De validatie gebeurt dat in de browser, en als er op de verzend knop gedrukt wordt, weet je zeker dat alles velden er zijn, en een geldige waarde hebben.

voorbeeld:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
  <head>
    <script language="javascript" type="text/javascript" src="jquery-latest.js"></script>
    
    <script language="javascript" type="text/javascript" src="jquery.validate.min.js"></script>
    <script>
      $(document).ready(function(){
        $( "#myform" ).validate();
      });
    </script>
  </head>

...

<form id="myform" action="/myaction" method="POST">

    <p>
      <input type="text" name="Name" id="name" value="" size="22" class="required" minlength="3">
      <label for="name">Name (required)</label>
    </p>

    <p>
      <input type="text" name="EMail" id="email" value="" size="22" class="required email">
      <label for="email">EMail (will not be published) (required)</label>
    </p>

...


En het ziet er dan zo uit:

Afbeelding

zomaar een ideetje ..
 
Sebas V

Sebas V

28/08/2012 11:51:54
Quote Anchor link
@Chris, heb ik gedaan, maar nog resultaat.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<div id="formulierruimte">
                                        <?php
                                            require_once("php/db.php");                 /* Database Class */
                                            require_once('php/utils/is_email.php');     /* Email Validation Script */
                                            
                                            /* Handle Ajax Request */

                                            if(isset($_POST['newcontact'])){
                                                $contact = new Contact();
                                                //We'll refer to that later on, to display errors.
                                                //unset($contact);

                                            }
                                            else{
                                                //header('Location: result.php');
                                            }
                                            
                                            /* Class Contact */
                                            class Contact{
                                            
                                                private $db;                        /* the database obj */
                                            
                                                //private $errors         = array();  /* holds error messages */
                                                //we have to init $errors array, as otherwise form will produce errors on missing array entry

                                                private $errors = array(            /* holds error messages */
                                                            'aanhef' => '',
                                                            'bedrijfsnaam' => '',
                                                            'email' => '',
                                                            'telefoon' => '',
                                                            'vraag1_antwoorden' => '',
                                                            'vraag2_antwoorden' => ''
                                                        );
                                            
                                            
                                                private $num_errors;                /* number of errors in submitted form */
                                            
                                                public function __construct(){
                                                    $this->db = new DB();
                                                    if(isset($_POST['newcontact']))
                                                        $this->processNewMessage();
                                                    /* We don't need this anymore
                                                     *
                                                    else
                                                        header('Location: result.php');*/

                                                }
                                            
                                                public function processNewMessage(){
                                            
                                                    $aanhef                = $_POST['aanhef'];            
                                                    $contactpersoon        = $_POST['contactpersoon'];        
                                                    $bedrijfsnaam        = $_POST['bedrijfsnaam'];
                                                    $telefoon            = $_POST['telefoon'];
                                                    $email                = $_POST['email'];
                                                    $vraag1_antwoorden    = $_POST['vraag1_antwoorden'];
                                                    $vraag2_antwoorden    = $_POST['vraag2_antwoorden'];
                                            
                                            
                                                    /* Server Side Data Validation */
                                            
                                                    /* Aanhef Validation */

                                                    if(!$aanhef || mb_strlen($aanhef = trim($aanhef)) == '')
                                                        $this->setError('aanhef', 'Selecteer uw aanhef');
                                            
                                                    /* Contactpersoon Validation */
                                                    if(!$contactpersoon || mb_strlen($contactpersoon = trim($contactpersoon)) == '')
                                                        $this->setError('contactpersoon', 'Vul uw contactpersoon in');
                                                    else if(mb_strlen(trim($contactpersoon)) > 120)
                                                        $this->setError('contactpersoon', 'Te lang! 120 karakters max.');
                                            
                                                    /* Bedrijfsnaam Validation */
                                                        if(!$bedrijfsnaam || mb_strlen($bedrijfsnaam = trim($bedrijfsnaam)) == '')
                                                        $this->setError('bedrijfsnaam', 'Vul uw bedrijfsnaam in');
                                                    else if(mb_strlen(trim($bedrijfsnaam)) > 120)
                                                        $this->setError('bedrijfsnaam', 'Te lang! 120 karakters max.');
                                            
                                                        /* Telefoon Validation */
                                                        if(!$telefoon || mb_strlen($telefoon = trim($telefoon)) == '')
                                                        $this->setError('telefoon', 'Vul uw telefoonnummer in');
                                                    else if(mb_strlen(trim($telefoon)) > 120)
                                                        $this->setError('telefoon', 'Te lang! 120 karakters max.');
                                            
                                                        /* Vraag 1 Validation */
                                                        if(!$vraag1_antwoorden || mb_strlen($vraag1_antwoorden = trim($vraag1_antwoorden)) == '')
                                                        $this->setError('vraag1_antwoorden', 'Selecteer een antwoord a.u.b.');
                                            
                                                        /* Vraag 2 Validation */
                                                        if(!$vraag2_antwoorden || mb_strlen($vraag2_antwoorden = trim($vraag2_antwoorden)) == '')
                                                        $this->setError('vraag2_antwoorden', 'Selecteer een antwoord a.u.b.');
                                            
                                                        /* Email Validation */
                                                    if(!$email || mb_strlen($email = trim($email)) == '')
                                                        $this->setError('email','Vul uw e-mail in');
                                                    else{
                                                        if(!is_email($email))
                                                            $this->setError('email', 'Vul een correct email adres in');
                                                        else if(mb_strlen($email) > 120)
                                                            $this->setError('email', 'Te lang! 120 karakters max.');
                                                    }

                                            
                                                    /* No errors, insert in db
                                                    else*/

                                                    if($this->countErrors() == 0) {
                                                        if(($ret = $this->db->dbNewMessage($aanhef, $contactpersoon, $bedrijfsnaam, $email, $telefoon, $vraag1_antwoorden, $vraag2_antwoorden)) > 0){
                                                            //$json = array('result'         => 1);
                                                            if(SEND_EMAIL)
                                                                $this->sendEmail($aanhef,$contactpersoon,$bedrijfsnaam,$email,$telefoon,$vraag1_antwoorden,$vraag2_antwoorden);
                                            
                                                            //This is for relocating to successful result page
                                                            header('Location: result.php');
                                                        }
else {  
                                                            // else
                                                            //    $json = array('result'      => -2); /* something went wrong in database insertion  */
                                            
                                                            //This will need special treatment. You have to prepare an errorpage
                                                            //for database-related issues.

                                                            header("Location: database-error.html");
                                                        }

                                            
                                                        /*$encoded = json_encode($json);
                                                        echo $encoded;
                                                        unset($encoded);*/

                                                    }
                                                }

                                            
                                                public function sendEmail($aanhef,$contactpersoon,$bedrijfsnaam,$email,$telefoon,$vraag1_antwoorden,$vraag2_antwoorden){
                                                /* Just format the email text the way you want ... */
                                                $message_body   = "<div style=\"font-size:12px; font-weight:normal;\">Hallo,<br><br>"
                                                                            ."Het volgende bedrijf heeft zich zojuist aangemeld:</div><br>"
                                                                            ."<table cellpadding=\"1\" cellspacing=\"1\" width=\"550px\"><tr><td style=\"font-size:12px; color:#000000\">Bedrijfsnaam:</td><td style=\"font-size:12px; color:#000000\">".$bedrijfsnaam."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Aanhef:</td><td style=\"font-size:12px; color:#000000\">".$aanhef."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Contactpersoon:</td><td style=\"font-size:12px; color:#000000\">".$contactpersoon."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Telefoonnummer:</td><td style=\"font-size:12px; color:#000000\">".$telefoon."</td></tr><tr><td style=\"font-size:12px; color:#000000\">E-mail:</td><td style=\"font-size:12px; color:#000000\">".$email."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Antwoord vraag 1:</td><td style=\"font-size:12px; color:#000000\">".$vraag1_antwoorden."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Antwoord vraag 2:</td><td style=\"font-size:12px; color:#000000\">".$vraag2_antwoorden."</td></tr></table><br>";
                                                
                                                    // Geef GELDIGE adressen op
                                                    // Een korte benaming voor jouw website

                                                    $website_naam = 'Aanmelding';
                                                    // Jouw eigen geldige emailadres
                                                    $eigen_emailadres = 'MAIL';
                                                    // Een geldig emailadres voor errors
                                                    $error_emailadres = 'MAIL';
                                                    // De naam van de verzender
                                                    $naam_verzender = ''.$bedrijfsnaam.'';
                                                    // Het geldige emailadres van de afzender
                                                    $email_verzender = ''.$email.'';
                                                    // Een geldig emailadres of helemaal leeg laten
                                                    $bcc_emailadres = '';
                                                    // HTML mail? True/False
                                                    $html = true;
                                                    
                                                    // De headers samenstellen
                                                    $headers     = 'From: ' . $website_naam . ' <' . $eigen_emailadres . '>' . PHP_EOL;
                                                    $headers    .= 'Reply-To: ' . $naam_verzender . ' <' . $email_verzender . '>' . PHP_EOL;
                                                    $headers    .= 'Return-Path: Mail-Error <' . $error_emailadres . '>' . PHP_EOL;
                                                    $headers    .= ($bcc_emailadres != '') ? 'Bcc: ' . $bcc_emailadres . PHP_EOL : '';
                                                    $headers    .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
                                                    $headers    .= 'X-Priority: Normal' . PHP_EOL;
                                                    $headers    .= ($html) ? 'MIME-Version: 1.0' . PHP_EOL : '';
                                                    $headers    .= ($html) ? 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL : '';
                                        
                                                
                                                    mail(EMAIL_TO,MESSAGE_SUBJECT,$message_body,$headers);
                                            
                                                }

                                            
                                                public function setError($field, $errmsg){
                                                    $this->errors[$field]   = $errmsg;
                                                    $this->num_errors       = count($this->errors);
                                                }

                                            
                                                public function error_value($field){
                                                    if(array_key_exists($field,$this->errors))
                                                        return $this->errors[$field];
                                                    else
                                                        return '';
                                                }

                                            
                                                public function countErrors(){
                                                    return $this->num_errors;
                                                }
                                            };

                                            ?>

                                            <table width="675px" cellpadding="0" cellspacing="0">
                                                <form id="ContactForm" action="quiz.php" method="post">
                                                <tr>
                                                    <td class="left_td">&nbsp;</td><td><font size="-2">* Gelieve alle velden in te vullen.</font></td>
                                                </tr>
                                                <tr class="rij_aanhef">
                                                    <td class="left_td">Aanhef<font size="-2" style="vertical-align:top;">*</font></td>
                                                    <td>
                                                        <label class="label_aanhef" for="aanhef_1"><input name="aanhef" id="aanhef_1" value="Dhr." type="radio" /> Dhr.</label><label class="label_aanhef" for="aanhef_2"><input name="aanhef" id="aanhef_2" value="Mevr." type="radio" /> Mevr.</label>
                                                        <span class="error">
                                                            <?php echo $aanhef->error_value['aanhef']; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                                <tr class="rij_contactpersoon">
                                                    <td class="left_td">Contactpersoon</td>
                                                    <td>
                                                        <input id="contactpersoon" name="contactpersoon" class="inplaceError" maxlength="120" type="text" class="error" autocomplete="off" onFocus="window.scrollTo(0, 0);"/>
                                                        <span class="error">
                                                            <?php echo $contactpersoon->error_value['contactpersoon']; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                                <tr class="rij_bedrijf">
                                                    <td class="left_td">Bedrijfsnaam</td>
                                                    <td>
                                                        <input id="bedrijfsnaam" name="bedrijfsnaam" class="inplaceError" maxlength="120" type="text" autocomplete="off" onFocus="window.scrollTo(0, 0);"/>
                                                        <span class="error">
                                                            <?php echo $bedrijfsnaam->error_value['bedrijfsnaam']; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="left_td">E-mail</td>
                                                    <td>
                                                        <input id="email" name="email" class="inplaceError" maxlength="120" type="text" autocomplete="off" onFocus="window.scrollTo(0, 0);"/>
                                                        <span class="error">
                                                            <?php echo $email->error_value['email']; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="left_td">Telefoonnummer</td>
                                                    <td><input id="telefoon" name="telefoon" class="inplaceError" maxlength="120" type="text" autocomplete="off" onFocus="window.scrollTo(0, 0);"/>
                                                        <span class="error">
                                                            <?php echo $telefoon->error_value['telefoon']; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
Gewijzigd op 28/08/2012 11:53:06 door Sebas V
 
Chris PHP

Chris PHP

28/08/2012 11:55:05
Quote Anchor link
Ik zie nu dat je ook bij elke if en if else statement geen {} hebt. Deze zijn ook vereist, dus plaats dit bij al je statements, hieronder een voorbeeld.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
<?php
if(!$contactpersoon || mb_strlen($contactpersoon = trim($contactpersoon)) == '') {
                                                        $this->setError('contactpersoon', 'Vul uw contactpersoon in');
                                                   }
else if (mb_strlen(trim($contactpersoon)) > 120) {
                                                        $this->setError('contactpersoon', 'Te lang! 120 karakters max.'); }
?>
Gewijzigd op 28/08/2012 11:56:10 door Chris PHP
 
Erwin H

Erwin H

28/08/2012 11:59:17
Quote Anchor link
John Berg op 28/08/2012 11:48:13:
Niet het antwoord op je vraag: je zou kunnen overwegen om de jquery validate plugin in je form te hangen. De validatie gebeurt dat in de browser, en als er op de verzend knop gedrukt wordt, weet je zeker dat alles velden er zijn, en een geldige waarde hebben.

En waarbij je dan de volledige validatie en check server side ook nog moet doen natuurlijk. Vertrouw NOOIT op data die van de browser komt. client side validatie is handig voor de gebruiker, maar vervangt niets server side.



Toevoeging op 28/08/2012 12:00:26:

@Chris, niet verplicht voor 1 regel tekst. Wel beter leesbaar en consequenter.
 
Sebas V

Sebas V

28/08/2012 12:07:21
Quote Anchor link
@Chris: Ik kreeg wat foutmeldingen en heb de } aangepast in {, nu zijn de foutmeldingen weg maar werkt de het vertonen van de foutmelding nog niet.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
                                            require_once("php/db.php");                 /* Database Class */
                                            require_once('php/utils/is_email.php');     /* Email Validation Script */
                                            
                                            /* Handle Ajax Request */

                                            if(isset($_POST['newcontact'])){
                                                $contact = new Contact();
                                                //We'll refer to that later on, to display errors.
                                                //unset($contact);

                                            }
                                            else{
                                                //header('Location: result.php');
                                            }
                                            
                                            /* Class Contact */
                                            class Contact{
                                            
                                                private $db;                        /* the database obj */
                                            
                                                //private $errors         = array();  /* holds error messages */
                                                //we have to init $errors array, as otherwise form will produce errors on missing array entry

                                                private $errors = array(            /* holds error messages */
                                                            'aanhef' => '',
                                                            'bedrijfsnaam' => '',
                                                            'email' => '',
                                                            'telefoon' => '',
                                                            'vraag1_antwoorden' => '',
                                                            'vraag2_antwoorden' => ''
                                                        );
                                            
                                            
                                                private $num_errors;                /* number of errors in submitted form */
                                            
                                                public function __construct(){
                                                    $this->db = new DB();
                                                    if(isset($_POST['newcontact']))
                                                        $this->processNewMessage();
                                                    /* We don't need this anymore
                                                     *
                                                    else
                                                        header('Location: result.php');*/

                                                }
                                            
                                                public function processNewMessage(){
                                            
                                                    $aanhef                = $_POST['aanhef'];            
                                                    $contactpersoon        = $_POST['contactpersoon'];        
                                                    $bedrijfsnaam        = $_POST['bedrijfsnaam'];
                                                    $telefoon            = $_POST['telefoon'];
                                                    $email                = $_POST['email'];
                                                    $vraag1_antwoorden    = $_POST['vraag1_antwoorden'];
                                                    $vraag2_antwoorden    = $_POST['vraag2_antwoorden'];
                                            
                                            
                                                    /* Server Side Data Validation */
                                                    if(!$aanhef || mb_strlen($aanhef = trim($aanhef)) == ''){
                                                        $this->setError('aanhef', 'Vul uw aanhef in');
                                                        }

                                                  
                                                    if(!$contactpersoon || mb_strlen($contactpersoon = trim($contactpersoon)) == ''){
                                                        $this->setError('contactpersoon', 'Vul uw contactpersoon in');
                                                        }

                                                        else if(mb_strlen(trim($contactpersoon)) > 120){
                                                        $this->setError('contactpersoon', 'Te lang! 120 karakters max.');
                                                        }

                                            
                                                    if(!$bedrijfsnaam || mb_strlen($bedrijfsnaam = trim($bedrijfsnaam)) == '') {
                                                        $this->setError('bedrijfsnaam', 'Vul uw bedrijfsnaam in');
                                                        }

                                                           else if (mb_strlen(trim($bedrijfsnaam)) > 120){
                                                        $this->setError('bedrijfsnaam', 'Te lang! 120 karakters max.');
                                                        }

                                                        
                                                    if(!$telefoon || mb_strlen($telefoon = trim($telefoon)) == '') {
                                                        $this->setError('telefoon', 'Vul uw telefoon in');
                                                        }

                                                           else if (mb_strlen(trim($telefoon)) > 120){
                                                        $this->setError('telefoon', 'Te lang! 120 karakters max.');
                                                        }

                                            
                                                    if(!$vraag1_antwoorden || mb_strlen($vraag1_antwoorden = trim($vraag1_antwoorden)) == '') {
                                                        $this->setError('vraag1_antwoorden', 'Selecteer een antwoord a.u.b.');
                                                        }

                                                    
                                                    if(!$vraag2_antwoorden || mb_strlen($vraag2_antwoorden = trim($vraag2_antwoorden)) == '') {
                                                        $this->setError('vraag2_antwoorden', 'Selecteer een antwoord a.u.b.');
                                                        }

                                                        
                                                    if(!$email || mb_strlen($email = trim($email)) == '') {
                                                        $this->setError('email', 'Vul uw email in');
                                                        }

                                                           else if (mb_strlen(trim($email)) > 120){
                                                        $this->setError('email', 'Te lang! 120 karakters max.');
                                                        }
                        
                                            
                                            
                                                    /* No errors, insert in db
                                                    else*/

                                                    if($this->countErrors() == 0) {
                                                        if(($ret = $this->db->dbNewMessage($aanhef, $contactpersoon, $bedrijfsnaam, $email, $telefoon, $vraag1_antwoorden, $vraag2_antwoorden)) > 0){
                                                            //$json = array('result'         => 1);
                                                            if(SEND_EMAIL)
                                                                $this->sendEmail($aanhef,$contactpersoon,$bedrijfsnaam,$email,$telefoon,$vraag1_antwoorden,$vraag2_antwoorden);
                                            
                                                            //This is for relocating to successful result page
                                                            header('Location: result.php');
                                                        }
else {  
                                                            // else
                                                            //    $json = array('result'      => -2); /* something went wrong in database insertion  */
                                            
                                                            //This will need special treatment. You have to prepare an errorpage
                                                            //for database-related issues.

                                                            header("Location: database-error.html");
                                                        }

                                            
                                                        /*$encoded = json_encode($json);
                                                        echo $encoded;
                                                        unset($encoded);*/

                                                    }
                                                }
[
/code]
Gewijzigd op 28/08/2012 12:08:03 door Sebas V
 
Chris PHP

Chris PHP

28/08/2012 12:11:34
Quote Anchor link
Het zijn arrays, dus pas het eens aan na dit, kijk eens wat hij nu doet. Je array setError zal nu leeg blijven.

$this->setError ['aanhef' => 'Vul uw aanhef in'];

Zo even al je set Error's aanpassen naar deze schrijfwijze.
 
Sebas V

Sebas V

28/08/2012 12:16:32
Quote Anchor link
Aan de hand van je voorbeeld heb ik nu dit staan, maar dan krijg ik foutmeldingen. Moeten de { weg of op een andere plek?

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
if(!$aanhef || mb_strlen($aanhef = trim($aanhef)) == '') {
                                                        $this->setError ['aanhef' => 'Vul uw aanhef in'];
                                                         }
 
Chris PHP

Chris PHP

28/08/2012 12:18:47
Quote Anchor link
Sebastian V op 28/08/2012 12:16:32:
Aan de hand van je voorbeeld heb ik nu dit staan, maar dan krijg ik foutmeldingen. Moeten de { weg of op een andere plek?

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
if(!$aanhef || mb_strlen($aanhef = trim($aanhef)) == '') {
                                                        $this->setError ['aanhef' => 'Vul uw aanhef in'];
                                                         }


Kun je de foutmelding(en) eens posten? Dat is wat makkelijker zoeken :D
 
Erwin H

Erwin H

28/08/2012 12:20:30
Quote Anchor link
Van dat bovenstaande klopt helemaal niets helaas. Zo te zien is setError een functie met twee argumenten:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
<?php
$this
->setError('aanhef', 'Vul uw aanhef in' );
?>
 
Sebas V

Sebas V

28/08/2012 12:21:40
Quote Anchor link
Ik heb het hieronder alleen bij "aanhef" toegepast, maar er verschijnt een syntax error bij deze regel: $this->setError ['aanhef' => 'Vul uw aanhef in'];

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
/* Server Side Data Validation */
                                                    if(!$aanhef || mb_strlen($aanhef = trim($aanhef)) == '') {
                                                        $this->setError ['aanhef' => 'Vul uw aanhef in'];
                                                         }
                                                  
                                                    if(!$contactpersoon || mb_strlen($contactpersoon = trim($contactpersoon)) == '') {
                                                        $this->setError('contactpersoon', 'Vul uw contactpersoon in');
                                                        } else if (mb_strlen(trim($contactpersoon)) > 120) {
                                                        $this->setError('contactpersoon', 'Te lang! 120 karakters max.'); }
                                            
                                                    if(!$bedrijfsnaam || mb_strlen($bedrijfsnaam = trim($bedrijfsnaam)) == '') {
                                                        $this->setError('bedrijfsnaam', 'Vul uw bedrijfsnaam in');
                                                        } else if (mb_strlen(trim($bedrijfsnaam)) > 120) {
                                                        $this->setError('bedrijfsnaam', 'Te lang! 120 karakters max.'); }
                                                        
                                                    if(!$telefoon || mb_strlen($telefoon = trim($telefoon)) == '') {
                                                        $this->setError('telefoon', 'Vul uw telefoon in');
                                                        } else if (mb_strlen(trim($telefoon)) > 120) {
                                                        $this->setError('telefoon', 'Te lang! 120 karakters max.'); }
                                            
                                                    if(!$vraag1_antwoorden || mb_strlen($vraag1_antwoorden = trim($vraag1_antwoorden)) == '') {
                                                        $this->setError('vraag1_antwoorden', 'Selecteer een antwoord a.u.b.'); }
                                                    
                                                    if(!$vraag2_antwoorden || mb_strlen($vraag2_antwoorden = trim($vraag2_antwoorden)) == '') {
                                                        $this->setError('vraag2_antwoorden', 'Selecteer een antwoord a.u.b.'); }
                                                        
                                                    if(!$email || mb_strlen($email = trim($email)) == '') {
                                                        $this->setError('email', 'Vul uw email in');
                                                        } else if (mb_strlen(trim($email)) > 120) {
                                                        $this->setError('email', 'Te lang! 120 karakters max.'); }    
 

Pagina: 1 2 3 volgende »



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.