dropdown mysql-data verzenden met formulier

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Scrippy do

scrippy do

08/04/2011 23:23:22
Quote Anchor link
hi daar,

Ik ben met een formulier bezig en heb binnen dit formulier een dual dropdownfunctie werkend. bron:
http://www.phphulp.nl/php/script/snippets/dynamic-linked-dropdown-menu-phpmysqljavascript/481/

Nu wil ik deze data gaan verzenden met een formulier.
Ik kom er niet uit hoe ik dit moet coden:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
<select name="subject" type="text" id="subject">
                <? foreach($subjects as $value) { ?>
                    <option value="<?=$value ?>"><?=$value ?></option>
                <? } ?>
            </select>


Kan iemand mij op weg helpen?
 
PHP hulp

PHP hulp

29/04/2024 12:11:06
 
- SanThe -

- SanThe -

09/04/2011 02:28:55
Quote Anchor link
Select heeft geen type.
 
Scrippy do

scrippy do

09/04/2011 22:15:25
Quote Anchor link
hierbij mijn form.
Ik krijg het niet voor elkaar de juiste waarde/data uit de variabele met het formulier meetesturen.

Doe ik iets verkeerd met het definieeren van de variabele?


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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php

$Group1
= $_POST["Group1"];
$Group2 = $_POST["Group2"];

// CONFIG YOUR E-MAIL
define('SEND_TO', '[email protected]');
define('SUBJECTS', 'Subject 1, Subject 2, Subject 3');
define('SUCCESS_MESSAGE', 'Thank you {name}.');
define('INBOX_MESSAGE', 'Name: {name} {enter} Company: {company} {enter} E-mail: {email} {enter} group1: {$group1} {enter} group2: {$group2} {enter} Phone number: {phone} {enter} group_01_options: {group_01_options} {enter} Comment: {enter} {comment}');


// CONFIG YOUR ERROR MESSAGES
define('ERROR_MESSAGE_NAME', '(Name can\'t be blank)');
define('ERROR_MESSAGE_EMAIL', '(E-mail can\'t be blank)');
define('ERROR_MESSAGE_EMAIL_INVALID', '(Email is invalid)');
define('ERROR_MESSAGE_PHONE_INVALID', '(Phone is invalid)');
define('ERROR_MESSAGE_COMMENT', '(Your comment can\'t be blank)');
define('ERROR_MESSAGE_CAPTCHA', '(Verification code can\'t be blank)');
define('ERROR_MESSAGE_CAPTCHA_INVALID', '(Verification code is invalid)');


// DON'T TOUCH BEGIN
@ini_set('session.use_cookies', '1');
@
ini_set('session.use_trans_sid', 'false');
    
@
session_set_cookie_params(0, '/');
@
session_start();

if (ini_get('register_globals')) {
    $globals = array('_SESSION');

    foreach ($globals as $global) {
        foreach ($GLOBALS[$global] as $key => $value) {
            if ($value === @$GLOBALS[$key]) {
                unset($GLOBALS[$key]);
            }
        }
    }
}

@
ini_set('magic_quotes_gpc', 'Off');

if (ini_get('magic_quotes_gpc')) {
    function
clean($data) {
           if (is_array($data)) {
              foreach ($data as $key => $value) {
                $data[$key] = clean($value);
              }
        }
else {
              $data = stripslashes($data);
        }

        return $data;
    }
            
    $_POST = clean($_POST);
}

function
php_self(){
    if(isset($_SERVER['PATH_INFO']) && strlen($_SERVER['PATH_INFO'])> 0){
        return substr($_SERVER['PHP_SELF'], 0, (strlen($_SERVER['PHP_SELF']) - @strlen($_SERVER['PATH_INFO'])));
    }
else{
        return $_SERVER['PHP_SELF'];
    }
}


$subjects = explode(',', SUBJECTS);

$error                    = FALSE;
$send                     = FALSE;

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $name                = $_POST['name'];
    $company            = $_POST['company'];
    $email               = $_POST['email'];
    $phone               = $_POST['phone'];
    $comment            = $_POST['comment'];
    $captcha            = $_POST['captcha'];
        
    $pfind = array(
                  '+',
                '-',
                ' '
            );
            
    $phone_check = str_replace($pfind, '', $phone);
    
    if(empty($name)){
        $error_name     = ERROR_MESSAGE_NAME;
        $error             = TRUE;    
    }

    
    if(empty($email)){
        $error_email    = ERROR_MESSAGE_EMAIL;
        $error             = TRUE;    
    }
