Sorin Ilie
Sorin Ilie

Reputation: 21

Cannot Submit Form to Php Script

Im trying to make a form filling website with DevOOPS Bootstrap 3 Admin theme, and i'm having problems with posting the data from the form fields to a insert.php script that inserts the data into the database.

The theme i'm using : https://github.com/nnmware/devoops

So the theme uses ajax loader. When i insert the data into the form and then hit SUBMIT, the theme redirects me to insert.php but nothing happens. Then i tryied sending the html form data trough javascript with this :

> $(function () {
>     $('#form').on('submit',function (e) {
> 
>               $.ajax({
>                 type: 'post',
>                 url: 'insert.php',
>                 data: $('#form').serialize(),
>                 success: function () {
>                  alert("Introdus cu success!");
>                 }
>               });
>           e.preventDefault();
>         }); });

Still no clue why its not sending the data.

The insert.php

 if(isset($_POST['submit']))
{
    $sql = "INSERT INTO `dateclienti` (`Nume Societate`, `Adresa Sediu`, `Adresa Punct de lucru 1`, `Adresa Punct de lucru 2`, `Registrul Comertului`, `Cod Fiscal`, `E-Mail`, `Cont Banca`, `Model casa de marcat`, `Marca casa`, `Numar Aviz`, `Data de achizitie`, `Configuratie`, `Serie casa`, `Serie fiscala`, `Nume Pers. De contact`, `Functie`, `Serie CI`, `CNP`, `Eliberat de`, `La data de`, `Telefon`,`Data`,`ID`) VALUES ('$_POST[numesoc]', '$_POST[adresasoc]', '$_POST[punct1]', '$_POST[punct2]', '$_POST[regcom]', '$_POST[codf]', '$_POST[email]', '$_POST[contbancar]', '$_POST[modelcasa]', '$_POST[marcacasa]', '$_POST[nraviz]', '$_POST[dataach]', '$_POST[config]', '$_POST[seriecasa]', '$_POST[seriefiscala]', '$_POST[perscon]', '$_POST[functie]', '$_POST[serieci]', '$_POST[CNP]', '$_POST[eliberat]', '$_POST[dataeliberarii]', '$_POST[telefon]', '$_POST[data]', null)";
    if(mysqli_query($db,$sql))
    {
        header("location: admin.php#ajax/adaugafirma.php");
    } 
    else
    {
        echo "ERRRORRRRRR !!!!";
        printf("ERRORRRRRRR: %s\n", mysqli_error($db));
    }
}

and the page with the forms :

<form action="admin.php#ajax/insert.php" id="form" method="post" name="form" class="form"> 
<div class="row">
    <div id="breadcrumb" class="col-xs-12">
        <a href="#" class="show-sidebar">
            <i class="fa fa-bars"></i>
        </a>
    </div>
