Skip to content
Advertisement

PHP Form is not submitting when clicking the submit button [closed]

For some reason my form is not submiting and therefore I cant POST the variables to use them. I tried using the action attribute and submiting the form through jQuery, nothing works. I also get no errors in the console.

Here is the full code of my form:

<!-- Form -->
<form method="POST" enctype="multipart/form-data" action="candidatura.php" id="formCandidatura">


    <!-- Name -->
    <div class="col-md-8">
        
        <div class="form-group">

            <label for="form_name">Nome Completo *</label>
            <input type="text" class="form-control" name="candNome"
                id="candNome">

        </div>
    </div><!-- /End Name -->


    <!-- Subject -->
    <div class="col-md-4">
        <div class="form-group">

            <label for="form_subject">Data de nascimento *</label>
            <input type="date" class="form-control" name="candDataNasc"
                id="candDataNasc">

        </div>
    </div><!-- /End Subject -->


    <!-- Message -->
    <div class="col-md-4">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">Nacionalidade *</label>
            <input type="text" class="form-control" name="candNac"
                id="candNac">

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->

        <!-- Message -->
        <div class="col-md-4">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">Naturalidade *</label>
            <input type="text" class="form-control" name="candNat"
                id="candNat">

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->

        <!-- Message -->
        <div class="col-md-4">
        <!-- Form Group -->
        <div class="form-group">

        <label for="form_message">Género *</label>
        <select class="custom-select" name="candGen" id="candGen">
            <option disabled selected value="">Clique para abrir o menu</option>
            <option value="0">Masculino</option>
            <option value="1">Feminino</option>
        </select>

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->

    <!-- Message -->
    <div class="col-md-8">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">Morada *</label>
            <input type="text" class="form-control" name="candMorada"
                id="candMorada">

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->

    <!-- Message -->
    <div class="col-md-4">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">Código Postal *</label>
            <input type="text" class="form-control" name="candCodPost"
                id="candCodPost">

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->
    <div class="col-md-4">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">Cidade/loc. e freguesia *</label>
            <input type="text" class="form-control" name="candCidade"
                id="candCidade">

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->
    <div class="col-md-8">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">E-mail *</label>
            <input type="text" class="form-control" name="candEmail"
                id="candEmail">

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->
    <div class="col-md-4">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">Telefone </label>
            <input type="text" class="form-control" name="candTelefone"
                id="candTelefone">

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->
    <div class="col-md-4">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">Telemóvel *</label>
            <input type="text" class="form-control" name="candTelemovel"
                id="candTelemovel">

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->
    <div class="col-md-4">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">NIF *</label>
            <input type="text" class="form-control" name="candNif"
                id="candNif">

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->

    <!-- Message -->
    <div class="col-md-12">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">Tipo de documento de identificação *</label>
        <select class="custom-select" name="candTipoDoc" id="candTipoDoc">
            <option disabled selected value="">Clique para abrir o menu</option>
            <option value="0">Cartão de cidadão</option>
            <option value="1">Bilhete de identidade</option>
        </select>

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->
    

    <div class="col-md-6">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">Nº do documento de identificação *</label>
            <input type="text" class="form-control" name="candNDoc"
                id="candNDoc">

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->
    <div class="col-md-6">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">Validade do documento de identificação *</label>
            <input type="date" class="form-control" name="candValDoc"
                id="candValDoc">

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->
    
    <div class="col-md-6">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">Habilitações literárias *</label>
            <input type="text" class="form-control" name="candHabLit"
                id="candHabLit">
        </div><!-- /End Form Group -->
    </div><!-- /End Message -->
    <div class="col-md-6">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">Situação profissional *</label>
            <input type="text" class="form-control" name="candSitProf"
                id="candSitProf">
        </div><!-- /End Form Group -->
    </div><!-- /End Message -->
    <div class="hr-divider hr-divider-style-6 text-center">
        <i class="fa fa-comments-o"></i>
    </div>

    <div class="col-md-12">
        <!-- Form Group -->
        <div class="form-group">
            <label for="form_message">Designação do grau que possui</label>
            <input type="text" class="form-control" name="candGrau"
                id="candGrau">
        </div><!-- /End Form Group -->
    </div><!-- /End Message -->

    <div class="col-md-6">
        <!-- Form Group -->
        <div class="form-group">
            <label for="form_message">Instituição que concedeu o grau</label>
            <input type="text" class="form-control" name="candInstGrau"
                id="candInstGrau">
        </div><!-- /End Form Group -->
    </div><!-- /End Message -->
    
    <div class="col-md-6   ">
        <!-- Form Group -->
        <div class="form-group">
            <label for="form_message">Data da obtenção do grau</label>
            <input type="date" class="form-control" name="candDataGrau"
                id="candDataGrau">
        </div><!-- /End Form Group -->
    </div><!-- /End Message -->
    <div class="hr-divider hr-divider-style-6 text-center">
        <i class="fa fa-comments-o"></i>
    </div>

    <!-- Message -->
    <div class="col-md-12">
        <!-- Form Group -->
        <div class="form-group">

            <label for="form_message">Informações sobre a experiência profissional</label>
            <textarea class="form-control" name="candInstFunc"
                id="candInstFunc"></textarea>

        </div><!-- /End Form Group -->
    </div><!-- /End Message -->

    <!-- Submit Button -->
    <div class="col-md-12">
        <!-- Form Group -->
        <div class="form-group">
            <button name="submeter" id="submeter" type="submit" class="btn btn-gfort">Submeter</button>
        </div><!-- /End Form Group -->
    </div><!-- /End Submit Button -->

    
