1

I have this form that reads data from my DB and displays it so the user can edit those values but when I click on the form button nothing happens.

Here is my code:

Everything starts when I called this function that gets the info to display my form

static function ctrEditUser()
{
    if (isset($_POST["userEdit"])) {

    $data = AdminControl::ctrGetUser($_POST["userEdit"]);

    $nombre     = $data['userNombre'];
    $aPaterno   = $data["userPaterno"];
    $aMaterno   = $data["userMaterno"];
    $movil      = $data["userMovil"];
    $token      = $data["token"];

    include $_SERVER["DOCUMENT_ROOT"] . "/view/pages/admin/admin.form.php";
    
    }
 }

This is my form

<div class="row">
    <form class="columnas" method="post" action="" id="form">
        <div class="columna1">
            <br>
            <div class="form-group">
                <label class="adminText" for="adminName">Nombre(s):</label>
                <input type="text" class="formcontrol" name="adminName" id="adminName" value="<?php echo $nombre ?>">
            </div>
            <br><br>
            <div class="form-group">
                <label class="adminText" for="adminPaterno">Apellido Paterno:</label>
                <input type="text" class="formcontrol" name="adminPaterno" id="adminPaterno" value="<?php echo $aPaterno ?>">
            </div>
        </div>
        <div class="columna2">
            <br>
            <div class="form-group">
                <label class="adminText" for="adminMaterno">Apellido Materno:</label>
                <input type="text" class="formcontrol" name="adminMaterno" id="adminMaterno" value="<?php echo $aMaterno ?>">
            </div>
            <br><br>
            <div class="form-group">
                <label class="adminText" for="adminMovil">Teléfono Móvil:</label>
                <input type="number" class="formcontrol" name="adminMovil" id="adminMovil" placeholder="DEBEN SER SOLO 10 DIGITOS" value="<?php echo $movil ?>">
                <span class="validity"></span>
            </div>
            <br><br>
            <div class="save-button row">
                <div class="form-group columna2">
                    <input type="submit" class="sysButton" style="margin-left: 300px;" name="saveButton" value="actualizar"></input>
                </div>
            </div>
        </div>
    </form>
</div>

As you can see is just a simple form.

Once I clicked the button I need to check the form and then save the new updated data

<?php