elseif(!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', $email)){
        $error_email    = ERROR_MESSAGE_EMAIL_INVALID;
        $error             = TRUE;
    }

    
    if(!empty($phone)){
        if(!ctype_digit($phone_check)){
            $error_phone       = ERROR_MESSAGE_PHONE_INVALID;
            $error             = TRUE;
        }
    }

    

    if(empty($comment)){
        $error_comment  = ERROR_MESSAGE_COMMENT;
        $error             = TRUE;    
    }

    
    if(empty($captcha)){
        $error_captcha  = ERROR_MESSAGE_CAPTCHA;
        $error             = TRUE;    
    }
elseif($_SESSION['captcha'] != $captcha){
        $error_captcha  = ERROR_MESSAGE_CAPTCHA_INVALID;
        $error             = TRUE;
    }

    
    if(!$error) {
    
        if(strtoupper(substr(PHP_OS, 0, 3) == 'WIN')) {
              $ent         = "\r\n";
        }
elseif(strtoupper(substr(PHP_OS, 0, 3) == 'MAC')) {
              $ent         = "\r";
        }
else{
             $ent         = "\n";
        }

        
        $find = array(
                  '{name}',
                  '{company}',
                '{email}',
                '{phone}',
                '{group_01_options}',
                  '{comment}',
                  '{enter}'
            );
    
        $replace = array(
                  'name'         => $name,
                'company'     => $company,
                'email'     => $email,
                'phone'     => $phone,
                'group_01_options'   => $group_01_options,
                  'commment'  => $comment,
                  'enter'        => $ent
            );
            
        $comment = str_replace($find, $replace, INBOX_MESSAGE);
    
        $boundary         = '----=_NextPart_' . md5(rand());  
            
        $headers          = 'From: ' . $name . '<' . $email . '>' . $ent;
        $headers        .= 'X-Mailer: PHP/' . phpversion() . $ent;  
        $headers        .= 'MIME-Version: 1.0' . $ent;
        $headers        .= 'Content-Type: multipart/mixed; boundary="' . $boundary . '"' . $ent . $ent;  
    
        $message          = '--' . $boundary . $ent;  
        $message        .= 'Content-Type: text/plain; charset="utf-8"' . $ent;
        $message        .= 'Content-Transfer-Encoding: base64' . $ent . $ent;
        $message        .= chunk_split(base64_encode($comment));
        
        ini_set('sendmail_from', $email);
        
        mail(SEND_TO, $subject, strip_tags(html_entity_decode($message)), $headers);  
        
        $send             = TRUE;
        $success         = str_replace($find, $replace, SUCCESS_MESSAGE);
    }
}

// DON'T TOUCH END

?>










<?
// Database connection
$db_database = 'xxx';
$db_host = 'localhost';
$db_user = 'xxxx';
$db_pass = 'xxxx';
mysql_connect($db_host,$db_user,$db_pass) or die("Could not connect to MySQL (Main connection)");
mysql_select_db($db_database) or die("Could not connect to database (Main connection)");

$group_01_options = $_POST["group_01_options"];