</form><!-- /End Form -->

Here is the jQuery I used to try to submit the form:

 <script>
    $(document).ready(function(){
       $("#submeter").on("click", function(){
           $("#formCandidatura").submit(); 
       });
    });
 </script>

action attribute: candidatura.php:

<?php

    //Tratamento das variáveis do formulário
    echo "<script>alert('Im here')</script>";
    $candNome      = $_POST['candNome'];
    $candDataNasc  = $_POST['candDataNasc'];
    $candNac       = addslashes($_POST['candNac']);
    $candNat       = addslashes($_POST['candNat']);
    $candGen       = addslashes($_POST['candGen']);
    $candMorada    = addslashes($_POST['candMorada']);
    $candCodPost   = $_POST['candCodPost'];
    $candCidade    = addslashes($_POST['candCidade']);
    $candEmail     = addslashes($_POST['candEmail']);
    $candTelefone  = intval($_POST['candTelefone']);
    $candTelemovel = intval($_POST['candTelemovel']);
    $candTipoDoc   = $_POST['candTipoDoc'];
    $candNDoc      = intval($_POST['candNDoc']);
    $candValDoc    = $_POST['candValDoc'];
    $candNif       = intval($_POST['candNif']);
    $candHabLit    = addslashes($_POST['candHabLit']);
    $candSitProf   = addslashes($_POST['candSitProf']);
    $candGrau      = addslashes($_POST['candGrau']);
    $candInstGrau  = addslashes($_POST['candInstGrau']);
    if ($_POST['candDataGrau'] !== "") {
        $candDataGrau = $_POST['candDataGrau'];
    } else {
        $candDataGrau = "1000-01-01";
    }
    $candInstFunc  = addslashes($_POST['candInstFunc']);
    $data = date("Y-m-d H:i:s");
    $query = "INSERT INTO tbl_candidatos(cand_nome, cand_datanasc, cand_nac, cand_nat, cand_gen, cand_morada, cand_codpost, cand_cidade, cand_email, cand_telefone, cand_telemovel, cand_tipodoc, cand_ndoc, cand_valdoc, cand_nif, cand_hablit, cand_sitprof, cand_grau, cand_instgrau, cand_datagrau, cand_instfunc, datainsc) VALUES ('$candNome','$candDataNasc','$candNac','$candNat','$candGen','$candMorada','$candCodPost','$candCidade','$candEmail','$candTelefone','$candTelemovel','$candTipoDoc','$candNDoc','$candValDoc', '$candNif','$candHabLit','$candSitProf','$candGrau','$candInstGrau','$candDataGrau','$candInstFunc', '$data')";
    die($query);
    $result = mysqli_query($link, $query); // Executa a instrução MYSQL
    if($result){
        echo "<script>alert('Dados Guardados com sucesso');</script>";
        print "<script>top.location = 'adminpanel.php?id=7';</script>";
    } else {
        echo "<script>alert('ERRO!!! Dados não guardados...');</script>";
    }
?>

Advertisement

Answer

After so much time trying to fix the issue I gradually removed my code from the page leaving only the form and it worked just fine. Then, I realized that it was a class from a div that was causing the issue of not letting the form be submitted.

 <div class="form-block contact-form-block">

The class form-block had a property “display: table” which specifies the display behavior of the div and it was making the div to behave like a table.
Since my from was inside the div, it wouldn’t submit because a form is not allowed to be a child element of a table.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement