简体   繁体   中英

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

?>

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.

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

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");

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM