1

** EDIT **
Just noticed that I forgot to query the $sql var so I added the mysqli_query($conn, $sql); Inside my php file that insert the data and now its working.



I'm a newbie around coding.

I'm trying to insert data into MYSQL using an $.ajax that is inside a script, that I load with $.ajax, inside a JQuery Dialog.

window.onbeforeunload = function(event) {
  var message = 'Tem certeza que deseja sair?';
  if (typeof event == 'undefined') {
    event = window.event;
  }
  if (event) {
    event.returnValue = message;
  }
  return message;
}

$(".submit_button").click(function(event) {
  var nome_c = $("#novo_Nome").val();
  var tiragem_c = $("#novo_Tiragem").val();
  var formato_c = $("#novo_Formato").val();
  var papel_c = $("#novo_Papel").val();
  var grama_c = $("#novo_Grama").val();
  var pagina_c = $("#novo_Pag").val();
  var corte_c = $("#novo_Corte").val();

  var dataString = 'nome_Trb=' + nome_c + '&tiragem_Trb=' + tiragem_c + '&formato_Trb=' + formato_c + '&papel_Trb=' + papel_c + '&gram_Trb=' + '&pag_Trb=' + pagina_c + '&corte_Trb=' + corte_c;

  if (nome_c == '' || tiragem_c == '' || formato_c == '' || papel_c == '' || grama_c == '' || pagina_c == '' || corte_c == '') {
    alert('Preencha todos os campos');
  } else {
    $.ajax({
      type: 'POST',
      url: 'includeData.php',
      data: dataString,
      cache: false,
      success: function(result) {
        alert(result);
      }
    });
  }
});
.submit_button {
  width: 80px;
  height: 25px;
}

#include_data_form input {
  width: 99%;
  float: left;
  position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">

<body>
  <table border="1" id="include_data_form">
    <tr>
      <td>
        <b>Nome:</b>
      </td>
      <td colspan="3">
        <input type="text" id="novo_Nome" name="nome_Trb" title="Editável após a inserção.">
      </td>
    </tr>
    <tr>
      <td>
        <b>Tiragem:</b>
      </td>
      <td>
        <input type="text" size="7" id="novo_Tiragem" name="tiragem_Trb" title="Editável após inserção.">
      </td>
      <td>
        <b>Formato:</b>
      </td>
      <td>
        <select name="formato_Trb" id="novo_Formato">
          <option value="standard">Standard</option>
          <option value="tabloide">Tablóide</option>
          <option value="berliner">Berliner</option>
        </select>
      </td>
    </tr>
    <tr>
      <td>
        <b>Papel:</b>
      </td>
      <td>
        <select name="papel_Trb" id="novo_Papel">
          <option value="jornal">Jornal</option>
          <option value="sulfite">Sulfite</option>
          <option value="brite">Brite</option>
        </select>
      </td>
      <td>
        <b>Gramatura:</b>
      </td>
      <td>
        <select name="gram_Trb" id="novo_Grama">
          <option value="45">45.0</option>
          <option value="63">63.0</option>
          <option value="52">52.0</option>
        </select>
      </td>
    </tr>
    <tr>
      <td>
        <b>Páginas:</b>
      </td>
      <td>
        <input type="text" size="2" title="Editável após inserção" name="pag_Trb" id="novo_Pag">
      </td>
      <td>
        <b>Corte:</b>
      </td>
      <td>
        <select name="corte_Trb" id="novo_Corte">
          <option value="0" selected>0</option>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
        </select>
      </td>
    </tr>
    <tr>
      <tr>
        <td colspan="4">
          <input type="button" value="Concluir" class="submit_button">
        </td>
      </tr>
  </table>

</body>

So the Input DATA is arranged into a variable that is sent to the PHP script. The PHP script:

<?php
require 'mainphp.php';

    $nomePost = $_POST["nome_Trb"];
    $tiragemPost = $_POST["tiragem_Trb"];
    $formatoPost = $_POST["formato_Trb"];
    $papelPost = $_POST["papel_Trb"];
    $gramPost = $_POST["gram_Trb"];
    $pagPost=  $_POST["pag_Trb"];
    $cortePost = $_POST["corte_Trb"];

    echo $nomePost . ' ' . $tiragemPost . ' ' . $formatoPost . ' ' . $papelPost . ' ' . $gramPost . ' ' . $pagPost . ' ' . $cortePost;

    $sql = "INSERT INTO db_clientes (Nome, Tiragem, Formato, Papel, Gramatura, Paginas, Corte) VALUES ('$nomePost', '$tiragemPost', '$formatoPost', '$papelPost', '$gramPost', '$pagPost', '$cortePost')";


mysqli_close($conn);

?>

I noticed that the alert(result) inside $.ajax print out the 'echo' of my php script.

jrbedard
  • 3,662
  • 5
  • 30
  • 34
Fotkurz
  • 63
  • 1
  • 6
  • 1
    You never execute your query -`$sql` -> `mysqli_query($conn, $sql);` – Sean Jan 14 '17 at 00:05
  • but you also need to learn about [How can I prevent SQL injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Sean Jan 14 '17 at 00:06
  • 1
    Thanks Sean, noticed it a second after posting, been bashing the head on the keyboard 2 days straight and only noticed it now .-. – Fotkurz Jan 14 '17 at 00:12
  • 1
    Also, thank you for the link about SQL injection! – Fotkurz Jan 14 '17 at 00:23

0 Answers0