Berend Hulshof
Berend Hulshof

Reputation: 1049

PHP - Form not saving data to POST

So I encountered a weird problem with my form. When I click the oplsaan button it does not run through the code in if(isset($_POST['opslaan'])). The thing is that PHP doesn't save the form data to the $_POST array. For example, when I click opslaan in the following form:

    if(isset($_POST['wijzigen']))
    {
                    $query_selecteren = 'SELECT *
                                        FROM contacten';

                    $result = mysqli_query($link, $query_selecteren);

                    echo '<form id="wijzigingscherm" method="post" action="' . $_SERVER['PHP_SELF'] . '">';
                        echo '<table>
                            <tr>
                                <th>Keuze</th>
                                <th>Afbeelding</th>
                                <th>Voornaam</th>
                                <th>Achternaam</th>
                                <th>Woonplaats</th>
                                <th>Telefoonnummer</th>
                                <th>Adres</th>
                                <th>Postcode</th>
                                <th>Opmerking</th>
                            </tr>';
                            foreach ($result as $rij) {
                                echo '<tr>';
                                    $rijnr%2==0 ? $rijkleur='#6B9ED1' : $rijkleur='#99B3CC';
                                    echo '<td style="background-color'  . $rijkleur .'"> <input type="checkbox" name="checkedUser" value="' . $rij['contact_id'] . '"/>';
                                    echo '<td style="background-color'.$rijkleur.'"> <img src="ImgPersonen/profiel.jpg"/>';
                                    echo '<td style="background-color:' . $rijkleur .'"> <input size="7" type="text" name="voornaam" placeholder="' . $rij['vnaam'] .'"/></td>';
                                    echo '<td style="background-color:' . $rijkleur .'"> <input size="7" type="text" name="achternaam" placeholder="' . $rij['anaam'] . '"</td>';
                                    echo '<td style="background-color:' . $rijkleur .'"> <input size="7" type="text" name="woonplaats" placeholder="' . $rij['wplaats'] . '"/></td>';
                                    echo '<td style="background-color:' . $rijkleur .'"> <input size="7" type="text" name="telefoonnummer" placeholder="' . $rij['tnummer'] . '"/></td>';
                                    echo '<td style="background-color:' . $rijkleur .'"> <input size="7" type="text" name="adres" placeholder="' . $rij['adres'] . '"/></td>';
                                    echo '<td style="background-color:' . $rijkleur . '"> <input size="7" type="text" name="postcode" placeholder="' . $rij['postcode'] . '"/></td>';
                                    echo '<td style="background-color:' . $rijkleur . '"> <input size="7" type="text" name="opmerking" placeholder="' . $rij['opmerking'] . '"/></td>';
                                    $rijnr++;
                                echo '</tr>';
                            }
                        echo '</table>
                        <input type="submit" name="opslaan" style="width:15%;" value="Opslaan" />
                        <input type="submit" name="annuleren" style="width:15%;" value="Annuleren" />
                    </form>';
    }

It should run this code which I placed under session_start

if(isset($_POST['opslaan']))
{
    $query_opslaan = 'a query using the post data';

    mysqli_query($link, $query_opslaan) or die(mysqli_error($query_opslaan));                       

    header('location:'.$_SERVER['PHP_SELF']);
}

but the weird thing is that the if(isset($_POST['osplaan'])) doesn't work for some reason and I really don't know what I'm doing wrong here! Can someone please tell me what I'm doing wrong here?

Upvotes: 1

Views: 1003

Answers (1)

Poiz
Poiz

Reputation: 7617

One of your Inputs Fields wasn't properly closed: achternaam and thus it might present some really unexpected results. Below is what you might want to try, instead and by the way; your Form Tag should be nested within the Loop for this to work:

THE FORM PART

    <?php
        if(isset($_POST['wijzigen'])) {
            $query_selecteren = 'SELECT *
                                                FROM contacten';

            $result = mysqli_query($link, $query_selecteren);
            $count  = 1;

            echo '<table>
                                    <tr>
                                        <th>Keuze</th>
                                        <th>Afbeelding</th>
                                        <th>Voornaam</th>
                                        <th>Achternaam</th>
                                        <th>Woonplaats</th>
                                        <th>Telefoonnummer</th>
                                        <th>Adres</th>
                                        <th>Postcode</th>
                                        <th>Opmerking</th>
                                        <th>Edits</th>
                                    </tr>';
            foreach ($result as $rij) {
                echo '<form id="wijzigingscherm" method="post" action="' . $_SERVER['PHP_SELF'] . '">';
                echo '<tr>';
                $rijkleur = ($rijnr%2==0) ? '#6B9ED1' : '#99B3CC';
                echo '<td style="background-color'  . $rijkleur .'"> <input type="checkbox" name="checkedUser" value="'                 . $rij['contact_id']    . '"/>';
                echo '<td style="background-color'  .$rijkleur.'"> <img src="ImgPersonen/profiel.jpg"/>';
                echo '<td style="background-color:' . $rijkleur .'"> <input size="7" type="text" name="voornaam" placeholder="'         . $rij['vnaam']         . '" value="' . $rij['vnaam']       . '" /></td>';
                echo '<td style="background-color:' . $rijkleur .'"> <input size="7" type="text" name="achternaam" placeholder="'       . $rij['anaam']         . '" value="' . $rij['anaam']       . '" /></td>';
                echo '<td style="background-color:' . $rijkleur .'"> <input size="7" type="text" name="woonplaats" placeholder="'       . $rij['wplaats']       . '" value="' . $rij['wplaats']     . '" /></td>';
                echo '<td style="background-color:' . $rijkleur .'"> <input size="7" type="text" name="telefoonnummer" placeholder="'   . $rij['tnummer']       . '" value="' . $rij['tnummer']     . '" /></td>';
                echo '<td style="background-color:' . $rijkleur .'"> <input size="7" type="text" name="adres" placeholder="'            . $rij['adres']         . '" value="' . $rij['adres']       . '" /></td>';
                echo '<td style="background-color:' . $rijkleur . '"> <input size="7" type="text" name="postcode" placeholder="'        . $rij['postcode']      . '" value="' . $rij['postcode']    . '" /></td>';
                echo '<td style="background-color:' . $rijkleur . '"> <input size="7" type="text" name="opmerking" placeholder="'       . $rij['opmerking']     . '" value="' . $rij['opmerking']   . '" /></td>';
                echo '<td style="">
                <input type="submit" name="opslaan" style="width:15%;"      value="Opslaan" /><br />
                <input type="reset" name="annuleren" style="width:15%;"     value="Annuleren" /><br/>
                </tr>;
            </form>';
                $rijnr++;
                $count++;


            }
            echo '</table>';
        }

THE PROCESSING PART

        if(isset($_POST['opslaan'])){
                //THIS IS WHERE AND WHEN YOU GATHER THE POSTED DATA FROM THE FORM...
                $voorname           = isset($_POST['voorname'])         ? htmlspecialchars(trim($_POST['voorname']))        : null;
                $achternaam         = isset($_POST['achternaam'])       ? htmlspecialchars(trim($_POST['achternaam']))      : null;
                $woonplaats         = isset($_POST['woonplaats'])       ? htmlspecialchars(trim($_POST['woonplaats']))      : null;
                $telefoonnummer     = isset($_POST['telefoonnummer'])   ? htmlspecialchars(trim($_POST['telefoonnummer']))  : null;
                $adres              = isset($_POST['adres'])            ? htmlspecialchars(trim($_POST['adres']))           : null;
                $postcode           = isset($_POST['postcode'])         ? htmlspecialchars(trim($_POST['postcode']))        : null;
                $opmerking          = isset($_POST['opmerking'])        ? htmlspecialchars(trim($_POST['opmerking']))       : null;

                // NOW YOU CAN USE THE ABOVE VARIABLES IN YOUR QUERY, SHOULD YOU NEED TO...
                $query_opslaan      = 'a query using the post data';
                mysqli_query($link, $query_opslaan) or die(mysqli_error($query_opslaan));
                header('location:'.$_SERVER['PHP_SELF']);
        }

Upvotes: 1

Related Questions