Dropdown lijst reset naar eerste optie in lijst bij editen

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Niels Hurkmans

Niels Hurkmans

19/06/2015 10:29:19
Quote Anchor link
Beste mede forum gebruikers,

Ik ben met een project bezig, de bedoeling is dat ik een configuratiemanagement applicatie maak.
Alles werkt prima, het enigste waar ik nog problemen mee heb is dat ik verschillende dropdown lijstjes heb bij het aanmaken van een nieuwe configuratie waar ik bijvoorbeeld de keuze Ja of Nee heb en/of een lijst met IP adressen die opgehaald worden uit een andere tabel.
Als ik die keuzes heb gemaakt en de configuratie opsla en daarna wil wijzigen worden de keuzes die ik heb gemaakt gereset naar het bovenste/eerste item in die dropdown lijst.

Hij onthoud de keuze die ik heb gemaakt als ik een bestaande configuratie wil wijzigen, maar als ik een nieuwe configuratie wil toevoegen geeft hij een foutmelding: Notice: Undefined variable: gresult in C:xampp\htdocs\cmdb2\config2.php on line 130 >JA
Ik denk dat dit komt omdat de applicatie een keuze uit de database probeert op te halen die nog niet bestaat?
Ik denk zelf dat het te iets te maken heeft met een if else.., hoe kan ik dit oplossen?

Nu heb ik al het een en andere zitten proberen en dat is redelijk succesvol:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
<tr>
                        <td>
                            <label for="fname">DHCP: </label>
                        </td>
                        <td>
                                    <select name="dhcp" required></br>
                                    <option <?php if($gresult["dhcp"] == "JA") echo 'selected="selected"';?>>JA</option>
                                    <option <?php if($gresult["dhcp"] == "NEE") echo 'selected="selected"';?>>NEE</option>
                                    
                                </select>
                        </td>
                    </tr>
[/CODE]
Gewijzigd op 19/06/2015 10:31:04 door Niels Hurkmans
 
PHP hulp

PHP hulp

28/11/2024 05:37:38
 

19/06/2015 10:49:29
Quote Anchor link
Ik denk dat je moet gaan checken of je data hebt die je anders wilt wwegeven.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
<?php
if(isset($variabele)) {
// hier dan de dropdown weergeven
}
?>
 
Niels Hurkmans

Niels Hurkmans

19/06/2015 10:58:30
Quote Anchor link
Zo dan?


Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
<?php
if(isset($gresult)) {

<
select name="dhcp" required></br>
<
option [code]<?php if($gresult["dhcp"] == "JA") echo 'selected="selected"';?>
>JA</option>
<option <?php if($gresult["dhcp"] == "NEE") echo 'selected="selected"';?>>NEE</option>                                    
</select>

}
?>

[/CODE]
 

19/06/2015 11:12:03
Quote Anchor link
Ja dat kan inderdaad.
Of misschien wel specifieker.

met $gresult["dhcp"] ipv de gehele variabele $gresult.
Maar misschien krijg je dan weer errors op $gresult["dhcp"].
 
Niels Hurkmans

Niels Hurkmans

19/06/2015 11:15:27
Quote Anchor link
Ik krijg nu een syntax error: unexpected '<' online 132 ?
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
        <?php
            if(isset($gresult["dhcp"])) {

        <
select name="dhcp" required></br>
        <
option [code][code]<?php if($gresult["dhcp"] == "JA") echo 'selected="selected"';?>
>JA</option>
        <option <?php if($gresult["dhcp"] == "NEE") echo 'selected="selected"';?>>NEE</option>                                    
        </select>

        }
           ?>
 

19/06/2015 11:21:38
Quote Anchor link
Probeer dit eens. Maakt je code ook wat leesbaarder

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
<?php
if(isset($gresult["dhcp"])):?>


  <select name="dhcp" required>
      <option <?= ($gresult["dhcp"] == "JA" ? 'selected="selected' : '')>JA</option>
      <option <?= ($gresult["dhcp"] == "NEE" ? 'selected="selected' : '')>NEE</option>                                    
   </select>
<?php endif; ?>
 
Niels Hurkmans

Niels Hurkmans

19/06/2015 11:32:31
Quote Anchor link
Mmm.. Ik krijg nog steeds een : syntax error, unexpected '<' in C:\xampp\htdocs\cmdb2\config2.php on line 133

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
<tr>
                        <td>
                            <label for="fname">DHCP: </label>
                        </td>
                        <td>
                        <?php
