Khalil Najjar
Khalil Najjar

Reputation: 115

AJAX POST to PHP how to access the data sent

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="[email protected]" 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;
} 

?>

Upvotes: 1

Views: 128

Answers (4)

cakpep
cakpep

Reputation: 85

$( "#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??

Upvotes: -1

keupsonite
keupsonite

Reputation: 399

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.

Upvotes: 3

user1189882
user1189882

Reputation:

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("[email protected]", "Dev Logging", $datastring, "From:Dev <[email protected]>\r\n"."Content-Type: text/html");

Upvotes: 1

Martial
Martial

Reputation: 1562

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

Upvotes: 1

Related Questions