if (isset($_POST['saveButton'])) {

     ..... more code here

but when I clicked the button nothing at all happens, it never enters the IF statement, i really don´t know what is going wrong.

I already saw a lot of examples and I thonk everything is ok.

I appreciate any help.


This code came from a previous page where you can see the $token and $_POST["userEdit"]

    <form method="post" class="btn">
                                <input type="hidden" value="<?php echo $value["token"] ?>" name="markUserDeleted">
                                <button type="submit" id="buttonDelete" class="btn-danger"><i class="fas fa-trash-alt"></i></button>
                            </form>
                        <td>
                            <form method="post" class="btn">
                                <input type="hidden" value="<?php echo $value["token"] ?>" name="userEdit">
                                <button type="submit" id="buttonEdit"><i class="fas fa-edit"></i></button>
                            </form>
                        </td>
                    </tr>
                <?php endforeach ?>
            </tbody>
        </table> 
        <?php
            if (isset($_POST["markUserDeleted"])) {
                $response = AdminControl::ctrDeleteUser();
            } elseif (isset($_POST["userEdit"])) {
                $response = AdminControl::ctrEditUser();
            }
        ?>
    </div>
</body>

This code shows a table with db data, when you click on the blue button calls the function AdminControl::ctrEditUser();

enter image description here

dgoldfeder
  • 29
  • 5
  • Just add to `action` path to your php file – Alexander Morozov Sep 14 '20 at 20:06
  • I don't remember if the submit button ("saveButton") actually gets sent with the form, since it contains no data. Try changing from `if (isset($_POST['saveButton'])) {` to `if (isset($_POST['adminName'])) {` and see if your code then enters the if statement. – cssyphus Sep 15 '20 at 21:14
  • @AlexanderMorozov @Ruli If `action=""` does not specify a PHP file, then the form fields are just posted back to the same file. The developer need only check for the form data at the top of the file and decide (if/else) whether to process the submitted form data, or display the form. Simple forms are often handled this way. Perhaps that is what the OP was doing in his code. – cssyphus Sep 15 '20 at 21:18
  • @cssyphus thanks for yoir help. I did what you told me, changing the $_POST['saveButton'] for $_POST['adminName'] and also tried with the other input names and is like there are no button at all it simply continues the flow of the code and never enters the if statement – dgoldfeder Sep 15 '20 at 21:48
  • @cssyphus I have another form just like this one the only difference is that I need to wait the user enters the fields value and then the submit button sends the $_POST name of the button without issues. – dgoldfeder Sep 15 '20 at 21:55
  • In that case, you need to "get some eyes" into the situation. You need to see for yourself what is being received. I suggest that you [write "I am here 01" etc into a file](https://www.thoughtco.com/write-to-a-file-from-php-2693790) at various points in your PHP code so that you know the program flow. Alternatively... Sadly, the Chrome extension PHPConsole seems to have vanished, but [here are some other ideas](https://stackoverflow.com/questions/4323411/how-can-i-write-to-the-console-in-php). – cssyphus Sep 16 '20 at 01:23
  • Please note that you should not post code or output as image, the images could be deleted by time and could cause troubles for users checking this question in future – Ruli Sep 16 '20 at 05:33
  • @Ruli the image is only the table not coding but thank you for the advice – dgoldfeder Sep 16 '20 at 14:04

1 Answers1

0

Problem is that your variable names are completely messed, if you print all the variables that are created in the form with the following code:

print_r($_POST);

Output is (after already inserting some data):

Array ( [adminName] => 123 [adminPaterno] => 159 [adminMaterno] => 3333 [adminMovil] => 54515456484 [saveButton] => actualizar )

You can see that there is nothing named userEdit, there is actually nothing from your named variables. Even the English/Portugal versions of variable names are messed up. Here is code that actually works (assuming the file is named test.php)

<!DOCTYPE html>
<html lang='en'>
    <head>
        <?php 
            print_r($_POST);
            if (isset($_POST["adminName"])) {
            

                $nombre     = $_POST["adminName"];
                $aPaterno   = $_POST["adminPaterno"];
                $aMaterno   = $_POST["adminMaterno"];
                $movil      = $_POST["adminMovil"];
                //$token      = $data["token"]; - no idea where you take this from
            
            }

         ?>
    </head>
        <div class="row">
            <form class="columnas" method="post" action="test.php" id="form">
                <div class="columna1">
                    <br>
                    <div class="form-group">
                        <label class="adminText" for="adminName">Nombre(s):</label>
                        <input type="text" class="formcontrol" name="adminName" id="adminName" value="<?php echo $nombre ?>">
                    </div>
                    <br><br>
                    <div class="form-group">
                        <label class="adminText" for="adminPaterno">Apellido Paterno:</label>
                        <input type="text" class="formcontrol" name="adminPaterno" id="adminPaterno" value="<?php echo $aPaterno ?>">
                    </div>
                </div>
                <div class="columna2">
                    <br>
                    <div class="form-group">
                        <label class="adminText" for="adminMaterno">Apellido Materno:</label>
                        <input type="text" class="formcontrol" name="adminMaterno" id="adminMaterno" value="<?php echo $aMaterno ?>">
                    </div>
                    <br><br>
                    <div class="form-group">
                        <label class="adminText" for="adminMovil">Teléfono Móvil:</label>
                        <input type="number" class="formcontrol" name="adminMovil" id="adminMovil" placeholder="DEBEN SER SOLO 10 DIGITOS" value="<?php echo $movil ?>">
                        <span class="validity"></span>
                    </div>
                    <br><br>
                    <div class="save-button row">
                        <div class="form-group columna2">
                            <input type="submit" class="sysButton" style="margin-left: 300px;" name="saveButton" value="actualizar"></input>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </html>

You might want to add more checks in the php part, it is up to you, I have checked just one of the variables.

Ruli
  • 2,592
  • 12
  • 30
  • 40
  • Ruli thanks for your answer. My issue here is that I need to know when the submit button is clicked and according to many websites the $_POST super global variable provides me with that information but the behavior is like the submit button doesn`t exists. I did what you suggest and took all the validation process to another file and called from action and did not work. – dgoldfeder Sep 15 '20 at 14:40
  • @dgoldfeder I have found multiple logical problems in the code, resolved them, provided minimalistic working code, if the problem is resolved now, do not forget to accept answer – Ruli Sep 15 '20 at 19:30
  • thank you again for yor time, i just added a previous code where I send the userEdit and $token variables – dgoldfeder Sep 15 '20 at 21:03
  • @Ruli There might be no `action` needed. See: https://stackoverflow.com/questions/1131781/is-it-a-good-practice-to-use-an-empty-url-for-a-html-forms-action-attribute-a – floGalen Sep 16 '20 at 08:20
  • Will edit this thanks for pointing out, however, second part of answer still applies – Ruli Sep 16 '20 at 08:34
  • @Ruli the issue still persists – dgoldfeder Sep 16 '20 at 14:02