if(isset($gresult["dhcp"])):?>


  <select name="dhcp" >
      <option <?= ($gresult["dhcp"] == "JA" ? 'selected="selected' : '')>JA</option>
      <option <?= ($gresult["dhcp"] == "NEE" ? 'selected="selected' : '')>NEE</option>                                    
   </select>
<?php endif; ?>
                        </td>
                    </tr>
 

19/06/2015 11:35:31
Quote Anchor link
oke maar post je document of iets want wij zien line 132 niet.
Er zal iets niet goed worden afgesloten ofzo
 
Niels Hurkmans

Niels Hurkmans

19/06/2015 11:39:25
Quote Anchor link
Oké, hier is het hele document:

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
<!DOCTYPE html>
<html>
<head>
    <title>update.php - .</title>
    <link href="style.css" rel="stylesheet" type="text/css" />

</head>

<script>
            $(document).ready(function() {
                var elements = document.getElementsByTagName("INPUT");
                for (var i = 0; i < elements.length; i++) {
                    elements[i].oninvalid = function(e) {
                        e.target.setCustomValidity("");
                        if (!e.target.validity.valid) {
                            e.target.setCustomValidity("Vul dit veld in aub");
                        }
                    };
                    elements[i].oninput = function(e) {
                        e.target.setCustomValidity("");
                    };
                }
            })
        </script>
        
<body>
<?php
$mysqli
= new mysqli('localhost','root','');
$mysqli->select_db('cmdb');
$dhcp_select = $gresult["dhcp"] ;
?>


    <div class="wrapper">
        <div class="content" style="width: 500px !important;">
            <?php include 'header.php'; ?><br/>
            <div>
            <form id="frmContact" method="POST" action="config1.php"         
                    onSubmit="return Validate();">
                <input type="hidden" name="id"
                value="<?php echo (isset($gresult) ? $gresult["id"] :  ''); ?>" />
                <table>
                    <tr>
                        <td>
                            <label for="fname">PC Naam: </label>
                        </td>
                        <td>
                            <input type="text" name="pc_naam"  
                            value="<?php echo (isset($gresult) ? $gresult["pc_naam"] :  ''); ?>"
                            id="pc_naam" class="txt-fld" required />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">Merk: </label>
                        </td>
                        <td>
                            <input type="text" name="merk"
                            value="<?php echo (isset($gresult) ? $gresult["merk"] :  ''); ?>"
                            id="merk" class="txt-fld" required/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">Type: </label>
                        </td>
                        <td>
                        
                        <?php
                            
                            $result
= $mysqli->query("SELECT * FROM `hardware` ORDER BY `hardware`");

                                    echo '<select name=type>';
                                    while($rows = $result->fetch_assoc()) {
                                    echo "<option>" . $rows['hardware'] . "</option>";
                            }

                                    echo '</select>';
                        ?>

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">Serienummer: </label>
                        </td>
                        <td>
                            <input type="text" name="serienummer" maxlength="30"
                            value="<?php echo (isset($gresult) ? $gresult["serienummer"] :  ''); ?>"
                            class="txt-fld" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">OS: </label>
                        </td>
                        <td>
                                    <select name="os" required></br>
                                    <option>Windows XP</option>
                                    <option>Windows Vista 32 Bits</option>
                                    <option>Windows Vista 64 Bits</option>
                                    <option>Windows 7 32 Bits</option>
                                    <option>Windows 7 64 Bits</option>
                                    <option>Windows Server</option>
                                    <option>Niet van toepassing</option>
                                </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">IP Adres: </label>
                        </td>
                        <td>
                        
                        <?php
                            
                            $result
= $mysqli->query("SELECT * FROM `ip_adressen` ORDER BY `ip_adres`");

                                    echo '<select name=ip_adres>';
                                    while($rows = $result->fetch_assoc()) {
                                    echo "<option>" . $rows['ip_adres'] . "</option>";
                            }

                                    echo '</select>';
                        ?>

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">DHCP: </label>
                        </td>
                        <td>
                        <?php
if(isset($gresult["dhcp"])):?>


  <select name="dhcp" >
      <option <?= ($gresult["dhcp"] == "JA" ? 'selected="selected' : '')>JA</option>
      <option <?= ($gresult["dhcp"] == "NEE" ? 'selected="selected' : '')>NEE</option>                                    
   </select>
<?php endif; ?>
                        </td>
                    </tr>
                        <tr>
                        <td>
                            <label for="fname">Patchpunt: </label>
                        </td>
                        <td>
                        
                        <?php
                            
                            $result
= $mysqli->query("SELECT * FROM `patch` ORDER BY `patchpunt`");

                                    echo '<select name=patchpunt>';
                                    while($rows = $result->fetch_assoc()) {
                                    ?>
<option  <?php if($gresult["patchpunt"] == $rows["patchpunt"]) echo 'selected="selected"'; echo '>' . $rows['patchpunt'] . "</option>";
                            }

                                    echo '</select>';
                        ?>

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">Serial Key: </label>
                        </td>
                        <td>
                            <input type="text" name="serial_key" maxlength="30"
                            value="<?php echo (isset($gresult) ? $gresult["serial_key"] :  ''); ?>"
                            id="serial_key" class="txt-fld" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">Gebruiker: </label>
                        </td>
                        <td>
                        
                        <?php
                            
                            $result
= $mysqli->query("SELECT * FROM `gebruikers` ORDER BY `gebruikersnaam`");

                                    echo '<select name=gebruikersnaam>';
                                    while($rows = $result->fetch_assoc()) {
                                    ?>
<option  <?php if($gresult["gebruiker"] == $rows["gebruikersnaam"]) echo 'selected="selected"'; echo '>' . $rows['gebruiker'] . "</option>";
                            }

                                    echo '</select>';
                        ?>

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">Locatie: </label>
                        </td>
                        <td>
                            <input type="text" name="locatie"
                            value="<?php echo (isset($gresult) ? $gresult["locatie"] :  ''); ?>"
                            id="locatie" class="txt-fld" required/>
                        </td>
                    </tr>
            
                </table>
                <script>
                function goBack() {
                    window.history.back();
                }
                </script>
                <input type="hidden" name="action_type" value="<?php echo (isset($gresult) ? 'edit' :  'add');?>"/>
                <div style="text-align: center; padding-top: 30px;">
                    <input class="btn" type="submit" name="save" id="save" value="Opslaan" />
                    <input class="btn" type="submit" name="save" id="cancel" value="Cancel" onclick=" return goBack();"/>
                    

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

19/06/2015 12:00:35
Quote Anchor link
Kijk eens aan.
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
<!DOCTYPE html>
<html>
<head>
    <title>update.php - .</title>
    <link href="style.css" rel="stylesheet" type="text/css"/>

</head>

<script>
    $(document).ready(function () {
        var elements = document.getElementsByTagName("INPUT");
        for (var i = 0; i < elements.length; i++) {
            elements[i].oninvalid = function (e) {
                e.target.setCustomValidity("");
                if (!e.target.validity.valid) {
                    e.target.setCustomValidity("Vul dit veld in aub");
                }
            };
            elements[i].oninput = function (e) {
                e.target.setCustomValidity("");
            };
        }
    })
</script>

<body>
<?php
$mysqli
= new mysqli('localhost', 'root', '');
$mysqli->select_db('cmdb');
$dhcp_select = $gresult["dhcp"];
?>


<div class="wrapper">
    <div class="content" style="width: 500px !important;">
        <?php include 'header.php'; ?><br/>

        <div>
            <form id="frmContact" method="POST" action="config1.php"
                  onSubmit="return Validate();">
                <input type="hidden" name="id"
                       value="<?php echo(isset($gresult) ? $gresult["id"] : ''); ?>"/>
                <table>
                    <tr>
                        <td>
                            <label for="fname">PC Naam: </label>
                        </td>
                        <td>
                            <input type="text" name="pc_naam"
                                   value="<?php echo(isset($gresult) ? $gresult["pc_naam"] : ''); ?>"
                                   id="pc_naam" class="txt-fld" required/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">Merk: </label>
                        </td>
                        <td>
                            <input type="text" name="merk"
                                   value="<?php echo(isset($gresult) ? $gresult["merk"] : ''); ?>"
                                   id="merk" class="txt-fld" required/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">Type: </label>
                        </td>
                        <td>

                            <?php

                            $result
