radio buttons verliest value na validatie
Ik ben bezig met een formulier waar radio buttons in staan die gevalideerd worden. Echter als je 1 van hen aanvinkt en op "Verstuur" drukt dan verliest hij de gekozen waarde. zie: http://www.rc-maps.com/modelbouwtest/contact-test.php
Kan iemand mij hiermee helpen?
2de vraag: Wat doen die stripslashes eigenlijk?
Hier mijn 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
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
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
<?php
$contact_form_field_location= 'Locatie:';
$contact_form_field_name = 'Naam:';
$contact_form_field_email = 'E-mail:';
$contact_form_field_subject = 'Onderwerp:';
$contact_form_field_test1 = 'Test1:';
$contact_form_field_test2 = 'Test2:';
$contact_form_field_message = 'Bericht:';
$contact_form_field_number = 'Controle nummer:';
$contact_form_field_prompt = 'Controle nummer';
$contact_form_field_formname= 'Contactformulier';
$contact_form_button = 'Verstuur';
$contact_form_sent = 'Het bericht is met succes verzonden';
$contact_form_not_sent = 'Het bericht is niet verzonden';
$contact_form_invalid_data = 'Vul alle "rood" verplichte velden in.';
?>
<div class="contact_form">
<?php
function contact_form_post($name)
{
return isset($_POST[$name]) ? htmlentities($_POST[$name], ENT_COMPAT) : '';
}
if (isset($_POST['Contact']))
{
if (get_magic_quotes_gpc() && !function_exists('strip_slashes_deep'))
{
function strip_slashes_deep($value)
{
if (is_array($value)) return array_map('strip_slashes_deep', $value);
return stripslashes($value);
}
$_GET = strip_slashes_deep($_GET);
$_POST = strip_slashes_deep($_POST);
$_COOKIE = strip_slashes_deep($_COOKIE);
}
$patern_aux1 = "(\\w+(-\\w+)*)";
$patern_aux2 = "($patern_aux1\\.)*$patern_aux1@($patern_aux1\\.)+$patern_aux1";
$valid_location = isset($_POST['Location' ]) && trim($_POST['Location']);
$valid_name = isset($_POST['Name' ]) && trim($_POST['Name']);
$valid_email = isset($_POST['Email' ]) && preg_match("/^$patern_aux2$/iX", $_POST['Email']);
$valid_subject = isset($_POST['Subject']) && trim($_POST['Subject']);
$valid_test1 = isset($_POST['test1']) && trim($_POST['test1']);
$valid_test2 = isset($_POST['test2']) && trim($_POST['test2']);
$valid_message = isset($_POST['Message']) && trim($_POST['Message']);
$valid_number = isset($_POST['Number' ]) && isset($_SESSION['Number']) && $_POST['Number'] == $_SESSION['Number'];
// verplichte velden worden rood en schuin gezet
if (!$valid_location) $contact_form_field_location = "<em>$contact_form_field_location</em>";
if (!$valid_name) $contact_form_field_name = "<em>$contact_form_field_name</em>";
if (!$valid_email) $contact_form_field_email = "<em>$contact_form_field_email</em>";
if (!$valid_subject) $contact_form_field_subject = "<em>$contact_form_field_subject</em>";
if (!$valid_test2) $contact_form_field_test2 = "<em>$contact_form_field_test2</em>";
if (!$valid_message) $contact_form_field_message = "<em>$contact_form_field_message</em>";
if (!$valid_number) $contact_form_field_number = "<em>$contact_form_field_number</em>";
// verplichte velden
if ($valid_location && $valid_name && $valid_email && $valid_subject && $valid_test2 && $valid_message && $valid_number)
{
$error_reporting = error_reporting(E_ERROR | E_PARSE);
$tekst .= "\r\n$_POST[Location]";
$tekst .= "\r\n$_POST[Name]";
$tekst .= "\r\n$_POST[Email]";
$tekst .= "\r\n$_POST[Message]";
$tekst .= "\r\n$_POST[test1]";
$tekst .= "\r\n$_POST[test2]";
$subject .= "$_POST[Subject]";
$mail_sent = mail($contact_form_your_email, $subject, $tekst,
"To: $contact_form_your_name <$contact_form_your_email>\r\n" .
"From: {$_POST['Name']} <{$_POST['Email']}>\r\n");
error_reporting($error_reporting);
if ($mail_sent)
echo "<div class=\"error\"><h2>$contact_form_sent</h2><a href=\"terug naar overzicht.php\">Klik hier om weer terug te keren naar het overzicht</a></div>";
else echo "<div class=\"error\"><em class=\"error\">$contact_form_not_sent</em></div>";
if ($mail_sent) $_POST = array();
}
else echo "<div class=\"contact_form\"><div class=\"error\"><em>$contact_form_invalid_data</em></div></div>";
}
$_SESSION['Number'] = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT);
?>
<?php { ?>
<form method="post" action="<?=$_SERVER['REQUEST_URI'];?>">
<table>
<tr>
<th colspan="3"><div align="left"><?=$contact_form_field_formname;?></div></th>
</tr>
<tr>
<th><?=$contact_form_field_location; ?></th><td colspan="2"><div class="input">
<p>
<input type="radio" name="Location" value="loc1<?=contact_form_post('Location');?>">
loc1<br />
<input type="radio" name="Location" value="loc2<?=contact_form_post('Location');?>" />
loc2<br />
<input type="radio" name="Location" value="loc3<?=contact_form_post('Location');?>" />
loc3 </p>
</div></td></tr>
<tr><th><?=$contact_form_field_name; ?></th><td colspan="2"><div class="input"><input name="Name" type="text" value="<?=contact_form_post('Name');?>"></div></td></tr>
<tr><th><?=$contact_form_field_email; ?></th><td colspan="2"><div class="input"><input name= "Email" type="text" value="<?=contact_form_post('Email');?>"></div></td></tr>
<tr><th><?=$contact_form_field_subject;?></th><td colspan="2"><div class="input"><input name="Subject" type="text" value="<?=contact_form_post('Subject');?>"></div></td></tr>
<tr><th><?=$contact_form_field_test1;?></th><td colspan="2"><div class="input"><input name="test1" type="text" value="<?=contact_form_post('test1');?>"></div></td></tr>
<tr><th><?=$contact_form_field_test2;?></th><td colspan="2"><div class="input"><input name="test2" type="text" value="<?=contact_form_post('test2');?>" /></div></td></tr>
<tr><th><?=$contact_form_field_message;?></th><td colspan="2"><div class="input"><textarea name="Message" cols="35" rows="6"><?=contact_form_post('Message');?></textarea></div></td></tr>
<tr><th><?=$contact_form_field_number;?></th><td colspan="2"><div class="input" style="float: left;"><input type="text" name="Number" autocomplete="off">
</div>
</td>
</tr>
<tr>
<th> </th>
<td width="60"><img width="60" height="17" src="<?=$contact_form_image_url;?>?sname=<?=session_name();?>" alt=""></td>
<td width="324"><span class="input" style="float: left;"><small>
<?=$contact_form_field_prompt;?>
</small></span></td>
</tr>
<tr>
<th colspan="3"> </th>
</tr>
<tr><th></th><td colspan="2"><input id="submit_contact" class="submit" type="submit" name="Contact" value="<?=$contact_form_graphic_button ? '' : $contact_form_button;?>"></td></tr>
</table>
</form>
<?php } ?>
</div>
$contact_form_field_location= 'Locatie:';
$contact_form_field_name = 'Naam:';
$contact_form_field_email = 'E-mail:';
$contact_form_field_subject = 'Onderwerp:';
$contact_form_field_test1 = 'Test1:';
$contact_form_field_test2 = 'Test2:';
$contact_form_field_message = 'Bericht:';
$contact_form_field_number = 'Controle nummer:';
$contact_form_field_prompt = 'Controle nummer';
$contact_form_field_formname= 'Contactformulier';
$contact_form_button = 'Verstuur';
$contact_form_sent = 'Het bericht is met succes verzonden';
$contact_form_not_sent = 'Het bericht is niet verzonden';
$contact_form_invalid_data = 'Vul alle "rood" verplichte velden in.';
?>
<div class="contact_form">
<?php
function contact_form_post($name)
{
return isset($_POST[$name]) ? htmlentities($_POST[$name], ENT_COMPAT) : '';
}
if (isset($_POST['Contact']))
{
if (get_magic_quotes_gpc() && !function_exists('strip_slashes_deep'))
{
function strip_slashes_deep($value)
{
if (is_array($value)) return array_map('strip_slashes_deep', $value);
return stripslashes($value);
}
$_GET = strip_slashes_deep($_GET);
$_POST = strip_slashes_deep($_POST);
$_COOKIE = strip_slashes_deep($_COOKIE);
}
$patern_aux1 = "(\\w+(-\\w+)*)";
$patern_aux2 = "($patern_aux1\\.)*$patern_aux1@($patern_aux1\\.)+$patern_aux1";
$valid_location = isset($_POST['Location' ]) && trim($_POST['Location']);
$valid_name = isset($_POST['Name' ]) && trim($_POST['Name']);
$valid_email = isset($_POST['Email' ]) && preg_match("/^$patern_aux2$/iX", $_POST['Email']);
$valid_subject = isset($_POST['Subject']) && trim($_POST['Subject']);
$valid_test1 = isset($_POST['test1']) && trim($_POST['test1']);
$valid_test2 = isset($_POST['test2']) && trim($_POST['test2']);
$valid_message = isset($_POST['Message']) && trim($_POST['Message']);
$valid_number = isset($_POST['Number' ]) && isset($_SESSION['Number']) && $_POST['Number'] == $_SESSION['Number'];
// verplichte velden worden rood en schuin gezet
if (!$valid_location) $contact_form_field_location = "<em>$contact_form_field_location</em>";
if (!$valid_name) $contact_form_field_name = "<em>$contact_form_field_name</em>";
if (!$valid_email) $contact_form_field_email = "<em>$contact_form_field_email</em>";
if (!$valid_subject) $contact_form_field_subject = "<em>$contact_form_field_subject</em>";
if (!$valid_test2) $contact_form_field_test2 = "<em>$contact_form_field_test2</em>";
if (!$valid_message) $contact_form_field_message = "<em>$contact_form_field_message</em>";
if (!$valid_number) $contact_form_field_number = "<em>$contact_form_field_number</em>";
// verplichte velden
if ($valid_location && $valid_name && $valid_email && $valid_subject && $valid_test2 && $valid_message && $valid_number)
{
$error_reporting = error_reporting(E_ERROR | E_PARSE);
$tekst .= "\r\n$_POST[Location]";
$tekst .= "\r\n$_POST[Name]";
$tekst .= "\r\n$_POST[Email]";
$tekst .= "\r\n$_POST[Message]";
$tekst .= "\r\n$_POST[test1]";
$tekst .= "\r\n$_POST[test2]";
$subject .= "$_POST[Subject]";
$mail_sent = mail($contact_form_your_email, $subject, $tekst,
"To: $contact_form_your_name <$contact_form_your_email>\r\n" .
"From: {$_POST['Name']} <{$_POST['Email']}>\r\n");
error_reporting($error_reporting);
if ($mail_sent)
echo "<div class=\"error\"><h2>$contact_form_sent</h2><a href=\"terug naar overzicht.php\">Klik hier om weer terug te keren naar het overzicht</a></div>";
else echo "<div class=\"error\"><em class=\"error\">$contact_form_not_sent</em></div>";
if ($mail_sent) $_POST = array();
}
else echo "<div class=\"contact_form\"><div class=\"error\"><em>$contact_form_invalid_data</em></div></div>";
}
$_SESSION['Number'] = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT);
?>
<?php { ?>
<form method="post" action="<?=$_SERVER['REQUEST_URI'];?>">
<table>
<tr>
<th colspan="3"><div align="left"><?=$contact_form_field_formname;?></div></th>
</tr>
<tr>
<th><?=$contact_form_field_location; ?></th><td colspan="2"><div class="input">
<p>
<input type="radio" name="Location" value="loc1<?=contact_form_post('Location');?>">
loc1<br />
<input type="radio" name="Location" value="loc2<?=contact_form_post('Location');?>" />
loc2<br />
<input type="radio" name="Location" value="loc3<?=contact_form_post('Location');?>" />
loc3 </p>
</div></td></tr>
<tr><th><?=$contact_form_field_name; ?></th><td colspan="2"><div class="input"><input name="Name" type="text" value="<?=contact_form_post('Name');?>"></div></td></tr>
<tr><th><?=$contact_form_field_email; ?></th><td colspan="2"><div class="input"><input name= "Email" type="text" value="<?=contact_form_post('Email');?>"></div></td></tr>
<tr><th><?=$contact_form_field_subject;?></th><td colspan="2"><div class="input"><input name="Subject" type="text" value="<?=contact_form_post('Subject');?>"></div></td></tr>
<tr><th><?=$contact_form_field_test1;?></th><td colspan="2"><div class="input"><input name="test1" type="text" value="<?=contact_form_post('test1');?>"></div></td></tr>
<tr><th><?=$contact_form_field_test2;?></th><td colspan="2"><div class="input"><input name="test2" type="text" value="<?=contact_form_post('test2');?>" /></div></td></tr>
<tr><th><?=$contact_form_field_message;?></th><td colspan="2"><div class="input"><textarea name="Message" cols="35" rows="6"><?=contact_form_post('Message');?></textarea></div></td></tr>
<tr><th><?=$contact_form_field_number;?></th><td colspan="2"><div class="input" style="float: left;"><input type="text" name="Number" autocomplete="off">
</div>
</td>
</tr>
<tr>
<th> </th>
<td width="60"><img width="60" height="17" src="<?=$contact_form_image_url;?>?sname=<?=session_name();?>" alt=""></td>
<td width="324"><span class="input" style="float: left;"><small>
<?=$contact_form_field_prompt;?>
</small></span></td>
</tr>
<tr>
<th colspan="3"> </th>
</tr>
<tr><th></th><td colspan="2"><input id="submit_contact" class="submit" type="submit" name="Contact" value="<?=$contact_form_graphic_button ? '' : $contact_form_button;?>"></td></tr>
</table>
</form>
<?php } ?>
</div>
Stripslashes is handig wanneer je escaped kakakters weer wilt escapen :)
Bij sql insert moet je deze namelijk escapen
hoe werkt dit functie:dit heb je \' -> strip slash dit maakt het-> '
ps php.net aanrader!
Je form verlies waarde... mmm.. als ik je code bekijk zie ik dat je voor de variables namen laadt, je test eerst of form verzonden is! dat is op zich normaal, maar op dat punt wijs je de variablen toe.
<input type= text value="">
verschilt met
<input type= text value="">
Ik denk dat dit je probleem ook oplost.
Suc6
Rico
Dit gaat over radio-buttons.
een voorbeeldje, we gaan ervan uit dat je Alkmaar standaard geselecteerd wilt hebben:
Code (php)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
<?php
$locationValue = '072';
if(isset($_POST['location'])) {
$locationValue = strip_slashes($_POST['location']);
}
?>
<input type="radio" name="location" value="072"<?php echo $locationValue == '072' ? ' checked="checked"' : ''; ?> /> Alkmaar
<input type="radio" name="location" value="020"<?php echo $locationValue == '020' ? ' checked="checked"' : ''; ?> /> Amsterdam
<input type="radio" name="location" value="030"<?php echo $locationValue == '030' ? ' checked="checked"' : ''; ?> /> Utrecht
$locationValue = '072';
if(isset($_POST['location'])) {
$locationValue = strip_slashes($_POST['location']);
}
?>
<input type="radio" name="location" value="072"<?php echo $locationValue == '072' ? ' checked="checked"' : ''; ?> /> Alkmaar
<input type="radio" name="location" value="020"<?php echo $locationValue == '020' ? ' checked="checked"' : ''; ?> /> Amsterdam
<input type="radio" name="location" value="030"<?php echo $locationValue == '030' ? ' checked="checked"' : ''; ?> /> Utrecht
Volgens mij moet je er zo wel uitkomen... zoniet horen we het weer!
Bedankt, maar ik wil standaard niets geselecteerd hebben. zoek me rot op internet...lol blijkt niet zo eenvoudig. Wie weet er meer??
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
<p>
<input name="Location" type="radio" value="loc1"<?php if ($_POST['Location'] == 'loc1') { echo 'checked'; } ?> />
loc1<br />
<input name="Location" type="radio" value="loc2"<?php if ($_POST['Location'] == 'loc2') { echo 'checked'; } ?> />
loc2<br />
<input name="Location" type="radio" value="loc3"<?php if ($_POST['Location'] == 'loc3') { echo 'checked'; } ?> />
loc3
</p>
<input name="Location" type="radio" value="loc1"<?php if ($_POST['Location'] == 'loc1') { echo 'checked'; } ?> />
loc1<br />
<input name="Location" type="radio" value="loc2"<?php if ($_POST['Location'] == 'loc2') { echo 'checked'; } ?> />
loc2<br />
<input name="Location" type="radio" value="loc3"<?php if ($_POST['Location'] == 'loc3') { echo 'checked'; } ?> />
loc3
</p>
Code (php)
1
2
3
4
5
2
3
4
5
<p>
<input type="radio" name="Location" value="loc1" <?PHP if(isset($_POST["Location"]) and $_POST["Location"]==value) {ECHO "\"checked\" />";}?>
<input type="radio" name="Location" value="loc2" <?PHP if(isset($_POST["Location"]) and $_POST["Location"]==value) {ECHO "\"checked\" />";}?>
<input type="radio" name="Location" value="loc3" <?PHP if(isset($_POST["Location"]) and $_POST["Location"]==value) {ECHO "\"checked\" />";}?>
</p>
<input type="radio" name="Location" value="loc1" <?PHP if(isset($_POST["Location"]) and $_POST["Location"]==value) {ECHO "\"checked\" />";}?>
<input type="radio" name="Location" value="loc2" <?PHP if(isset($_POST["Location"]) and $_POST["Location"]==value) {ECHO "\"checked\" />";}?>
<input type="radio" name="Location" value="loc3" <?PHP if(isset($_POST["Location"]) and $_POST["Location"]==value) {ECHO "\"checked\" />";}?>
</p>
Code (php)
1
2
3
4
5
6
7
2
3
4
5
6
7
<input name="Location" type="radio" value="loc1" <?php if($_POST['Location']=='loc1') {echo 'checked';} ?> />
<input name="Location" type="radio" value="loc2" <?php if($_POST['Location']=='loc2') {echo 'checked';} ?> />
<input name="Location" type="radio" value="loc3" <?php if($_POST['Location']=='loc3') {echo 'checked';} ?> />
<input type="radio" name="Location" value="loc1" <?PHP if(isset($_POST["Location"]) and $_POST["Location"]==value) {ECHO "\"checked\" />";}?>
<input type="radio" name="Location" value="loc2" <?PHP if(isset($_POST["Location"]) and $_POST["Location"]==value) {ECHO "\"checked\" />";}?>
<input type="radio" name="Location" value="loc3" <?PHP if(isset($_POST["Location"]) and $_POST["Location"]==value) {ECHO "\"checked\" />";}?>
<input name="Location" type="radio" value="loc2" <?php if($_POST['Location']=='loc2') {echo 'checked';} ?> />
<input name="Location" type="radio" value="loc3" <?php if($_POST['Location']=='loc3') {echo 'checked';} ?> />
<input type="radio" name="Location" value="loc1" <?PHP if(isset($_POST["Location"]) and $_POST["Location"]==value) {ECHO "\"checked\" />";}?>
<input type="radio" name="Location" value="loc2" <?PHP if(isset($_POST["Location"]) and $_POST["Location"]==value) {ECHO "\"checked\" />";}?>
<input type="radio" name="Location" value="loc3" <?PHP if(isset($_POST["Location"]) and $_POST["Location"]==value) {ECHO "\"checked\" />";}?>
Niet Bumpen::
Gewijzigd op 01/01/1970 01:00:00 door Milcoi