</div>
<div class="row">
    <div class="col-xs-12 col-sm-6">
        <div class="box">
            <div class="box-header">
                <div class="box-name">
                    <i class="fa fa-search"></i>
                    <span>Datele Firmei</span>
                </div>
                <div class="box-icons">
                    <a class="collapse-link">
                        <i class="fa fa-chevron-up"></i>
                    </a>
                    <a class="expand-link">
                        <i class="fa fa-expand"></i>
                    </a>
                    <a class="close-link">
                        <i class="fa fa-times"></i>
                    </a>
                </div>
                <div class="no-move"></div>
            </div>

            <div class="box-content">
                 <div class="row">
                    <div class="col-sm-offset-3 col-sm-5"><input type="text" class="form-control" placeholder="Data" name="data"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Nume Societate" name="numesoc"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Adresa Societate" name="adresasoc"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Adresa Punct de lucru 1" name="punct1"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Adresa Punct de lucru 2" name="punct2"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Registrul Comertului" name="regcom"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Cod Fiscal" name="codf"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="E-Mail" name="email"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Cont Bancar" name="contbancar"></div>
             </div>
            </div>
        </div>
    </div>
    <div class="col-xs-12 col-sm-6">
        <div class="box">
            <div class="box-header">
                <div class="box-name">
                    <i class="fa fa-search"></i>
                    <span>Datele Casei de marcat</span>
                </div>
                <div class="box-icons">
                    <a class="collapse-link">
                        <i class="fa fa-chevron-up"></i>
                    </a>
                    <a class="expand-link">
                        <i class="fa fa-expand"></i>
                    </a>
                    <a class="close-link">
                        <i class="fa fa-times"></i>
                    </a>
                </div>
                <div class="no-move">
                </div>
            </div>
            <div class="box-content">
                                      <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:40px;"><input type="text" class="form-control" placeholder="Marca Casa de marcat" name="marcacasa"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Model Casa de marcat" name="modelcasa"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Numar si data aviz" name="nraviz"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Data de achizitie" name="dataach"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Configuratie" name="config"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Serie Casa" name="seriecasa"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;padding-bottom:52px;"><input type="text" class="form-control" placeholder="Serie Fiscala" name="seriefiscala"></div>
             </div>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-header">
                <div class="box-name">
                    <i class="fa fa-search"></i>
                    <span>Datele Persoanei de</span>
                </div>
                <div class="box-icons">
                    <a class="collapse-link">
                        <i class="fa fa-chevron-up"></i>
                    </a>
                    <a class="expand-link">
                        <i class="fa fa-expand"></i>
                    </a>
                    <a class="close-link">
                        <i class="fa fa-times"></i>
                    </a>
                </div>
                <div class="no-move"></div>
            </div>
            <div class="box-content">
                                      <div class="row">
                    <div class="col-sm-offset-3 col-sm-5"><input type="text" class="form-control" placeholder="Persoana de contact" name="perscon"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Functie" name="functie"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Serie CI" name="serieci"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="CNP" name="CNP"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Eliberat de" name="eliberat"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Data eliberarii CI" name="dataeliberarii"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Telefon" name="telefon"></div>
             </div>
            </div>
        </div>
    </div>
</div>

<div class="col-sm-offset-5 col-sm-5"><button type="submit" name="submit" class="btn btn-success" value="1">Adauga</button></div>
</form>
<script type="text/javascript">
    WinMove();
</script>

Upvotes: 0

Views: 117

Answers (5)

Himanshu Upadhyay
Himanshu Upadhyay

Reputation: 6565

Try after removing action value from <form> And then put e.preventDefault as a first line in jquery function. These two changes will fix your issue. Like this:

$(function() {
  $('#form').on('submit', function(e) {
    e.preventDefault();
    $.ajax({
      type: 'post',
      url: 'insert.php',
      data: $('#form').serialize(),
      success: function() {
        alert("Introdus cu success!");
      }
    });
    return false;
  });
});

Upvotes: 0

Kyrylo Romantsov
Kyrylo Romantsov

Reputation: 182

JS part:

$(function() {
  $('#form').on('submit', function(e) {
    e.preventDefault();
    $.ajax({
      type: 'post',
      url: 'insert.php',
      data: $('#form').serialize(),
      success: function() {
        alert("Introdus cu success!");
      }
    });
    return false;
  });
});

And also there isn't field submit, try to check using if(isset($_POST['data'])) and then your code.

Upvotes: 1

Ciccio
Ciccio

Reputation: 468

If you remove action property from your form it work.

<form id="form" method="post" name="form" class="form">

Upvotes: 0

Milan Chheda
Milan Chheda

Reputation: 8249

Change your JS code to below. You should always have e.preventDefault(); first. And secondly, since your submitting via AJAX, you should return false:

$(function() {
  $('#form').on('submit', function(e) {
    e.preventDefault();
    $.ajax({
      type: 'post',
      url: 'insert.php',
      data: $('#form').serialize(),
      success: function() {
        alert("Introdus cu success!");
      }
    });
    return false;
  });
});

Upvotes: 1

Germ Knol
Germ Knol

Reputation: 1

There isn't a submit entry, try:

if(isset($_POST['data']))

Upvotes: -1

Related Questions