function
dual_linked_select(
$table,            // Table to create linked selects from
$table_column_01,        // Root category
$table_column_02,        // Subcategory of the root category
$group_02_default_option_text    // Default option text for the group 2 select
)
{


// Define globals
    global $javascript;
    global $group_01_options;
    // Define variables
    $javascript = null; // Hold
    $group_01_options = null;//Hold

// Assembly of Javascript starts

    $javascript .=<<<content
    /* Linked Dropdown Selects Script Start */
    function DefaultGroup2()
    {
        var x = document.getElementById("Group2");
        x.length = 0;
        VarGroup2 = document.getElementById("Group2");
        VarGroup2.options[VarGroup2.options.length] = new Option("$group_02_default_option_text","");
        document.getElementById("Group2").disabled = true;
    }

    function CheckGroup1Select()
    {
        // If no Group1 is selected clear the Group2 and set to default
        if(document.getElementById("Group1").value == "")
        {
        // Clear the Group2 select and set to default value
        DefaultGroup2();
        }

content
;

// Javascript
    $group_01_query = "SELECT ".$table_column_01." FROM ".$table." GROUP BY ".$table_column_01;
    $group_01_result = mysql_query($group_01_query) or die(mysql_error());
    while($group_01 = mysql_fetch_array($group_01_result))
    {

        $group_01_options .= '<option value="'.$group_01[$table_column_01].'">'.$group_01[$table_column_01].'</option>'."\r\n";

        $javascript .=<<<content
        else if(document.getElementById("Group1").value == "$group_01[$table_column_01]")
        {
            // Clear the Group2 and set to default value
            DefaultGroup2();
            // Set variable options for Group2 select
            VarGroup2 = document.getElementById("Group2");

content
;

        $group_02_query = "SELECT ".$table_column_02." FROM ".$table." WHERE ".$table_column_01." = '".$group_01[$table_column_01]."' GROUP BY ".$table_column_02;
        $group_02_result = mysql_query($group_02_query) or die(mysql_error());
        while($group_02 = mysql_fetch_array($group_02_result))
        {

            $javascript .='            VarGroup2.options[VarGroup2.options.length] = new Option("'.$group_02[$table_column_02].'","'.$group_02[$table_column_02].'");'."\r\n";
        }


            $javascript .=<<<content
            document.getElementById("Group2").disabled = false;

        }

content
;
    }

        $javascript .='    }'."\r\n";

}
// End of dual_linked_selects function
?>


<?
// execute dual_linked_selects_function
dual_linked_select('type','merk_id','type','Select...');
?>













<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en" xml:lang="en">
<head>
<title>Contact Form</title>
<script type="text/javascript" language="JavaScript">
<? echo $javascript; ?>
</script>
<meta name="description" content="Contact Form" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>

<div id="box">

<? if($send){ ?>

<div id="success"><h1>Email Sent Successfully.</h1><p><?=$success; ?></p></div>

<? }else{ ?>

    <div id="form">

        <? if($error) { ?><div class="error"><h1>Invalid Form Input</h1></div><? } ?>

        <form  method="post" action="<?=php_self() ?>">

            <label for="name"><span class="required">*</span> Your Name
                <? if(!empty($error_name)) { ?>
                    <span class="error_message"><?=$error_name ?></span>
                <? }elseif(!empty($name)){ ?>
                    <img src="image/icon/accept.png" />
                <? } ?>
            </label>
            <input name="name" type="text" id="name" size="30" <? if(!empty($name)){ ?> value="<?=$name ?>" <? } ?> class="text-input" />
            
            <label for="company">Company
                <? if(!empty($error_company)) { ?>
                    <span class="error_message"><?=$error_company?></span>
                <? }elseif(!empty($company)){ ?>
                    <img src="image/icon/accept.png" />
                <? } ?>
            </label>
            <input name="company" type="text" id="company" size="30" <? if(!empty($company)){ ?> value="<?=$company ?>" <? } ?> class="text-input" />
    
    
            <label for="email"><span class="required">*</span> Email
                   <? if(!empty($error_email)) { ?>
                       <span class="error_message"><?=$error_email ?></span>
                   <? } elseif(!empty($email)){ ?>
                       <img src="image/icon/accept.png" />
                   <? } ?>
               </label>
            <input name="email" type="text" id="email" size="30" <? if(!empty($email)){ ?> value="<?=$email ?>" <? } ?> class="text-input" />
            
            
            <label for="phone">Phone number
                <? if(!empty($error_phone)) {?>
                    <span class="error_message"><?=$error_phone?></span>
                <? }elseif(!empty($phone)){ ?>
                    <img src="image/icon/accept.png" />
                <? } ?>
            </label>
            <input name="phone" type="text" id="phone" size="30" <? if(!empty($phone)){ ?> value="<?=$phone ?>"<? } ?> class="text-input" />




 <select id="Group1" name="group1" onChange="CheckGroup1Select(this);">
<option value="">Select...</option>
<? echo $group_01_options; ?>
</select>

<br>
Group2:
<br>

<select id="Group2" name="group2"><? echo $group_01_options; ?></select>






    
    
            <label for="comment"><span class="required">*</span> Your comment
                <? if(!empty($error_comment)) { ?>
                    <span class="error_message"><?=$error_comment ?></span>
                <? }elseif(!empty($comment)){ ?>
                    <img src="image/icon/accept.png" />
                <? } ?>
            </label>
            <textarea name="comment" id="comment" rows="8"><? if(!empty($comment)){ echo $comment; } ?>
</textarea>
            
            
            <label for="captcha"><span class="required">*</span> Are you human?
                <? if(!empty($error_captcha)) { ?>
                    <span class="error_message"><?=$error_captcha ?></span>
                <? } ?>
            </label>
            <div class="captcha"><img src="captcha.php" /></div>
            <input name="captcha" type="text" id="captcha" size="30" class="text-input-captcha" />
            <br />
            <input type="submit" class="submit" value="Send" />
  
        </form>

    </div>

<? } ?>

</div>
</body>
</html>
 



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.