1

I asked yesterday about a way to submit forms without page refreshing and you guys came up with jQuery's function $.post('url',{data}, callback).

How can I check if the data is being sent correctly to the URL I specified on the $.post() function and also check what was sent?

Here is a code I have:

HTML

<form id="formSubscripcion" >
    <div class="input-group">
        <input type="email" class="form-control" id="correoPromocion" placeholder="ejemplo@gmail.com" aria-describedby="basic-addon1">
        <span class="input-group-btn">
            <input class="btn btn-success" id="subscripcionBtn" type="submit" value="Enviar!">
        </span>
    </div>
</form>

jQuery

    $('#formSubscripcion').submit( function() {

    var correo = $('#correoPromocion').val();

        $.post('subscripcion.php', 
        {correo: correo}, 
        function(data){
            $('.suscrito').html("You've succesfully subscribed with the e-mail: <b>" + data + "</b>");
        })

PHP

<?php
include 'connection.php';

if(isset($_POST['correo'])){
    $correo = $_POST['correo'];
    if (filter_var($correo, FILTER_VALIDATE_EMAIL) && $correo != '') {
    $query = "INSERT INTO `CORREOS` (`email`) VALUES('".mysqli_real_escape_string($link, $correo)."')";
    mysqli_query($link, $query);

    echo $correo;
} 

?>

Khalil Najjar
  • 115
  • 3
  • 9

4 Answers4

3

Just put a fake email in the form and if the email is present in the database, that works. For debugging, you have the developper mode on every browser with the Network Panel and precisly the XHR Call, you can see every call, call parameter and response, etc... inside.

keupsonite
  • 399
  • 3
  • 15
  • I am checking those and I even tried inserting fake emails but nothings shows so I think my AJAX code is broken, thanks for the answer! – Khalil Najjar Jun 17 '16 at 03:29
1

If you use firefox you should consider using firebug with firephp

https://addons.mozilla.org/fr/firefox/addon/firephp/

Or you can use a server-side logger

Martial
  • 1,446
  • 15
  • 27
1

You can print the variable value to your error log:

error_log('correo: '.$_POST['correo']);

Or, if you have SMTP support on the server, you could mail yourself the variable dump instead:

ob_start();
var_dump($_POST);
$datastring = ob_get_clean();
mail("myemail@domain.com", "Dev Logging", $datastring, "From:Dev <no-reply@domain.com>\r\n"."Content-Type: text/html");
-1

$( "#formSubscripcion" ).submit(function( event ) {

  $.ajax(
      {
          type:'POST',
          url:'subscripcion.php',
          data:$('#formSubscripcion').serialize(),
          success:function(response)
          {
            console.log(response);
            var data = JSON.parse(response);
            $('.suscrito').html("You've succesfully subscribed with the e-mail: <b>" + data.correo + "</b>");
          }
      });
});
<?php
include 'connection.php';

if(isset($_POST['correo'])){
    $correo = $_POST['correo'];
    if (filter_var($correo, FILTER_VALIDATE_EMAIL) && $correo != '') {
    $query = "INSERT INTO `CORREOS` (`email`) VALUES('".mysqli_real_escape_string($link, $correo)."')";
    mysqli_query($link, $query);

    echo json_encode($_POST);
}
?>

how about this??

cakpep
  • 87
  • 6
  • Please look at this thread first: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – S.I. Oct 24 '16 at 05:03