= $mysqli->query("SELECT * FROM `hardware` ORDER BY `hardware`");

                            echo '<select name=type>';
                            while ($rows = $result->fetch_assoc()) {
                                echo "<option>" . $rows['hardware'] . "</option>";
                            }

                            echo '</select>';
                            ?>

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">Serienummer: </label>
                        </td>
                        <td>
                            <input type="text" name="serienummer" maxlength="30"
                                   value="<?= ($gresult != '' ? $gresult["serienummer"] : '') ?>"
                                   class="txt-fld"/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">OS: </label>
                        </td>
                        <td>
                            <select name="os" required></br>
                                <option>Windows XP</option>
                                <option>Windows Vista 32 Bits</option>
                                <option>Windows Vista 64 Bits</option>
                                <option>Windows 7 32 Bits</option>
                                <option>Windows 7 64 Bits</option>
                                <option>Windows Server</option>
                                <option>Niet van toepassing</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">IP Adres: </label>
                        </td>
                        <td>

                            <?php

                            $result
= $mysqli->query("SELECT * FROM `ip_adressen` ORDER BY `ip_adres`");

                            echo '<select name=ip_adres>';
                            while ($rows = $result->fetch_assoc()) {
                                echo "<option>" . $rows['ip_adres'] . "</option>";
                            }

                            echo '</select>';
                            ?>

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">DHCP: </label>
                        </td>
                        <td>
                            <?php if (isset($gresult["dhcp"])): ?>

                                <select name="dhcp">
                                    <option <?= ($gresult["dhcp"] == "JA" ? 'selected="selected' : '') ?>JA
                                    </option>
                                    <option <?= ($gresult["dhcp"] == "NEE" ? 'selected="selected' : '') ?>NEE
                                    </option>
                                </select>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">Patchpunt: </label>
                        </td>
                        <td>

                            <?php

                            $result
= $mysqli->query("SELECT * FROM `patch` ORDER BY `patchpunt`");

                            echo '<select name=patchpunt>';
                            while ($rows = $result->fetch_assoc()) {
                                ?>

                                <option  <?php if ($gresult["patchpunt"] == $rows["patchpunt"]) {
                                    echo 'selected="selected"';
                                }
echo '>' . $rows['patchpunt'] . "</option>";
                            }

                            echo '</select>';
                            ?>

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">Serial Key: </label>
                        </td>
                        <td>
                            <input type="text" name="serial_key" maxlength="30"
                                   value="<?php echo(isset($gresult) ? $gresult["serial_key"] : ''); ?>"
                                   id="serial_key" class="txt-fld"/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">Gebruiker: </label>
                        </td>
                        <td>

                            <?php

                            $result
= $mysqli->query("SELECT * FROM `gebruikers` ORDER BY `gebruikersnaam`");

                            echo '<select name=gebruikersnaam>';
                            while ($rows = $result->fetch_assoc()) {
                                ?>

                                <option  <?php if ($gresult["gebruiker"] == $rows["gebruikersnaam"]) {
                                    echo 'selected="selected"';
                                }
echo '>' . $rows['gebruiker'] . "</option>";
                            }

                            echo '</select>';
                            ?>

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="fname">Locatie: </label>
                        </td>
                        <td>
                            <input type="text" name="locatie"
                                   value="<?php echo(isset($gresult) ? $gresult["locatie"] : ''); ?>"
                                   id="locatie" class="txt-fld" required/>
                        </td>
                    </tr>

                </table>
                <script>
                    function goBack() {
                        window.history.back();
                    }
                </script>
                <input type="hidden" name="action_type" value="<?php echo(isset($gresult) ? 'edit' : 'add'); ?>"/>

                <div style="text-align: center; padding-top: 30px;">
                    <input class="btn" type="submit" name="save" id="save" value="Opslaan"/>
                    <input class="btn" type="submit" name="save" id="cancel" value="Cancel"
                           onclick=" return goBack();"/>


                </div>
            </form>
        </div>
    </div>
</div>

</body>
</html>
 
Niels Hurkmans

Niels Hurkmans

19/06/2015 12:49:45
Quote Anchor link
Ik heb het script even uitgeprobeerd en krijg nu de volgende error:

Afbeelding
 
Thomas van den Heuvel

Thomas van den Heuvel

19/06/2015 15:05:15
Quote Anchor link
Je controleert op een hoop plaatsen of $gresult uberhaupt bestaat.

Op een aantal plaatsen waar je dat niet doet (maar er vanuit gaat dat deze al bestaat) gaat het mis.

Conclusie: $gresult is niet gedefinieerd.

Trouwens, als je een YES/NO veld hebt, zou een checkbox (of zelfs een radiobutton) niet een geschikter formulierelementtype zijn?
 



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.