vendredi 24 juin 2016

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="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;
} 

?>

Aucun commentaire:

Enregistrer un commentaire