1

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?

2
  • What do you see from print_r($_POST) Commented Jun 8, 2016 at 17:21
  • Just Array ( ) and nothing else Commented Jun 8, 2016 at 17:26

1 Answer 1

1

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']);
        }
Sign up to request clarification or add additional context in comments.

11 Comments

The query gets executed in the opslaan fucntion but the data from $_POST['voorname'] etc. still has no value
@B.Hulshof Check with the part that reads if(isset($_POST['wijzigen'])) {. No one but you can tell if $results contains some data... foreach ($result as $rij) { may be another area to check. It is suggested to first do a var_dump after $result = mysqli_query($link, $query_selecteren); var_dump($result); exit; to see what you get...
the foreach loop contains data. I know this because it get's used as placeholder
@B. Hulshof Nice you mentioned that. You actually solved the problem yourself. I will update the post right now....
We are close, there seems to be some trouble with contact_id. The checkbox seems to get the contact_id from the next contact. For example: I have contact 1 and 2. When I check contact 1 and type in data and hit save, it saves the data from contact 2 to contact 1, this should not happen.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.